-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKdTree_MiS.java
More file actions
220 lines (198 loc) · 6.22 KB
/
KdTree_MiS.java
File metadata and controls
220 lines (198 loc) · 6.22 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
/**
@mseskar
@12/21/17
Create a symbol table data type whose keys are two-dimensional points.
Use a 2d-tree to support efficient range search (find all of the points contained in a query rectangle) and nearest neighbor search (find a closest point to a query point).
2d-trees have numerous applications, ranging from classifying astronomical objects to computer animation to speeding up neural networks to mining data to image retrieval.
**/
import java.util.ArrayList;
import java.util.List;
public class KdTree_MiS {
private static class Node {
private Point2D p;
private RectHV rect;
private Node lb;
private Node rt;
}
private Node tree;
private int sz;
public KdTree_MiS() {
tree = null;
sz = 0;
}
private boolean isEven(int n) {
return n % 2 == 0;
}
private boolean innerContains(Point2D p, Node node, int depth) {
int de = depth;
if (p.equals(node.p)) {
return true;
}
if (isEven(de) && p.x() <= node.p.x() || !isEven(de)
&& p.y() <= node.p.y()) {
if (node.lb == null) {
return false;
} else {
return innerContains(p, node.lb, ++de);
}
} else if (isEven(de) && p.x() > node.p.x() || !isEven(de)
&& p.y() > node.p.y()) {
if (node.rt == null) {
return false;
} else {
return innerContains(p, node.rt, ++de);
}
}
return false;
}
private void innerInsert(Point2D p, Node node, int depth) {
int de = depth;
if (isEven(de) && p.x() <= node.p.x() || !isEven(de)
&& p.y() <= node.p.y()) {
if (node.lb == null) {
RectHV newRect = null;
if (isEven(de)) {
newRect = new RectHV(node.rect.xmin(), node.rect.ymin(),
node.p.x(), node.rect.ymax());
} else {
newRect = new RectHV(node.rect.xmin(), node.rect.ymin(),
node.rect.xmax(), node.p.y());
}
Node newNode = new Node();
newNode.p = p;
newNode.lb = null;
newNode.rt = null;
newNode.rect = newRect;
node.lb = newNode;
sz++;
} else {
innerInsert(p, node.lb, ++de);
}
} else if (isEven(de) && p.x() > node.p.x() || !isEven(de)
&& p.y() > node.p.y()) {
if (node.rt == null) {
RectHV newRect = null;
if (isEven(de)) {
newRect = new RectHV(node.p.x(), node.rect.ymin(),
node.rect.xmax(), node.rect.ymax());
} else {
newRect = new RectHV(node.rect.xmin(), node.p.y(),
node.rect.xmax(), node.rect.ymax());
}
Node newNode = new Node();
newNode.p = p;
newNode.lb = null;
newNode.rt = null;
newNode.rect = newRect;
node.rt = newNode;
sz++;
} else {
innerInsert(p, node.rt, ++de);
}
}
}
private void innerDraw(Node node) {
node.p.draw();
node.rect.draw();
if (node.lb != null) {
innerDraw(node.lb);
}
if (node.rt != null) {
innerDraw(node.rt);
}
}
private void innerRange(Node node, RectHV rect, List<Point2D> a) {
if (node == null) {
return;
}
if (rect.contains(node.p)) {
a.add(node.p);
}
if (node.rect.intersects(rect)) {
innerRange(node.lb, rect, a);
innerRange(node.rt, rect, a);
}
}
private Point2D innerNearest(Node node, Point2D p, Point2D nep) {
Point2D np = nep;
if (node == null) {
return np;
}
if ((p.distanceSquaredTo(np)) < node.rect.distanceSquaredTo(p)) {
return np;
}
if (p.distanceSquaredTo(node.p) < p.distanceSquaredTo(np)) {
np = node.p;
}
if (node.lb == null && node.rt == null) {
return np;
}
if (node.lb == null) {
return innerNearest(node.rt, p, np);
}
if (node.rt == null) {
return innerNearest(node.lb, p, np);
}
if (node.lb.rect.distanceSquaredTo(p) < node.rt.rect
.distanceSquaredTo(p)) {
np = innerNearest(node.lb, p, np);
np = innerNearest(node.rt, p, np);
} else {
np = innerNearest(node.rt, p, np);
np = innerNearest(node.lb, p, np);
}
return np;
}
// is the set empty?
public boolean isEmpty() {
return sz == 0;
}
// number of points in the set
public int size() {
return sz;
}
// add the point p to the set (if it is not already in the set)
public void insert(Point2D p) {
if (contains(p)) {
return;
}
if (sz == 0) {
RectHV rect = new RectHV(0D, 0D, 1D, 1D);
tree = new Node();
tree.p = p;
tree.lb = null;
tree.rt = null;
tree.rect = rect;
sz++;
} else {
innerInsert(p, tree, 0);
}
}
// does the set contain the point p?
public boolean contains(Point2D p) {
if (sz == 0) {
return false;
}
return innerContains(p, tree, 0);
}
// draw all of the points to standard draw
public void draw() {
if (sz == 0) {
return;
}
innerDraw(tree);
}
// all points in the set that are inside the rectangle
public Iterable<Point2D> range(RectHV rect) {
List<Point2D> a = new ArrayList<Point2D>();
innerRange(tree, rect, a);
return a;
}
// a nearest neighbor in the set to p; null if set is empty
public Point2D nearest(Point2D p) {
if (sz == 0) {
return null;
}
return innerNearest(tree, p, tree.p);
}
}