-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2dTree.java
273 lines (229 loc) · 7.53 KB
/
2dTree.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
import edu.princeton.cs.algs4.Point2D;
import edu.princeton.cs.algs4.RectHV;
import edu.princeton.cs.algs4.StdDraw;
import java.lang.IllegalArgumentException;
import edu.princeton.cs.algs4.Queue;
public class KdTree {
private Node root;
private int size;
private Node nodeMin;
private double dmin;
public KdTree() // construct an empty set of points
{
size = 0;
}
private class Node
{
private Point2D point;
private RectHV rec;
private int numOfAncesters; // number of ancesters;
// if even, compare x coordinate; if odd, compare y coordinate;
private Node left;
private Node right;
public Node(Point2D point)
{
this.point = point;
numOfAncesters = 0;
}
private double compareTo(Node a)
{
if(numOfAncesters % 2 == 0)
return point.x() - a.point.x();
else
return point.y()-a.point.y();
}
private double compareTo(Point2D p)
{
if(numOfAncesters % 2 == 0)
return point.x() - p.x();
else
return point.y() - p.y();
}
}
public boolean isEmpty() // is the set empty?
{
return root == null;
}
public int size() // number of points in the set
{
return size;
}
public void insert(Point2D p) // add the point to the set (if it is not already in the set)
{
if(p == null) throw new IllegalArgumentException("empty point");
Node node = new Node(p);
size += 1;
if(root == null)
{
root = node;
root.rec = new RectHV(0,0,1,1);
}
else
{
Node x = root;
Node parent = x;
double cmp = 0;
while(x != null)
{
cmp = x.compareTo(node);
parent = x;
if(cmp>0) x = x.left;
else if(cmp<0) x = x.right;
else if(x.point.equals(p))
{
size -= 1;
return;
}
else x = x.right;
}
node.numOfAncesters = parent.numOfAncesters + 1;
if(cmp>0)
{
if(node.numOfAncesters % 2 == 0)
node.rec = new RectHV(parent.rec.xmin(),parent.rec.ymin(),parent.rec.xmax(),parent.point.y());
else node.rec = new RectHV(parent.rec.xmin(),parent.rec.ymin(),parent.point.x(),parent.rec.ymax());
parent.left = node;
}
else
{
if(node.numOfAncesters % 2 == 0)
node.rec = new RectHV(parent.rec.xmin(),parent.point.y(),parent.rec.xmax(),parent.rec.ymax());
else node.rec = new RectHV(parent.point.x(),parent.rec.ymin(),parent.rec.xmax(),parent.rec.ymax());
parent.right = node;
}
}
}
public boolean contains(Point2D p) // does the set contain point p?
{
if(size == 0) return false;
Node x = root;
double cmp;
while(x!=null)
{
cmp = x.compareTo(p);
if(cmp>0) x = x.left;
else if(cmp<0) x = x.right;
else if(x.point.equals(p)) return true;
else x = x.right;
}
return false;
}
public void draw() // draw all points to standard draw
{
StdDraw.setPenRadius(0.01);
draw(root);
}
private void draw(Node node)
{
if(node == null) return;
if(node.numOfAncesters % 2 == 0)
{
StdDraw.setPenColor(StdDraw.RED);
StdDraw.line(node.point.x(), node.rec.ymin(), node.point.x(), node.rec.ymax());
}
else
{
StdDraw.setPenColor(StdDraw.BLUE);
StdDraw.line(node.rec.xmin(), node.point.y(), node.rec.xmax(), node.point.y());
}
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.point(node.point.x(), node.point.y());
draw(node.left);
draw(node.right);
}
public Iterable<Point2D> range(RectHV rect)
// all points that are inside the rectangle (or on the boundary)
{
Queue<Point2D> queue = new Queue<Point2D>();
Point2D p1 = new Point2D(rect.xmin(),rect.ymin());
Point2D p2 = new Point2D(rect.xmax(),rect.ymax());
Point2D p3 = new Point2D(rect.xmin(),rect.ymax());
Point2D p4 = new Point2D(rect.xmax(),rect.ymin());
searchnext(root, rect, p1,p2,p3,p4, queue);
return queue;
}
private void searchnext(Node node, RectHV rect, Point2D p1,Point2D p2,Point2D p3,Point2D p4, Queue<Point2D> queue)
{
if(node == null) return;
if(node.compareTo(p1)>=0&&node.compareTo(p2)<=0)
{
if(rect.contains(node.point))
{
queue.enqueue(node.point);
}
searchnext(node.left, rect, p1,p2,p3,p4, queue);
searchnext(node.right,rect, p1,p2,p3,p4, queue);
return;
}
if(node.compareTo(p3)<0)
{
searchnext(node.right, rect, p1,p2,p3,p4, queue);
return;
}
if(node.compareTo(p4)>0)
searchnext(node.left, rect, p1,p2,p3,p4, queue);
}
public Point2D nearest(Point2D p)
// a nearest neighbor in the set to point p; null if the set is empty
{
if(p==null) throw new IllegalArgumentException("Empty point.");
if(root == null) return null;
dmin = p.distanceSquaredTo(root.point);
nodeMin = root;
findNearest(root, p);
return nodeMin.point;
}
private void findNearest(Node node, Point2D p)
{
double d1 = 0;
double d2 = 0;
if(node.left != null && node.left.rec.contains(p))
{
double d = p.distanceSquaredTo(node.left.point);
if(d<=dmin)
{
dmin = d;
nodeMin = node.left;
}
findNearest(node.left, p);
}
if(node.right != null && node.right.rec.contains(p))
{
double d = p.distanceSquaredTo(node.right.point);
if(d<=dmin)
{
dmin = d;
nodeMin = node.right;
}
findNearest(node.right,p);
}
if(node.left != null)
{
d1 = node.left.rec.distanceSquaredTo(p);
if(0<d1 && d1<=dmin)
{
double d = p.distanceSquaredTo(node.left.point);
if(d<=dmin)
{
dmin = d;
nodeMin = node.left;
}
findNearest(node.left, p);
}
}
if(node.right != null)
{
d2 = node.right.rec.distanceSquaredTo(p);
if(0<d2 && d2<=dmin)
{
double d = p.distanceSquaredTo(node.right.point);
if(d<=dmin)
{
dmin = d;
nodeMin = node.right;
}
findNearest(node.right, p);
}
}
}
}