-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKdTree.java
211 lines (185 loc) · 6.44 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
public class KdTree {
private static class Node {
private Point2D p;
private RectHV rect;
private Node left;
private Node right;
public Node(Point2D p, RectHV rect) {
RectHV r = rect;
if (r == null)
r = new RectHV(0, 0, 1, 1);
this.rect = r;
this.p = p;
}
}
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 root == null; }
// number of points in the set
public int size() { return size; }
// larger or equal keys go right
private Node insertH(Node x, Point2D p, RectHV rect) {
if (x == null) {
size++;
return new Node(p, rect);
}
if (x.p.equals(p)) return x;
RectHV r;
int cmp = Point2D.Y_ORDER.compare(x.p, p);
if (cmp > 0) {
if (x.left == null)
r = new RectHV(rect.xmin(), rect.ymin(), rect.xmax(), x.p.y());
else
r = x.left.rect;
x.left = insertV(x.left, p, r);
} else {
if (x.right == null)
r = new RectHV(rect.xmin(), x.p.y(), rect.xmax(), rect.ymax());
else
r = x.right.rect;
x.right = insertV(x.right, p, r);
}
return x;
}
// larger or equal keys go right
private Node insertV(Node x, Point2D p, RectHV rect) {
if (x == null) {
size++;
return new Node(p, rect);
}
if (x.p.equals(p)) return x;
RectHV r;
int cmp = Point2D.X_ORDER.compare(x.p, p);
if (cmp > 0) {
if (x.left == null)
r = new RectHV(rect.xmin(), rect.ymin(), x.p.x(), rect.ymax());
else
r = x.left.rect;
x.left = insertH(x.left, p, r);
} else {
if (x.right == null)
r = new RectHV(x.p.x(), rect.ymin(), rect.xmax(), rect.ymax());
else
r = x.right.rect;
x.right = insertH(x.right, p, r);
}
return x;
}
// add the point p to the set (if it is not already in the set)
public void insert(Point2D p) {
if (isEmpty())
root = insertV(root, p, null);
else
root = insertV(root, p, root.rect);
}
// larger or equal keys go right
private boolean contains(Node x, Point2D p, boolean vert) {
if (x == null) return false;
if (x.p.equals(p)) return true;
int cmp;
if (vert) cmp = Point2D.X_ORDER.compare(x.p, p);
else cmp = Point2D.Y_ORDER.compare(x.p, p);
if (cmp > 0) return contains(x.left, p, !vert);
else return contains(x.right, p, !vert);
}
// does the set contain the point p?
public boolean contains(Point2D p) {
return contains(root, p, true);
}
private void draw(Node x, boolean vert) {
if (x.left != null) draw(x.left, !vert);
if (x.right != null) draw(x.right, !vert);
// draw the point first
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.setPenRadius(.01);
StdDraw.point(x.p.x(), x.p.y());
// draw the line
double xmin, ymin, xmax, ymax;
if (vert) {
StdDraw.setPenColor(StdDraw.RED);
xmin = x.p.x();
xmax = x.p.x();
ymin = x.rect.ymin();
ymax = x.rect.ymax();
} else {
StdDraw.setPenColor(StdDraw.BLUE);
ymin = x.p.y();
ymax = x.p.y();
xmin = x.rect.xmin();
xmax = x.rect.xmax();
}
StdDraw.setPenRadius();
StdDraw.line(xmin, ymin, xmax, ymax);
}
// draw all of the points to standard draw
public void draw() {
// draw the rectangle
StdDraw.rectangle(0.5, 0.5, 0.5, 0.5);
if (isEmpty()) return;
draw(root, true);
}
private void range(Node x, RectHV rect, Queue<Point2D> q) {
if (x == null)
return;
if (rect.contains(x.p))
q.enqueue(x.p);
if (x.left != null && rect.intersects(x.left.rect))
range(x.left, rect, q);
if (x.right != null && rect.intersects(x.right.rect))
range(x.right, rect, q);
}
// all points in the set that are inside the rectangle
public Iterable<Point2D> range(RectHV rect) {
Queue<Point2D> q = new Queue<Point2D>();
range(root, rect, q);
return q;
}
private Point2D nearest(Node x, Point2D p, Point2D mp, boolean vert) {
Point2D min = mp;
if (x == null) return min;
if (p.distanceSquaredTo(x.p) < p.distanceSquaredTo(min))
min = x.p;
// choose the side that contains the query point first
if (vert) {
if (x.p.x() < p.x()) {
min = nearest(x.right, p, min, !vert);
if (x.left != null
&& (min.distanceSquaredTo(p)
> x.left.rect.distanceSquaredTo(p)))
min = nearest(x.left, p, min, !vert);
} else {
min = nearest(x.left, p, min, !vert);
if (x.right != null
&& (min.distanceSquaredTo(p)
> x.right.rect.distanceSquaredTo(p)))
min = nearest(x.right, p, min, !vert);
}
} else {
if (x.p.y() < p.y()) {
min = nearest(x.right, p, min, !vert);
if (x.left != null
&& (min.distanceSquaredTo(p)
> x.left.rect.distanceSquaredTo(p)))
min = nearest(x.left, p, min, !vert);
} else {
min = nearest(x.left, p, min, !vert);
if (x.right != null
&& (min.distanceSquaredTo(p)
> x.right.rect.distanceSquaredTo(p)))
min = nearest(x.right, p, min, !vert);
}
}
return min;
}
// a nearest neighbor in the set to p: null if set is empty
public Point2D nearest(Point2D p) {
if (isEmpty()) return null;
return nearest(root, p, root.p, true);
}
}