-
Notifications
You must be signed in to change notification settings - Fork 0
/
KdTree.java
195 lines (172 loc) · 5.83 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
189
190
191
192
193
194
195
import edu.princeton.cs.algs4.Point2D;
import edu.princeton.cs.algs4.RectHV;
import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.StdDraw;
public class KdTree {
private Node root;
private int size = 0;
private int tempLevel = 0;
private class Node {
private Point2D key;
private int level;
private Node left, right;
public Node(Point2D key, int level) {
this.key = key;
this.level = level;
}
}
// construct an empty set of points
public KdTree()
{}
// 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 IllegalArgumentException();
if (contains(p)) return;
root = insert(root, p);
}
private Node insert(Node x, Point2D p) {
if (x == null) {
size = size + 1;
int level = tempLevel + 1;
tempLevel = 0;
return new Node(p, level);
}
double cmp;
if (x.level % 2 != 0) {
cmp = p.x() - x.key.x();
} else {
cmp = p.y() - x.key.y();
}
if (cmp < 0.0) {
if (x.left == null) {
tempLevel = x.level;
}
x.left = insert(x.left, p);
} else {
if (x.right == null) {
tempLevel = x.level;
}
x.right = insert(x.right, p);
}
return x;
}
// does the set contain point p?
public boolean contains(Point2D p)
{
if (p == null) throw new IllegalArgumentException();
return contains(root, p);
}
private boolean contains(Node x, Point2D p) {
if (x == null) {
return false;
}
if (x.key.equals(p)) {
return true;
}
double cmp;
if (x.level % 2 != 0) {
cmp = p.x() - x.key.x();
} else {
cmp = p.y() - x.key.y();
}
if (cmp < 0.0) {
return contains(x.left, p);
} else {
return contains(x.right, p);
}
}
// draw all points to standard draw
public void draw()
{
Node temp = root;
draw(temp, true, false, 1.0, 1.0);
}
private void draw(Node x, boolean isRoot, boolean isSmall, double xMax, double yMax) {
if (x == null) {
return;
}
double xCoordinate = x.key.x();
double yCoordinate = x.key.y();
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.filledCircle(xCoordinate, yCoordinate, 0.008);
if (x.level % 2 == 0) {
StdDraw.setPenColor(StdDraw.BLUE);
if (isRoot) {
// do nothing
} else if (isSmall) {
StdDraw.line(xCoordinate, yCoordinate, 0.0, yCoordinate);
StdDraw.line(xCoordinate, yCoordinate, xMax, yCoordinate);
} else {
double xMin = xMax;
StdDraw.line(xCoordinate, yCoordinate, 1.0, yCoordinate);
StdDraw.line(xCoordinate, yCoordinate, xMin, yCoordinate);
}
} else {
StdDraw.setPenColor(StdDraw.RED);
if (isRoot) {
StdDraw.line(xCoordinate, yCoordinate, xCoordinate, 0.0);
StdDraw.line(xCoordinate, yCoordinate, xCoordinate, 1.0);
} else if (isSmall) {
StdDraw.line(xCoordinate, yCoordinate, xCoordinate, yMax);
StdDraw.line(xCoordinate, yCoordinate, xCoordinate, 0.0);
} else {
double yMin = yMax;
StdDraw.line(xCoordinate, yCoordinate, xCoordinate, 1.0);
StdDraw.line(xCoordinate, yCoordinate, xCoordinate, yMin);
}
}
draw(x.left, false, true, xCoordinate, yCoordinate);
draw(x.right, false, false, xCoordinate, yCoordinate);
}
// all points that are inside the rectangle (or on the boundary)
public Iterable<Point2D> range(RectHV rect)
{
if (rect == null) throw new IllegalArgumentException();
return range(rect, root, Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY);
}
private Iterable<Point2D> range(RectHV rect, Node x, double xMin, double yMin, double xMax, double yMax) {
if (isEmpty()) return null;
RectHV that = new RectHV(xMin, yMin, xMax, yMax);
if (!rect.intersects(that)) return null;
range(rect, x.left, Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY, x.key.x(), x.key.y());
range(rect, x.right, x.key.x(), x.key.y(), Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY);
return null;
}
// 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 IllegalArgumentException();
return nearest(p, root, Double.POSITIVE_INFINITY);
}
private Point2D nearest(Point2D p, Node x, double minDistance) {
if (isEmpty()) return null;
if (x == null) return null;
if (x.left != null) {
double distanceL = p.distanceTo(x.left.key);
if (distanceL < minDistance) {
return nearest(p, x.left, distanceL);
}
}
if (x.right != null) {
double distanceR = p.distanceTo(x.right.key);
if (distanceR < minDistance) {
return nearest(p, x.right, distanceR);
}
}
return x.key;
}
// unit testing of the methods (optional)
public static void main(String[] args)
{}
}