-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRtree.java
137 lines (105 loc) · 4.29 KB
/
Rtree.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
package rtree;
import java.util.ArrayList;
import java.util.List;
import rtree.Constants;
public class RTree {
private RTNode root;
private int tree_type;
private int nodeCapacity = -1;
private float fillFactor = -1;
private int dimension;
public RTree(int capacity, float fillFactor, int type, int dimension) {
this.fillFactor = fillFactor;
tree_type = type;
nodeCapacity = capacity;
this.dimension = dimension;
root = new RTDataNode(this, Constants.NULL);
}
public int getDimension() {
return dimension;
}
public void setRoot(RTNode root) {
this.root = root;
}
public float getFillFactor() {
return fillFactor;
}
public int getNodeCapacity() {
return nodeCapacity;
}
public int getTreeType() {
return tree_type;
}
public boolean insert(Rectangle rectangle) {
if (rectangle == null)
throw new IllegalArgumentException("Rectangle cannot be null.");
if (rectangle.getHigh().getDimension() != getDimension())
{
throw new IllegalArgumentException("Rectangle dimension different than RTree dimension.");
}
RTDataNode leaf = root.chooseLeaf(rectangle);
return leaf.insert(rectangle);
}
public int delete(Rectangle rectangle) {
if (rectangle == null) {
throw new IllegalArgumentException("Rectangle cannot be null.");
}
if (rectangle.getHigh().getDimension() != getDimension()) {
throw new IllegalArgumentException("Rectangle dimension different than RTree dimension.");
}
RTDataNode leaf = root.findLeaf(rectangle);
if (leaf != null) {
return leaf.delete(rectangle);
}
return -1;
}
public List<RTNode> traversePostOrder(RTNode root) {
if (root == null)
throw new IllegalArgumentException("Node cannot be null.");
List<RTNode> list = new ArrayList<RTNode>();
list.add(root);
if (!root.isLeaf()) {
for (int i = 0; i < root.usedSpace; i++) {
List<RTNode> a = traversePostOrder(((RTDirNode) root).getChild(i));
for (int j = 0; j < a.size(); j++) {
list.add(a.get(j));
}
}
}
return list;
}
public static void main(String[] args) throws Exception {
RTree tree = new RTree(4, 0.4f, Constants.RTREE_QUADRATIC, 2);
float[] f = { 5, 30, 25, 35, 15, 38, 23, 50, 10, 23, 30, 28, 13, 10, 18, 15, 23, 10, 28, 20, 28, 30, 33, 40, 38,
13, 43, 30, 35, 37, 40, 43, 45, 8, 50, 50, 23, 55, 28, 70, 10, 65, 15, 70, 10, 58, 20, 63, };
for (int i = 0; i < f.length;) {
Point p1 = new Point(new float[] { f[i++], f[i++] });
Point p2 = new Point(new float[] { f[i++], f[i++] });
final Rectangle rectangle = new Rectangle(p1, p2);
tree.insert(rectangle);
Rectangle[] rectangles = tree.root.datas;
System.out.println("level:" + tree.root.level);
for (int j = 0; j < rectangles.length; j++)
System.out.println(rectangles[j]);
}
System.out.println("---------------------------------");
System.out.println("Insert finished.");
System.out.println("---------------------------------");
System.out.println("Begin delete.");
for (int i = 0; i < f.length;) {
Point p1 = new Point(new float[] { f[i++], f[i++] });
Point p2 = new Point(new float[] { f[i++], f[i++] });
final Rectangle rectangle = new Rectangle(p1, p2);
tree.delete(rectangle);
Rectangle[] rectangles = tree.root.datas;
System.out.println(tree.root.level);
for (int j = 0; j < rectangles.length; j++)
System.out.println(rectangles[j]);
}
System.out.println("---------------------------------");
System.out.println("Delete finished.");
Rectangle[] rectangles = tree.root.datas;
for (int i = 0; i < rectangles.length; i++)
System.out.println(rectangles[i]);
}
}