-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKdTree.java
197 lines (168 loc) · 5.88 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
196
197
package kdtree;
import edu.princeton.cs.algs4.*;
import java.util.ArrayList;
import java.util.List;
/**
* Created by tuanpa on 11/26/16.
*/
public class KdTree {
private int size;
private Node root;
private Point2D nearestPoint = null;
private Point2D pointParam = null;
private double nearestDistance = Double.POSITIVE_INFINITY;
private static final boolean VERTICAL = true;
private static class Node {
public Point2D p; // the point
public 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 point, RectHV rect) {
this.p = point;
this.rect = rect;
this.lb = null;
this.rt = null;
}
}
public KdTree() {
root = null;
size = 0;
}
// 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) {
root = insert(root, p, VERTICAL, 0, 0, 1, 1);
}
private Node insert(Node parent, Point2D point, boolean isVertical, double xmin, double ymin, double xmax, double ymax) {
boolean isLeftBottom;
if (parent == null) {
size++;
return new Node(point, new RectHV(xmin, ymin, xmax, ymax));
}
if (parent.p.equals(point)) {
return parent;
}
if (isVertical) {
isLeftBottom = parent.p.x() > point.x();
if (isLeftBottom) {
parent.lb = insert(parent.lb, point, !isVertical,
parent.rect.xmin(), parent.rect.ymin(), parent.p.x(), parent.rect.ymax());
} else {
parent.rt = insert(parent.rt, point, !isVertical,
parent.p.x(), parent.rect.ymin(), parent.rect.xmax(), parent.rect.ymax());
}
} else {
isLeftBottom = parent.p.y() > point.y();
if (isLeftBottom) {
parent.lb = insert(parent.lb, point, !isVertical,
parent.rect.xmin(), parent.rect.ymin(), parent.rect.xmax(), parent.p.y());
} else {
parent.rt = insert(parent.rt, point, !isVertical,
parent.rect.xmin(), parent.p.y(), parent.rect.xmax(), parent.rect.ymax());
}
}
return parent;
}
// does the set contain point p?
public boolean contains(Point2D point) {
return contains(root, point, VERTICAL);
}
private boolean contains(Node parent, Point2D point, boolean isVertical) {
if (parent == null) {
return false;
}
if (parent.p.equals(point)) {
return true;
}
boolean isLeftBottom = (isVertical && parent.p.x() > point.x())
|| (!isVertical && parent.p.y() > point.y());
if (isLeftBottom) {
return contains(parent.lb, point, !isVertical);
} else {
return contains(parent.rt, point, !isVertical);
}
}
// draw all points to standard draw
public void draw() {
draw(root, VERTICAL);
}
private void draw(Node node, boolean direction) {
if (direction) {
StdDraw.setPenColor(StdDraw.RED);
StdDraw.line(node.p.x(), node.rect.ymin(), node.p.x(), node.rect.ymax());
} else {
StdDraw.setPenColor(StdDraw.BLUE);
StdDraw.line(node.rect.xmin(), node.p.y(), node.rect.xmax(), node.p.y());
}
if (node.lb != null) {
draw(node.lb, !direction);
}
if (node.rt != null) {
draw(node.rt, !direction);
}
StdDraw.setPenColor(StdDraw.BLACK);
node.p.draw();
}
// all points that are inside the rectangle
public Iterable<Point2D> range(RectHV rect) {
ArrayList<Point2D> listRange = new ArrayList<Point2D>();
range(root, rect, listRange);
return listRange;
}
private void range(Node node, RectHV rect, ArrayList<Point2D> listRange) {
if (node == null || !node.rect.intersects(rect)) {
return;
}
if (rect.contains(node.p)) {
listRange.add(node.p);
}
range(node.lb, rect, listRange);
range(node.rt, rect, listRange);
}
// a nearest neighbor in the set to point p; null if the set is empty
public Point2D nearest(Point2D point) {
nearestPoint = null;
nearestDistance = Double.POSITIVE_INFINITY;
pointParam = point;
return nearest(root);
}
private Point2D nearest(Node parent) {
if (parent == null) {
return null;
}
if (parent.rect.distanceTo(pointParam) >= nearestDistance) {
return null;
}
Node fisrtNode = parent.lb;
Node lastNode = parent.rt;
checkNearestPoint(parent.p);
if (fisrtNode != null && lastNode != null
&& fisrtNode.rect.distanceTo(pointParam) > lastNode.rect.distanceTo(pointParam)) {
fisrtNode = parent.rt;
lastNode = parent.lb;
}
Point2D nearestPointFirst = nearest(fisrtNode);
if (nearestPointFirst != null) {
checkNearestPoint(nearestPointFirst);
}
Point2D nearestPointLast = nearest(lastNode);
if (nearestPointLast != null) {
checkNearestPoint(nearestPointLast);
}
return nearestPoint;
}
private void checkNearestPoint(Point2D neighborPoint) {
double distanceFlag = pointParam.distanceTo(neighborPoint);
if (distanceFlag < nearestDistance) {
nearestPoint = neighborPoint;
nearestDistance = distanceFlag;
}
}
}