KdTree.java
7.38 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
298
299
300
301
302
303
304
305
306
307
import java.io.BufferedReader;
import java.io.FileReader;
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 vertical = true;
private static final boolean horizontal = false;
private Node root;
private int size;
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
public Node(Point2D p, RectHV rect) {
this.p = p;
this.rect = rect;
}
}
// construct an empty set of points
public KdTree() {
root = null;
}
// 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) {
if (p == null)
throw new NullPointerException();
root = insert(root, p, new RectHV(0, 0, 1, 1), vertical);
}
// helper function to recursively insert to the tree
private Node insert(Node x, Point2D p, RectHV rect, boolean orientation) {
// if x is null we've reached the end and can add a new node
if (x == null) {
this.size++;
return new Node(p, rect);
}
// if the node's point equals the point passed in
// then return that node to avoid duplicates
if (x.p.equals(p)) {
return x;
}
// determine if a node belongs to the left or right branch of the tree
// based off it's orientation. The root node is vertical and the
// orientation
// alternates between that and horizontal
if (orientation == vertical) {
// if the current node is vertical then the node it branches from
// will be horizontal
// so the x values are compared to determine which side to add the
// new node to
double cmp = p.x() - x.p.x();
if (cmp < 0) {
x.lb = insert(x.lb, p, new RectHV(x.rect.xmin(), x.rect.ymin(), x.p.x(), x.rect.ymax()), horizontal);
} else {
x.rt = insert(x.rt, p, new RectHV(x.p.x(), x.rect.ymin(), x.rect.xmax(), x.rect.ymax()), horizontal);
}
} else {
// same as above except the current node is horizontal so the
// branches will be vertical
// the y values are compared to determine which side to add the new
// node to
double cmp = p.y() - x.p.y();
if (cmp < 0) {
x.lb = insert(x.lb, p, new RectHV(x.rect.xmin(), x.rect.ymin(), x.rect.xmax(), x.p.y()), vertical);
} else {
x.rt = insert(x.rt, p, new RectHV(x.rect.xmin(), x.p.y(), x.rect.xmax(), x.rect.ymax()), vertical);
}
}
return x;
}
// does the set contain point p?
public boolean contains(Point2D p) {
if (p == null)
throw new NullPointerException();
return get(p);
}
// helper function to get a specific point p
private boolean get(Point2D p) {
return get(root, p, vertical);
}
// helper function to recursively find the node in the tree
private boolean get(Node x, Point2D p, boolean orientation) {
// the point doesn't exist in the tree
if (x == null)
return false;
// the point does exist in the tree
if (x.p.equals(p)) {
return true;
}
// compare points based on the orientation and either their x or y
// coordinate
// and returns the next node in the tree
double cmp;
if (orientation == vertical) {
cmp = p.x() - x.p.x();
} else {
cmp = p.y() - x.p.y();
}
if (cmp < 0) {
return get(x.lb, p, !orientation);
} else {
return get(x.rt, p, !orientation);
}
}
// draw all points to standard draw
public void draw() {
draw(root, vertical);
}
// draws red lines for vertical line segments
// draws blue lines for horizontal line segments
private void draw(Node x, boolean orientation) {
if (orientation == vertical) {
StdDraw.setPenColor(StdDraw.RED);
StdDraw.line(x.p.x(), x.rect.ymin(), x.p.x(), x.rect.ymax());
} else {
StdDraw.setPenColor(StdDraw.BLUE);
StdDraw.line(x.rect.xmin(), x.p.y(), x.rect.xmax(), x.p.y());
}
if (x.lb != null) {
draw(x.lb, !orientation);
}
if (x.rt != null) {
draw(x.rt, !orientation);
}
// draw point last to be on top of line
StdDraw.setPenColor(StdDraw.BLACK);
x.p.draw();
}
// all points that are inside the rectangle
public Iterable<Point2D> range(RectHV rect) {
Queue<Point2D> queue = new Queue<>();
range(root, rect, queue);
return queue;
}
// recurse through the tree to find intersecting rectangles of the
// nodes in the tree while the node is not null.
private void range(Node x, RectHV rect, Queue<Point2D> queue) {
if (x != null) {
if (!x.rect.intersects(rect)) {
return;
}
if (rect.contains(x.p)) {
queue.enqueue(x.p);
}
range(x.lb, rect, queue);
range(x.rt, rect, queue);
}
}
// a nearest neighbor in the set to point p; null if the set is empty
public Point2D nearest(Point2D p) {
if (p == null)
throw new NullPointerException();
return nearest(root, p, root.p, vertical);
}
// garbage please redo
private Point2D nearest(Node x, Point2D p, Point2D min, boolean orientation) {
if (x == null)
return min;
if (orientation == vertical) {
if (p.x() < x.p.x()) {
min = nearest(x.rt, p, min, horizontal);
if (x.lb != null && min.distanceSquaredTo(p) > x.lb.rect.distanceSquaredTo(p)) {
min = nearest(x.lb, p, min, horizontal);
}
} else {
min = nearest(x.lb, p, min, horizontal);
if (x.rt != null && min.distanceSquaredTo(p) > x.rt.rect.distanceSquaredTo(p)) {
min = nearest(x.rt, p, min, horizontal);
}
}
} else {
if (p.y() < x.p.y()) {
min = nearest(x.lb, p, min, vertical);
if (x.lb != null && min.distanceSquaredTo(p) > x.lb.rect.distanceSquaredTo(p)) {
min = nearest(x.lb, p, min, vertical);
}
} else {
min = nearest(x.lb, p, min, vertical);
if (x.rt != null && min.distanceSquaredTo(p) > x.rt.rect.distanceSquaredTo(p)) {
min = nearest(x.rt, p, min, vertical);
}
}
}
return min;
}
// unit testing of the methods (optional)
public static void main(String[] args) throws Exception {
KdTree kdtree = new KdTree();
/*
* System.out.println(kdtree.size());
* System.out.println(kdtree.isEmpty()); kdtree.insert(new Point2D(0.2,
* 0.4)); kdtree.insert(new Point2D(0.9, 0.6)); kdtree.insert(new
* Point2D(0.024, 0.34)); kdtree.insert(new Point2D(0.1, 0.6));
* kdtree.insert(new Point2D(0.6, 0.2)); kdtree.insert(new Point2D(0.7,
* 0.1)); kdtree.insert(new Point2D(0.6, 0.2)); kdtree.insert(new
* Point2D(0.7, 0.1)); kdtree.insert(new Point2D(0.5, 0.5));
*
* System.out.println(kdtree.isEmpty());
* System.out.println(kdtree.contains(new Point2D(0.97, 0.34)));
* System.out.println(kdtree.contains(new Point2D(0.5, 0.5)));
*
* Iterable<Point2D> iterable = kdtree.range(new RectHV(0,0,1,1));
*
* for(Point2D point : iterable){ System.out.println(point.toString());
* }
*
* kdtree.draw();
*/
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(args[0]));
} catch (Exception e) {
System.out.println("File not found");
}
String line;
while ((line = reader.readLine()) != null) {
String[] splitLine = line.trim().split("\\s+");
double a = Double.parseDouble(splitLine[0]);
double b = Double.parseDouble(splitLine[1]);
Point2D p = new Point2D(a, b);
kdtree.insert(p);
}
}
}