-
Notifications
You must be signed in to change notification settings - Fork 0
/
KdTree.java
188 lines (169 loc) · 6.44 KB
/
KdTree.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
import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.Point2D;
import edu.princeton.cs.algs4.RectHV;
import edu.princeton.cs.algs4.Stack;
import edu.princeton.cs.algs4.StdDraw;
public class KdTree {
private int size;
private Node root;
private Node champion;
private double minDistSquared;
private static class Node {
private final Point2D p; // the point
private final 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;
}
}
public KdTree() {
}
// construct an empty set of points
public boolean isEmpty() {
return size == 0;
} // is the set empty?
public int size() {
return size;
} // number of points in the set
public void insert(Point2D p) {
if (p == null) throw new IllegalArgumentException();
root = put(root, p, false, 0, 0, 1, 1);
} // add the point to the set (if it is not already in the set)
private Node put(Node node, Point2D p, boolean horizontal, double x1, double y1, double x2, double y2) {
boolean less;
if (node == null) {
size++;
return new Node(p, new RectHV(x1, y1, x2, y2));
}
// 4 cases
if (p.equals(node.p)) return node;
if (horizontal) {
less = p.y() < node.p.y();
if (less) node.lb = put(node.lb, p, !horizontal, x1, y1, x2, node.p.y()); // down
else node.rt = put(node.rt, p, !horizontal, x1, node.p.y(), x2, y2);
} else {
less = p.x() < node.p.x();
if (less) node.lb = put(node.lb, p, !horizontal, x1, y1, node.p.x(), y2); // left
else node.rt = put(node.rt, p, !horizontal, node.p.x(), y1, x2, y2); // right
}
return node;
}
public boolean contains(Point2D p) {
if (p == null) throw new IllegalArgumentException();
return get(root, p, false);
} // does the set contain point p?
private boolean get(Node x, Point2D p, boolean horizontal) {
if (x == null) return false;
if (p.equals(x.p)) return true;
boolean less;
if (horizontal) less = p.y() < x.p.y();
else less = p.x() < x.p.x();
if (less) return get(x.lb, p, !horizontal);
else return get(x.rt, p, !horizontal);
}
public void draw() {
draw(root, false);
} // draw all points to standard draw
private void draw(Node x, boolean horizontal) {
if (x == null) return;
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.setPenRadius(0.01);
x.p.draw();
if (horizontal) {
StdDraw.setPenColor(StdDraw.GREEN);
StdDraw.setPenRadius();
StdDraw.line(x.rect.xmin(), x.p.y(), x.rect.xmax(), x.p.y());
} else {
StdDraw.setPenColor(StdDraw.RED);
StdDraw.setPenRadius();
StdDraw.line(x.p.x(), x.rect.ymin(), x.p.x(), x.rect.ymax());
}
draw(x.lb, !horizontal);
draw(x.rt, !horizontal);
}
public Iterable<Point2D> range(RectHV rect) {
if (rect == null) throw new IllegalArgumentException();
Stack<Point2D> pts = new Stack<>();
Stack<Node> stack = new Stack<>();
stack.push(root);
while (!stack.isEmpty()) {
Node node = stack.pop();
if (node == null) continue;
if (rect.intersects(node.rect)) {
if (rect.contains(node.p)) {
pts.push(node.p);
}
stack.push(node.lb);
stack.push(node.rt);
}
}
return pts;
} // all points that are inside the rectangle (or on the boundary)
public Point2D nearest(Point2D p) {
if (p == null) throw new IllegalArgumentException();
if (isEmpty()) return null;
champion = null;
minDistSquared = Double.POSITIVE_INFINITY;
search(root, p, false);
return champion.p;
} // a nearest neighbor in the set to point p; null if the set is empty
private void search(Node node, Point2D p, boolean horizontal) {
if (node == null) return;
double dist = p.distanceSquaredTo(node.p);
if (dist < minDistSquared) {
minDistSquared = dist;
champion = node;
}
boolean flag;
if (horizontal) flag = p.y() < node.p.y();
else flag = p.x() < node.p.x();
if (flag) {
// search left / bottom first
search(node.lb, p, !horizontal);
if (node.rt == null) return;
if (minDistSquared > node.rt.rect.distanceSquaredTo(p)) // search only when possible
search(node.rt, p, !horizontal);
} else {
// search right / top first
search(node.rt, p, !horizontal);
if (node.lb == null) return;
if (minDistSquared > node.lb.rect.distanceSquaredTo(p)) // search only when possible
search(node.lb, p, !horizontal);
}
}
public static void main(String[] args) {
if (args.length > 0) {
String filename = args[0];
In in = new In(filename);
KdTree kdtree = new KdTree();
while (!in.isEmpty()) {
double x = in.readDouble();
double y = in.readDouble();
Point2D p = new Point2D(x, y);
kdtree.insert(p);
}
Point2D q = kdtree.nearest(new Point2D(0.27, 0.28));
System.out.println("----");
System.out.println(q);
}
// Point2D p1 = new Point2D(0.7, 0.2);
// Point2D p2 = new Point2D(0.5, 0.4);
// Point2D p3 = new Point2D(0.2, 0.3);
// Point2D p4 = new Point2D(0.4, 0.7);
// Point2D p5 = new Point2D(0.9, 0.6);
// KdTree tree = new KdTree();
// tree.insert(p1);
// tree.insert(p2);
// tree.insert(p3);
// tree.insert(p4);
// tree.insert(p5);
// System.out.println(tree.size());
// Point2D query = new Point2D(0.272, 0.466);
// StdDraw.setPenRadius(0.02);
// query.draw();
// tree.draw();
// System.out.println(tree.nearest(query));
}
}