Repository navigation
Expand file tree
/
Copy pathKdTree2.java
More file actions
199 lines (171 loc) · 4.66 KB
/
Copy pathKdTree2.java
File metadata and controls
199 lines (171 loc) · 4.66 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
package kdtrees;
import java.util.ArrayList;
import edu.princeton.cs.algs4.Point2D;
import edu.princeton.cs.algs4.RectHV;
import edu.princeton.cs.algs4.StdDraw;
public class KdTree2 {
private PointNode root;
private Integer count;
private class PointNode{
private Point2D point;
private PointNode left;
private PointNode right;
private PointNode parent;
private boolean comAxis;
public PointNode(Point2D point, PointNode left, PointNode right,PointNode parent,boolean comAxis) {
this.point = point;
this.left = left;
this.right = right;
this.comAxis = comAxis;
this.parent = parent;
}
int nodeSize(){
int nodesize = 0;
if(point != null) {
if(null != left) {
nodesize += left.nodeSize();
}
if(null != right) {
nodesize += left.nodeSize();
}
}
return nodesize + 1;
}
}
// construct an empty set of points
public KdTree2() {
}
// is the set empty?
public boolean isEmpty() {
return null == root;
}
// number of points in the set
public int size() {
if(count == null && root != null){
count = root.nodeSize();
}
return count;
}
// add the point to the set (if it is not already in the set)
public void insert(Point2D p) {
root = insert(null,root,p);
}
private PointNode insert(PointNode parent,PointNode node,Point2D p){
if(node == null){
if(parent == null ){
return new PointNode(p,null,null,null,true);
}else{
return new PointNode(p,null,null,parent,!parent.comAxis);
}
}
if(node.comAxis){
int cmpX = p.x() < node.point.x() ? -1 : p.x() > node.point.x() ? 1 : 0;
if(cmpX == -1){
node.left = insert(node,node.left,p);
}else{
node.right = insert(node,node.right,p);
}
}else{
int cmpY = p.y() < node.point.y() ? -1 : p.y() > node.point.y() ? 1 : 0;
if(cmpY == -1){
node.left = insert(node,node.left,p);
}else{
node.right = insert(node,node.right,p);
}
}
return node;
}
// does the set contain point p?
public boolean contains(Point2D p) {
if(null == root) return false;
return contains(root,p);
}
private boolean contains(PointNode node,Point2D p){
if(p == null) throw new NullPointerException();
if(node.point.compareTo(p) == 0) return true;
if( node.comAxis) {
int cmpX = p.x() < node.point.x() ? -1 : p.x() > node.point.x() ? 1 : 0;
if(cmpX == -1){
if(null == node.left) return false;
return contains(node.left,p);
}else{
if(null == node.right) return false;
return contains(node.right,p);
}
}else{
int cmpY = p.y() < node.point.y() ? -1 : p.y() > node.point.y() ? 1 : 0;
if(cmpY == -1){
if(null == node.left) return false;
return contains(node.left,p);
}else{
if(null == node.right) return false;
return contains(node.right,p);
}
}
}
// draw all points to standard draw
public void draw() {
//设置颜色 StdDraw.setPenColor(StdDraw.RED);
//画线 drawTo(Point2D that)
draw(null,root);
}
private void draw(PointNode parent,PointNode p){
if(null == parent){
if(null == p) { return; }
StdDraw.setPenColor(StdDraw.BLACK);
p.point.draw();
StdDraw.setPenColor(StdDraw.RED);
new Point2D(p.point.x(),0).drawTo(new Point2D(p.point.x(),1.0));
if(p.left != null){
draw(p,p.left);
}
if(p.right != null){
draw(p,p.right);
}
} else{
if(parent.comAxis){
StdDraw.setPenColor(StdDraw.BLUE);
if(parent.point.x() > p.point.x()){
new Point2D(0,p.point.y()).drawTo(new Point2D(parent.point.x(),p.point.y()));
}else{
new Point2D(parent.point.x(),p.point.y()).drawTo(new Point2D(1.0,p.point.y()));
}
}else{
StdDraw.setPenColor(StdDraw.RED);
if(parent.point.y() > p.point.y()){
new Point2D(p.point.x(),0).drawTo(new Point2D(p.point.x(),parent.point.y()));
}else{
new Point2D(p.point.x(),parent.point.y()).drawTo(new Point2D(p.point.x(),1.0));
}
}
if(p.left != null){
draw(p,p.left);
}
if(p.right != null){
draw(p,p.right);
}
}
}
// all points that are inside the rectangle
public Iterable<Point2D> range(RectHV rect) {
ArrayList<Point2D> innerPoint = new ArrayList<Point2D>();
return null;
}
// a nearest neighbor in the set to point p; null if the set is empty
public Point2D nearest(Point2D p) {
return null;
}
// unit testing of the methods (optional)
public static void main(String[] args) {
KdTree2 kt = new KdTree2();
kt.insert(new Point2D(0.7,0.2));
kt.insert(new Point2D(0.5,0.4));
kt.insert(new Point2D(0.2,0.3));
kt.insert(new Point2D(0.4,0.7));
kt.insert(new Point2D(0.9,0.6));
kt.insert(new Point2D(0.5,0.9));
kt.draw();
System.out.println(kt.contains(new Point2D(0.3,0.9)));
System.out.println("hello");
}
}