-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKdTree.java
executable file
·674 lines (523 loc) · 22.3 KB
/
KdTree.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.Point2D;
import edu.princeton.cs.algs4.RectHV;
import edu.princeton.cs.algs4.StdDraw;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class KdTree {
private Node root;
private int size;
public KdTree() { // construct an empty set of points
root = null;
size = 0;
}
public boolean isEmpty() { // is the set empty?
return root == null;
}
public int size() { // number of points in the set
return this.size;
}
public void insert(Point2D p) { // add the point to the set (if it is not already in the set)
if (root == null) {
Node aux = new Node(p,null);
aux.vertical = true;
root = aux;
size++;
} else {
Node aux = new Node(p,root);
insert(root, p);
}
}
private void insert(Node father, Point2D p) {
if (father.p.equals(p)) {
return;
}
Node chield = new Node(p,father);
if (father.comparator().compare(father, chield) > 0) {
if (father.lb != null) {
insert(father.lb, p);
} else {
chield.vertical = !father.vertical;
father.lb = chield;
size++;
}
} else if (father.comparator().compare(father, chield) < 0) {
if (father.rt != null) {
insert(father.rt, p);
} else {
chield.vertical = !father.vertical;
father.rt = chield;
size++;
}
} else if (father.rt != null) {
insert(father.rt, p);
} else {
chield.vertical = !father.vertical;
father.rt = chield;
size++;
}
}
public boolean contains(Point2D p) { // does the set contain point p?
Node aux = new Node(p);
return (this.search(root, aux) != null);
}
public void draw() { // draw all points to standard draw
StdDraw.clear();
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.setPenRadius(0.01);
root.p.draw();
StdDraw.setPenRadius();
StdDraw.setPenColor(StdDraw.RED);
Point2D top = new Point2D(root.p.x(), 1);
Point2D botton = new Point2D(root.p.x(), 0);
root.p.drawTo(top);
root.p.drawTo(botton);
this.breadthFirstTraversal(root);
}
private Point2D search(Node father, Node n) {
while (true) {
if (father == null) {
return null;
}
if (father.p.equals(n.p)) {
return father.p;
}
if (father.comparator().compare(father, n) > 0) {
father = father.lb;
} else if (father.comparator().compare(father, n) < 0) {
father = father.rt;
} else {
father = father.rt;
}
}
}
private void breadthFirstTraversal(Node n) {
if (n == null) {
return;
}
// left chield
if (n.lb != null && n.lb.vertical) {
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.setPenRadius(0.01);
n.lb.p.draw();
StdDraw.setPenRadius();
StdDraw.setPenColor(StdDraw.RED);
Point2D top = new Point2D(n.lb.p.x(), n.p.y());
Point2D botton = new Point2D(n.lb.p.x(), 0);
n.lb.p.drawTo(top);
n.lb.p.drawTo(botton);
} else if (n.lb != null) { // horizontal
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.setPenRadius(0.01);
n.lb.p.draw();
StdDraw.setPenRadius();
StdDraw.setPenColor(StdDraw.BLUE);
Point2D left = new Point2D(0, n.lb.p.y());
Point2D right = new Point2D(n.p.x(), n.lb.p.y());
n.lb.p.drawTo(left);
n.lb.p.drawTo(right);
}
// right chield
if (n.rt != null && n.rt.vertical) {
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.setPenRadius(0.01);
n.rt.p.draw();
StdDraw.setPenRadius();
StdDraw.setPenColor(StdDraw.RED);
Point2D top = new Point2D(n.rt.p.x(), n.p.y());
Point2D botton = new Point2D(n.rt.p.x(), 0);
n.rt.p.drawTo(top);
n.rt.p.drawTo(botton);
} else if (n.rt != null) { // horizontal
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.setPenRadius(0.01);
n.rt.p.draw();
StdDraw.setPenRadius();
StdDraw.setPenColor(StdDraw.BLUE);
Point2D left = new Point2D(0, n.rt.p.y());
Point2D right = new Point2D(n.p.x(), n.rt.p.y());
n.rt.p.drawTo(left);
n.rt.p.drawTo(right);
}
breadthFirstTraversal(n.lb);
breadthFirstTraversal(n.rt);
}
public Iterable<Point2D> range(RectHV rect) { // all points that are inside the rectangle
if (root == null)
return null;
ArrayList<Point2D> list = new ArrayList<Point2D>();
range(root, rect, list);
return list;
}
private void range(Node n, RectHV query, List<Point2D> list) {
if (query.contains(n.p)) {
list.add(n.p);
}
if (n.getRect().intersects(query)) {
// search both sides
if (n.lb != null) {
range(n.lb, query, list);
}
if (n.rt != null) {
range(n.rt, query, list);
}
} else // go to the side where the rect is
// go to the left
{
//if (n.lb != null && n.rt != null && query.distanceTo(n.lb.p) < query.distanceTo(n.rt.p)) {
if (n.lb != null && n.rt != null && n.comparator().compare(n, new Node(new Point2D(query.xmax(),query.ymax()))) > 0 ) {
range(n.lb, query, list);
} // go to the right
else if (n.lb != null && n.rt != null && n.comparator().compare(n, new Node(new Point2D(query.xmax(),query.ymax()))) < 0 ) {
range(n.rt, query, list);
} else if (n.lb != null && n.rt != null && n.comparator().compare(n, new Node(new Point2D(query.xmax(),query.ymax()))) == 0 ) {
range(n.rt, query, list);
range(n.lb, query, list);
} else if (n.lb == null && n.rt != null) // go to the right
{
range(n.rt, query, list);
} else if (n.lb != null && n.rt == null) // go to the left
{
range(n.lb, query, list);
}
}
}
public Point2D nearest(Point2D p) { // a nearest neighbor in the set to point p; null if the set is empty
Node n = new Node(p);
return nearest(root, n, root.p);
}
/*
private Point2D nearest(Node n, Point2D query, Point2D nearest) {
// go to the left
if (n.lb != null && n.rt != null && n.lb.p.distanceSquaredTo(query) < n.rt.p.distanceSquaredTo(query)) {
if (n.lb.p.distanceSquaredTo(query) < nearest.distanceSquaredTo(query)) {
nearest = nearest(n.lb, query, n.lb.p);
} else {
nearest = nearest(n.lb, query, nearest);
}
} // go to the right
else if (n.lb != null && n.rt != null && n.lb.p.distanceSquaredTo(query) > n.rt.p.distanceSquaredTo(query)) {
if (n.rt.p.distanceSquaredTo(query) < nearest.distanceSquaredTo(query)) {
nearest = nearest(n.rt, query, n.rt.p);
} else {
nearest = nearest(n.rt, query, nearest);
}
}
else if (n.lb != null && n.rt != null && n.lb.p.distanceSquaredTo(query) == n.rt.p.distanceSquaredTo(query)) {
if (n.rt.p.distanceSquaredTo(query) < nearest.distanceSquaredTo(query)) {
nearest = nearest(n.rt, query, n.rt.p);
}
if (n.lb.p.distanceSquaredTo(query) < nearest.distanceSquaredTo(query)) {
nearest = nearest(n.lb, query, nearest);
}
}
else if (n.lb == null && n.rt != null) // go to the right
{
if (n.rt.p.distanceSquaredTo(query) < nearest.distanceSquaredTo(query)) {
nearest = nearest(n.rt, query, n.rt.p);
} else {
nearest = nearest(n.rt, query, nearest);
}
} else if (n.lb != null && n.rt == null) // go to the left
{
if (n.lb.p.distanceSquaredTo(query) < nearest.distanceSquaredTo(query)) {
nearest = nearest(n.lb, query, n.lb.p);
} else {
nearest = nearest(n.lb, query, nearest);
}
}
return nearest;
}*/
private Point2D nearest(Node n, Node query, Point2D nearest) {
/* if (n.lb != null && n.rt != null) {
if (n.vertical) {
// go to the left first
if (query.p.x() < n.p.x()) // query point is in the left side of vertical line
{
//if(n.lb.p.distanceTo(query.p) < nearest.distanceTo(query.p))
{
if (n.lb.p.distanceTo(query.p) < nearest.distanceTo(query.p)) {
nearest = nearest(n.lb, query, n.lb.p);
} else {
nearest = nearest(n.lb, query, nearest);
}
}
if (n.rt.p.distanceTo(query.p) < nearest.distanceTo(query.p))
{
if (n.rt.p.distanceTo(query.p) < nearest.distanceTo(query.p)) {
nearest = nearest(n.rt, query, n.rt.p);
}
else {
nearest = nearest(n.rt, query, nearest);
}
}
}
//go to right first
if (query.p.x() >= n.p.x()) // query point is in the right side of vertical line
{
//if (n.rt.p.distanceTo(query.p) < nearest.distanceTo(query.p))
{
if (n.rt.p.distanceTo(query.p) < nearest.distanceTo(query.p)) {
nearest = nearest(n.rt, query, n.rt.p);
}
else {
nearest = nearest(n.rt, query, nearest);
}
}
if(n.lb.p.distanceTo(query.p) < nearest.distanceTo(query.p))
{
if (n.lb.p.distanceTo(query.p) < nearest.distanceTo(query.p)) {
nearest = nearest(n.lb, query, n.lb.p);
} else {
nearest = nearest(n.lb, query, nearest);
}
}
}
// horizontal
} else { // horizontal
if (query.p.y() < n.p.y()){ // query point is bellow horizontal line
// if (n.lb.p.distanceTo(query.p) < nearest.distanceTo(query.p))
{
if (n.lb.p.distanceTo(query.p) < nearest.distanceTo(query.p)) {
nearest = nearest(n.lb, query, n.lb.p);
} else {
nearest = nearest(n.lb, query, nearest);
}
}
if (n.rt.p.distanceTo(query.p) < nearest.distanceTo(query.p))
{
if (n.rt.p.distanceTo(query.p) < nearest.distanceTo(query.p)) {
nearest = nearest(n.rt, query, n.rt.p);
} else {
nearest = nearest(n.rt, query, nearest);
}
}
}
if (query.p.y() >= n.p.y()){ // query point is above horizontal line
//if (n.rt.p.distanceTo(query.p) < nearest.distanceTo(query.p))
{
if (n.rt.p.distanceTo(query.p) < nearest.distanceTo(query.p)) {
nearest = nearest(n.rt, query, n.rt.p);
} else {
nearest = nearest(n.rt, query, nearest);
}
}
if (n.lb.p.distanceTo(query.p) < nearest.distanceTo(query.p))
{
if (n.lb.p.distanceTo(query.p) < nearest.distanceTo(query.p)) {
nearest = nearest(n.lb, query, n.lb.p);
} else {
nearest = nearest(n.lb, query, nearest);
}
}
}
}
} else if (n.rt != null) {
if (n.rt.p.distanceTo(query.p) < nearest.distanceTo(query.p)) {
nearest = nearest(n.rt, query, n.rt.p);
} else {
nearest = nearest(n.rt, query, nearest);
}
} else if (n.lb != null) {
if (n.lb.p.distanceTo(query.p) < nearest.distanceTo(query.p)) {
nearest = nearest(n.lb, query, n.lb.p);
} else {
nearest = nearest(n.lb, query, nearest);
}
}*/
/*
// NOT working
// go to left first
if (n.lb != null && n.rt != null && n.comparator().compare(n, query) > 0) {
boolean changeNearest = false;
if (n.lb.p.distanceSquaredTo(query.p) < nearest.getRect().distanceSquaredTo(query.p)) {
nearest = nearest(n.lb, query, n.lb);
changeNearest = true;
} else {
nearest = nearest(n.lb, query, nearest);
}
if (!changeNearest) {
if (n.rt.p.distanceTo(query.p) < nearest.getRect().distanceTo(query.p)) {
nearest = nearest(n.rt, query, n.rt);
} else {
nearest = nearest(n.rt, query, nearest);
}
}
} // go to the right first
else if (n.lb != null && n.rt != null && n.comparator().compare(n, query) < 0) {
boolean changeNearest = false;
if (n.rt.p.distanceSquaredTo(query.p) < nearest.getRect().distanceSquaredTo(query.p)) {
nearest = nearest(n.rt, query, n.rt);
changeNearest = true;
} else {
nearest = nearest(n.rt, query, nearest);
}
if (!changeNearest) {
if (n.lb.p.distanceTo(query.p) < nearest.getRect().distanceTo(query.p)) {
nearest = nearest(n.lb, query, n.lb);
} else {
nearest = nearest(n.lb, query, nearest);
}
}
} else if (n.lb != null && n.rt != null && n.comparator().compare(n, query) == 0) {
if (n.rt.p.distanceSquaredTo(query.p) < nearest.getRect().distanceSquaredTo(query.p)) {
nearest = nearest(n.rt, query, n.rt);
} else {
nearest = nearest(n.rt, query, nearest);
}
} else if (n.lb == null && n.rt != null) // go to the right
{
if (n.rt.p.distanceSquaredTo(query.p) < nearest.getRect().distanceSquaredTo(query.p)) {
nearest = nearest(n.rt, query, n.rt);
} else {
nearest = nearest(n.rt, query, nearest);
}
} else if (n.lb != null && n.rt == null) // go to the left
{
if (n.lb.p.distanceSquaredTo(query.p) < nearest.getRect().distanceSquaredTo(query.p)) {
nearest = nearest(n.lb, query, n.lb);
} else {
nearest = nearest(n.lb, query, nearest);
}
}
*/
if (n.lb != null && n.rt != null && n.comparator().compare(n, query) > 0) {
if (n.lb.p.distanceSquaredTo(query.p) < nearest.distanceSquaredTo(query.p)) {
nearest = nearest(n.lb, query, n.lb.p);
} else {
nearest = nearest(n.lb, query, nearest);
}
if (n.rt.p.distanceTo(query.p) < nearest.distanceTo(query.p)) {
if (n.rt.p.distanceTo(query.p) < nearest.distanceTo(query.p)) {
nearest = nearest(n.rt, query, n.rt.p);
} else {
nearest = nearest(n.rt, query, nearest);
}
}
} // go to the right
else if (n.lb != null && n.rt != null && n.comparator().compare(n, query) < 0) {
if (n.rt.p.distanceSquaredTo(query.p) < nearest.distanceSquaredTo(query.p)) {
nearest = nearest(n.rt, query, n.rt.p);
} else {
nearest = nearest(n.rt, query, nearest);
}
if (n.lb.p.distanceTo(query.p) < nearest.distanceTo(query.p)) {
if (n.lb.p.distanceTo(query.p) < nearest.distanceTo(query.p)) {
nearest = nearest(n.lb, query, n.lb.p);
} else {
nearest = nearest(n.lb, query, nearest);
}
}
} else if (n.lb != null && n.rt != null && n.comparator().compare(n, query) == 0) {
if (n.rt.p.distanceSquaredTo(query.p) < nearest.distanceSquaredTo(query.p)) {
nearest = nearest(n.rt, query, n.rt.p);
} else {
nearest = nearest(n.rt, query, nearest);
}
} else if (n.lb == null && n.rt != null) // go to the right
{
if (n.rt.p.distanceSquaredTo(query.p) < nearest.distanceSquaredTo(query.p)) {
nearest = nearest(n.rt, query, n.rt.p);
} else {
nearest = nearest(n.rt, query, nearest);
}
} else if (n.lb != null && n.rt == null) // go to the left
{
if (n.lb.p.distanceSquaredTo(query.p) < nearest.distanceSquaredTo(query.p)) {
nearest = nearest(n.lb, query, n.lb.p);
} else {
nearest = nearest(n.lb, query, nearest);
}
}
return nearest;
}
private static class Node {
private Point2D p; // the point
private Node father;
// private RectHV rect; // the axis-aligned rectangle corresponding to this node
private Node lb; // the left/bottom subtree
private Node rt; // the right/top subtree
private boolean vertical;
public Node(Point2D p, Node father) {
this.father = father;
this.p = p;
}
public Node (Point2D p){
this.p = p;
this.father = null;
}
public Comparator<Node> comparator() {
return new XYOrder();
}
/* public RectHV getRect() {
if (vertical) {
return new RectHV(p.x(), 0, p.x(), 1);
} else {
return new RectHV(0, p.y(), 1, p.y());
}
}*/
public RectHV getRect() {
//if (father == null)
{
if (vertical) {
return new RectHV(p.x(), 0, p.x(), 1);
} else {
return new RectHV(0, p.y(), 1, p.y());
}
}
/*
if (vertical) {
if (father.comparator().compare(father, this) > 0){ // bellow chield
return new RectHV(this.p.x(), 0, this.p.x(), father.p.y());
}
else // above chield
{
return new RectHV(this.p.x(), father.p.y(), this.p.x(), 1);
}
} else { // horizontal
if (father.comparator().compare(father, this) > 0){ // feft chield
return new RectHV(0, this.p.y(), father.p.x(), this.p.y());
}
else // right chield
{
return new RectHV(father.p.x(), this.p.y(), 1, this.p.y());
}
} */
}
// compare points according to their x-coordinate or y-coordinate
private class XYOrder implements Comparator<Node> {
public int compare(Node p, Node q) {
if (p.vertical == true) {
return Point2D.X_ORDER.compare(p.p, q.p);
} else {
return Point2D.Y_ORDER.compare(p.p, q.p);
}
}
}
}
public static void main(String[] args) // unit testing of the methods (optional)
{
In in = new In("/home/christian/ProjetosNetBeans/Algorithms Princeton/src/circle10.txt");
StdDraw.enableDoubleBuffering();
// initialize the two data structures with point from standard input
PointSET brute = new PointSET();
KdTree kdtree = new KdTree();
while (!in.isEmpty()) {
double x = in.readDouble();
double y = in.readDouble();
Point2D p = new Point2D(x, y);
kdtree.insert(p);
//brute.insert(p);
}
boolean aux = kdtree.contains(new Point2D(0.510000, 0.000000));
Point2D p = new Point2D(0.5, 0.5);
Node n = new Node(p);
n.vertical = true;
StdDraw.clear();
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.setPenRadius(0.01);
n.getRect().draw();
StdDraw.show();
}
}