-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKdTree.java
More file actions
217 lines (209 loc) · 7.75 KB
/
KdTree.java
File metadata and controls
217 lines (209 loc) · 7.75 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
import edu.princeton.cs.algs4.Point2D;
import edu.princeton.cs.algs4.RectHV;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
public class KdTree {
private final Node root;
private int sz;
private class Node {
public Node leftChild;
public Node rightChild;
public boolean xCompare;
private Point2D point;
public Node(Point2D p) {
point = p;
}
public Node() {
this(null);
}
public boolean isEmpty() {
return point == null;
}
public byte compareTo(Node that) {
if (xCompare) {
double xThis = this.point.x();
double xThat = that.point.x();
if (xThis > xThat) return -1;
else if (xThis < xThat) return 1;
else return 0;
} else {
double yThis = this.point.y();
double yThat = that.point.y();
if (yThis > yThat) return -1;
else if (yThis < yThat) return 1;
else return 0;
}
}
public double coord() {
if (xCompare) {
return point.x();
} else {
return point.y();
}
}
};
private class Recur {
private Node node;
public boolean processedOnce;
public boolean isRight;
public double gap;
public Recur(Node n) {
node = n;
}
}
public KdTree() {
root = new Node();
sz = 0;
}
public boolean isEmpty() {
return root.isEmpty();
}
public int size() {
return sz;
}
public void insert(Point2D p) {
if (p == null) {
throw new IllegalArgumentException();
} else {
sz += 1;
if (root.isEmpty()) {
root.point = p;
root.xCompare = true;
} else {
Node temp = root;
Node node = new Node(p);
while (true) {
byte com = temp.compareTo(node);
if (com == 0 && temp.point.equals(node.point)) {
sz -= 1;
return;
}
if (com >= 0) {
if (temp.rightChild != null) {
temp = temp.rightChild;
} else {
node.xCompare = !temp.xCompare;
temp.rightChild = node;
break;
}
} else { // if (com < 0)
if (temp.leftChild != null) {
temp = temp.leftChild;
} else {
node.xCompare = !temp.xCompare;
temp.leftChild = node;
break;
}
}
}
}
}
}
public boolean contains(Point2D p) {
if (p == null) {
throw new IllegalArgumentException();
} else {
if (root.isEmpty()) {
return false;
} else {
Node temp = root;
Node node = new Node(p);
while (true) {
byte com = temp.compareTo(node);
if (com == 0 && temp.point.equals(node.point)) return true;
if (com >= 0) {
if (temp.rightChild != null) {
temp = temp.rightChild;
} else {
return false;
}
} else {
if (temp.leftChild != null) {
temp = temp.leftChild;
} else {
return false;
}
}
}
}
}
}
public void draw() {
if (root == null || root.point == null) return;
Queue<Node> nodes = new LinkedList<Node>();
nodes.add(root);
while (!nodes.isEmpty()) {
Node node = nodes.poll();
node.point.draw();
if (node.leftChild != null && node.leftChild.point != null) nodes.add(node.leftChild);
if (node.rightChild != null && node.rightChild.point != null) nodes.add(node.rightChild);
}
}
public Iterable<Point2D> range(RectHV rect) {
if (rect == null) throw new IllegalArgumentException();
else {
if (root == null || root.point == null) return null;
double xMin = rect.xmin(), xMax = rect.xmax(), yMin = rect.ymin(), yMax = rect.ymax();
Queue<Node> nodes = new LinkedList<Node>();
Queue<Point2D> ret = new LinkedList<Point2D>();
nodes.add(root);
while (!nodes.isEmpty()) {
Node node = nodes.poll();
if (rect.contains(node.point)) ret.add(node.point);
if (node.xCompare) {
double x = node.point.x();
if (xMin <= x && node.leftChild != null && node.leftChild.point != null) nodes.add(node.leftChild);
if (xMax >= x && node.rightChild != null && node.rightChild.point != null) nodes.add(node.rightChild);
} else {
double y = node.point.y();
if (yMin <= y && node.leftChild != null && node.leftChild.point != null) nodes.add(node.leftChild);
if (yMax >= y && node.rightChild != null && node.rightChild.point != null) nodes.add(node.rightChild);
}
}
return ret;
}
}
public Point2D nearest(Point2D query) {
if (query == null) {
throw new IllegalArgumentException();
} else {
if (root == null || root.point == null) return null;
Point2D ret = null;
double min = Double.POSITIVE_INFINITY;
double temp1 = 0, xQuery = query.x(), yQuery = query.y();
Stack<Recur> stak = new Stack<Recur>();
stak.push(new Recur(root));
while(!stak.isEmpty()) {
Recur elem = stak.pop();
if (!elem.processedOnce) {
temp1 = query.distanceSquaredTo(elem.node.point);
if (temp1 < min) {
min = temp1;
ret = elem.node.point;
}
stak.push(elem);
elem.gap = (elem.node.xCompare ? xQuery : yQuery) - elem.node.coord();
if (elem.gap < 0) {
if (elem.node.leftChild != null && elem.node.leftChild.point != null) stak.push(new Recur(elem.node.leftChild));
elem.processedOnce = true;
} else {
if (elem.node.rightChild != null && elem.node.rightChild.point != null) stak.push(new Recur(elem.node.rightChild));
elem.processedOnce = elem.isRight = true;
}
} else {
if (elem.isRight) {
if (min > (elem.gap * elem.gap) && elem.node.leftChild != null && elem.node.leftChild.point != null) {
stak.push(new Recur(elem.node.leftChild));
}
} else {
if (min > (elem.gap * elem.gap) && elem.node.rightChild != null && elem.node.rightChild.point != null) {
stak.push(new Recur(elem.node.rightChild));
}
}
}
}
return ret;
}
}
}