-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathKdTree.java
422 lines (350 loc) · 10.5 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
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.List;
public class KdTree {
private static class Node {
private Point2D p; // the point
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 Node(Point2D p, RectHV rect) {
this.p = p;
this.rect = rect;
this.lb = null;
this.rt = null;
}
}
private Node root;
private int size;
/**
* construct an empty set of points
*/
public KdTree() {
root = null;
size = 0;
}
/**
* is the set empty?
*/
public boolean isEmpty() {
return size == 0;
}
/**
* number of points in the set
*/
public int size() {
return size;
}
/**
* add the point to the set (if it is not already in the set)
*/
public void insert(Point2D p) {
checkNotNull(p);
if (root == null) {
root = new Node(p, new RectHV(p.x(), 0, p.x(), 1));
size += 1;
} else {
root = insertByX(p, root);
}
}
private void checkNotNull(Object o) {
if (o == null) {
throw new IllegalArgumentException("Cannot pass null");
}
}
private Node insertByX(Point2D p, Node root) {
if (p.equals(root.p)) {
return root;
}
if (p.x() < root.p.x()) {
if (root.lb == null) {
root.lb = new Node(p, new RectHV(0, p.y(), root.p.x(), p.y()));
size += 1;
} else {
root.lb = insertByY(p, root.lb);
}
} else {
if (root.rt == null) {
root.rt = new Node(p, new RectHV(root.p.x(), p.y(), 1, p.y()));
size += 1;
} else {
root.rt = insertByY(p, root.rt);
}
}
return root;
}
private Node insertByY(Point2D p, Node root) {
if (p.equals(root.p)) {
return root;
}
if (p.y() < root.p.y()) {
if (root.lb == null) {
root.lb = new Node(p, new RectHV(p.x(), 0, p.x(), root.p.y()));
size += 1;
} else {
root.lb = insertByX(p, root.lb);
}
} else {
if (root.rt == null) {
root.rt = new Node(p, new RectHV(p.x(), root.p.y(), p.x(), 1));
size += 1;
} else {
root.rt = insertByX(p, root.rt);
}
}
return root;
}
/**
* does the set contain point p?
*/
public boolean contains(Point2D p) {
checkNotNull(p);
return containsByX(p, root);
}
private boolean containsByX(Point2D p, Node root) {
if (root == null) {
return false;
}
if (root.p.equals(p)) {
return true;
}
if (p.x() < root.p.x()) {
return containsByY(p, root.lb);
} else {
return containsByY(p, root.rt);
}
}
private boolean containsByY(Point2D p, Node root) {
if (root == null) {
return false;
}
if (root.p.equals(p)) {
return true;
}
if (p.y() < root.p.y()) {
return containsByX(p, root.lb);
} else {
return containsByX(p, root.rt);
}
}
/**
* draw all points to standard draw
*/
public void draw() {
drawX(root);
}
private void drawX(Node root) {
if (root == null) {
return;
}
drawLineX(root.rect);
drawPoint(root.p);
drawY(root.lb);
drawY(root.rt);
}
private void drawLineX(RectHV rect) {
StdDraw.setPenColor(StdDraw.RED);
StdDraw.setPenRadius();
rect.draw();
}
private void drawY(Node root) {
if (root == null) {
return;
}
drawLineY(root.rect);
drawPoint(root.p);
drawX(root.lb);
drawX(root.rt);
}
private void drawLineY(RectHV rect) {
StdDraw.setPenColor(StdDraw.BLUE);
StdDraw.setPenRadius();
rect.draw();
}
private void drawPoint(Point2D p) {
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.setPenRadius(0.01);
p.draw();
}
/**
* all points that are inside the rectangle (or on the boundary)
*/
public Iterable<Point2D> range(RectHV rect) {
checkNotNull(rect);
List<Point2D> points = new ArrayList<>();
rangeByX(rect, points, root);
return points;
}
private void rangeByX(RectHV rect, List<Point2D> points, Node root) {
if (root == null) {
return;
}
if (rect.contains(root.p)) {
points.add(root.p);
}
if (rect.xmax() < root.p.x()) {
rangeByY(rect, points, root.lb);
return;
}
if (rect.xmin() > root.p.x()) {
rangeByY(rect, points, root.rt);
return;
}
rangeByY(rect, points, root.lb);
rangeByY(rect, points, root.rt);
}
private void rangeByY(RectHV rect, List<Point2D> points, Node root) {
if (root == null) {
return;
}
if (rect.contains(root.p)) {
points.add(root.p);
}
if (rect.ymax() < root.p.y()) {
rangeByX(rect, points, root.lb);
return;
}
if (rect.ymin() > root.p.y()) {
rangeByX(rect, points, root.rt);
return;
}
rangeByX(rect, points, root.lb);
rangeByX(rect, points, root.rt);
}
/**
* a nearest neighbor in the set to point p; null if the set is empty
*/
public Point2D nearest(Point2D p) {
checkNotNull(p);
if (root == null) {
return null;
}
Result res = nearestByX(p, root);
return res.p;
}
private static class Result {
Point2D p;
double distSquared;
Result(Point2D p, double distSquared) {
this.p = p;
this.distSquared = distSquared;
}
}
private Result nearestByX(Point2D p, Node root) {
Result nearest = new Result(root.p, root.p.distanceSquaredTo(p));
if (root.lb == null && root.rt == null) {
return nearest;
}
if (root.lb == null) {
Result rtResult = nearestByY(p, root.rt);
if (rtResult.distSquared < nearest.distSquared) {
return rtResult;
} else {
return nearest;
}
}
if (root.rt == null) {
Result lbResult = nearestByY(p, root.lb);
if (lbResult.distSquared < nearest.distSquared) {
return lbResult;
} else {
return nearest;
}
}
// Search the closer side first
if (p.x() < root.p.x()) {
Result lbResult = nearestByY(p, root.lb);
nearest = nearestResult(lbResult, nearest);
if (nearest.distSquared < root.rect.distanceSquaredTo(p)) {
// pruning other side
return nearest;
}
Result rtResult = nearestByY(p, root.rt);
nearest = nearestResult(rtResult, nearest);
return nearest;
} else {
Result rtResult = nearestByY(p, root.rt);
nearest = nearestResult(rtResult, nearest);
if (nearest.distSquared < root.rect.distanceSquaredTo(p)) {
// pruning other side
return nearest;
}
Result lbResult = nearestByY(p, root.lb);
nearest = nearestResult(lbResult, nearest);
return nearest;
}
}
private Result nearestResult(Result r1, Result r2) {
return r1.distSquared < r2.distSquared ? r1 : r2;
}
private Result nearestByY(Point2D p, Node root) {
Result nearest = new Result(root.p, root.p.distanceSquaredTo(p));
if (root.lb == null && root.rt == null) {
return nearest;
}
if (root.lb == null) {
Result rtResult = nearestByX(p, root.rt);
if (rtResult.distSquared < nearest.distSquared) {
return rtResult;
} else {
return nearest;
}
}
if (root.rt == null) {
Result lbResult = nearestByX(p, root.lb);
if (lbResult.distSquared < nearest.distSquared) {
return lbResult;
} else {
return nearest;
}
}
// Search the closer side first
if (p.y() < root.p.y()) {
Result lbResult = nearestByX(p, root.lb);
nearest = nearestResult(lbResult, nearest);
if (nearest.distSquared < root.rect.distanceSquaredTo(p)) {
// pruning other side
return nearest;
}
Result rtResult = nearestByX(p, root.rt);
nearest = nearestResult(rtResult, nearest);
return nearest;
} else {
Result rtResult = nearestByX(p, root.rt);
nearest = nearestResult(rtResult, nearest);
if (nearest.distSquared < root.rect.distanceSquaredTo(p)) {
// pruning other side
return nearest;
}
Result lbResult = nearestByX(p, root.lb);
nearest = nearestResult(lbResult, nearest);
return nearest;
}
}
/**
* unit testing of the methods (optional)
*/
public static void main(String[] args) {
/*
KdTree tree = new KdTree();
Point2D p1 = new Point2D(0.2, 0.2);
Point2D p2 = new Point2D(0.6, 0.4);
tree.insert(p1);
// tree.insert(p2);
System.out.println("tree.contains(p1) = " + tree.contains(p1));
System.out.println("tree.contains(p2) = " + tree.contains(p2));
*/
KdTree tree = new KdTree();
Point2D p1 = new Point2D(0.271484, 0.195313);
Point2D p2 = new Point2D(0.744141, 0.136719);
Point2D p3 = new Point2D(0.781250, 0.398438);
Point2D p4 = new Point2D(0.535156, 0.593750);
tree.insert(p1);
tree.insert(p2);
tree.insert(p3);
tree.insert(p4);
// tree.insert(null);
}
}