-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKdTree.java
More file actions
297 lines (242 loc) · 9.62 KB
/
KdTree.java
File metadata and controls
297 lines (242 loc) · 9.62 KB
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
/* *****************************************************************************
* Name:
* Date:
* Description:
**************************************************************************** */
import edu.princeton.cs.algs4.Point2D;
import edu.princeton.cs.algs4.Queue;
import edu.princeton.cs.algs4.RectHV;
import edu.princeton.cs.algs4.StdDraw;
public class KdTree {
private static final boolean VERT = true;
private static final boolean HORIZ = false;
private Node root;
private int size;
private class Node {
private Point2D point;
private Node left;
private Node right;
private boolean direction;
private RectHV leftRect;
private RectHV rightRect;
public Node(Point2D p, boolean direction, RectHV parentRect) {
this.point = p;
this.left = null;
this.right = null;
this.direction = direction;
// if line is vertical
if (this.direction == VERT) {
this.leftRect = new RectHV(parentRect.xmin(),
parentRect.ymin(),
p.x(),
parentRect.ymax());
this.rightRect = new RectHV(p.x(),
parentRect.ymin(),
parentRect.xmax(),
parentRect.ymax());
}
else {
this.leftRect = new RectHV(parentRect.xmin(),
parentRect.ymin(),
parentRect.xmax(),
p.y());
this.rightRect = new RectHV(parentRect.xmin(),
p.y(),
parentRect.xmax(),
parentRect.ymax());
}
}
}
public KdTree() {
root = null;
size = 0;
}
public boolean isEmpty() {
return (this.size == 0);
}
public int size() {
return this.size;
}
public void insert(Point2D p) {
if (p == null) throw new java.lang.IllegalArgumentException("null point");
RectHV board = new RectHV(0.0, 0.0, 1.0, 1.0);
root = insert(root, p, VERT, board);
}
private Node insert(Node node, Point2D p, boolean direc, RectHV parentRect) {
if (node == null) {
this.size++;
return new Node(p, direc, parentRect);
}
// if line should be is vertical, x coor is key
if (direc) {
if (p.x() < node.point.x()) {
if (node.leftRect.contains(p)) {
node.left = insert(node.left, p, !node.direction, node.leftRect);
}
else node.left = insert(node.left, p, !node.direction, node.rightRect);
}
else if (p.x() != node.point.x() || p.y() != node.point.y()) {
if (node.rightRect.contains(p)) {
node.right = insert(node.right, p, !node.direction, node.rightRect);
}
else node.right = insert(node.right, p, !node.direction, node.leftRect);
}
}
else {
if (p.y() < node.point.y()) {
if (node.leftRect.contains(p)) {
node.left = insert(node.left, p, !node.direction, node.leftRect);
}
else node.left = insert(node.left, p, !node.direction, node.rightRect);
}
else if (p.x() != node.point.x() || p.y() != node.point.y()) {
if (node.rightRect.contains(p)) {
node.right = insert(node.right, p, !node.direction, node.rightRect);
}
else node.right = insert(node.right, p, !node.direction, node.leftRect);
}
}
return node;
}
public boolean contains(Point2D p) {
if (p == null) throw new java.lang.IllegalArgumentException("null point");
Node node = root;
while (node != null) {
if (node.direction == VERT) {
if (p.x() < node.point.x()) {
if (node.left == null) return false;
node = node.left;
}
else if (p.x() == node.point.x() && p.y() == node.point.y()) return true;
else {
if (node.right == null) return false;
node = node.right;
}
}
else {
if (p.y() < node.point.y()) {
if (node.left == null) return false;
node = node.left;
}
else if (p.x() == node.point.x() && p.y() == node.point.y()) return true;
else {
if (node.right == null) return false;
node = node.right;
}
}
}
return false;
}
public void draw() {
Node currNode = this.root;
draw(currNode);
}
private void draw(Node currNode) {
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.setPenRadius(0.01);
StdDraw.point(currNode.point.x(), currNode.point.y());
if (currNode.direction == VERT) {
StdDraw.setPenColor(StdDraw.RED);
StdDraw.setPenRadius();
StdDraw.line(currNode.point.x(),
currNode.leftRect.ymin(),
currNode.point.x(),
currNode.leftRect.ymax());
}
else {
StdDraw.setPenColor(StdDraw.BLUE);
StdDraw.setPenRadius();
StdDraw.line(currNode.leftRect.xmin(),
currNode.point.y(),
currNode.leftRect.xmax(),
currNode.point.y());
}
if (currNode.left != null) draw(currNode.left);
if (currNode.right != null) draw(currNode.right);
}
public Iterable<Point2D> range(RectHV rect) {
if (rect == null) throw new java.lang.IllegalArgumentException("null argument");
Queue<Point2D> points = new Queue<Point2D>();
if (root != null) {
Node location = root;
range(rect, location, points);
}
return points;
}
private void range(RectHV rect, Node location, Queue<Point2D> points) {
if (rect.contains(location.point)) {
points.enqueue(location.point);
}
if (location.left != null && location.leftRect.intersects(rect)) {
range(rect, location.left, points);
}
if (location.right != null && location.rightRect.intersects(rect)) {
range(rect, location.right, points);
}
}
public Point2D nearest(Point2D p) {
if (p == null) throw new java.lang.IllegalArgumentException("null point");
if (this.root == null) return null;
if (this.contains(p)) return p;
Point2D nearestPt = this.root.point;
double nearestDist = p.distanceSquaredTo(nearestPt);
nearestPt = nearest(p, this.root, nearestPt, nearestDist);
return nearestPt;
}
private Point2D nearest(Point2D p, Node currNode, Point2D nearestPt, double nearestDist) {
double currDist = currNode.point.distanceSquaredTo(p);
if (currDist < nearestDist) {
nearestDist = currDist;
nearestPt = currNode.point;
}
double leftDist = currNode.leftRect.distanceSquaredTo(p);
double rightDist = currNode.rightRect.distanceSquaredTo(p);
if (leftDist < rightDist) {
if (leftDist < nearestDist && currNode.left != null) {
nearestPt = nearest(p, currNode.left, nearestPt, nearestDist);
nearestDist = nearestPt.distanceSquaredTo(p);
}
if (rightDist < nearestDist && currNode.right != null) {
nearestPt = nearest(p, currNode.right, nearestPt, nearestDist);
// nearestDist = nearestPt.distanceSquaredTo(p);
}
}
else {
if (rightDist < nearestDist && currNode.right != null) {
nearestPt = nearest(p, currNode.right, nearestPt, nearestDist);
nearestDist = nearestPt.distanceSquaredTo(p);
}
if (leftDist < nearestDist && currNode.left != null) {
nearestPt = nearest(p, currNode.left, nearestPt, nearestDist);
// nearestDist = nearestPt.distanceSquaredTo(p);
}
}
return nearestPt;
}
public static void main(String[] args) {
KdTree tree = new KdTree();
Point2D pa = new Point2D(0.206107, 0.095492);
tree.insert(pa);
Point2D pb = new Point2D(0.975528, 0.654508);
tree.insert(pb);
Point2D pc = new Point2D(0.024472, 0.345492);
tree.insert(pc);
Point2D pd = new Point2D(0.793893, 0.095492);
tree.insert(pd);
Point2D pe = new Point2D(0.024472, 0.345492);
tree.insert(pe);
Point2D pf = new Point2D(0.975528, 0.345492);
tree.insert(pf);
Point2D pg = new Point2D(0.206107, 0.904508);
tree.insert(pg);
Point2D ph = new Point2D(0.500000, 0.000000);
tree.insert(ph);
Point2D pi = new Point2D(0.024472, 0.654508);
tree.insert(pi);
Point2D pj = new Point2D(0.500000, 1.000000);
tree.insert(pj);
Point2D pp = new Point2D(0.75, 0.75);
System.out.println(tree.contains(pp));
System.out.println("nearest: " + tree.nearest(pp));
}
}