-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2DRangeTree.java
More file actions
363 lines (313 loc) · 9.91 KB
/
Copy path2DRangeTree.java
File metadata and controls
363 lines (313 loc) · 9.91 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
import java.util.ArrayList;
import java.util.List;
public class RangeTree {
Node root;
List<Node> leaves;
int[] xPoints;
int[] yPoints;
public RangeTree(int[] xs, int[] ys){
leaves = new ArrayList<>();
this.xPoints = xs;
this.yPoints = ys;
checkArraySize();
createLeaves(xPoints, yPoints);
SubTree.subtreePoints = false;
constructTree(leaves);
addLinksToLeafs();
}
public void createLeaves(int[] xs, int [] ys){
for (int i = 0; i < xs.length; i++){
leaves.add(new Node(xs[i], xs[i], ys[i]));
}
}
public void constructTree(List<Node> leaves){
if (leaves.size() == 1){
root = leaves.get(0);
return;
}
List<Node> nodeList = constructNodes(leaves);
constructSubtree(nodeList);
constructTree(nodeList);
}
public List<Node> constructNodes(List<Node> list){
List<Node> nodeList = new ArrayList<>();
for (int i = 0; i < list.size() - 1; i += 2){
int value = getValue(list, i);
Node node = new Node(value);
node.left = list.get(i);
node.right = list.get(i+1);
nodeList.add(node);
}
return nodeList;
}
public void constructSubtree(List<Node> list){
int size = xPoints.length / list.size();
int index = 0;
for (Node node : list) {
int[] xs = new int[size], ys = new int[size];
index = arrayCopy(xs, ys, xPoints, yPoints,index);
getSubtree(node, xs, ys);
}
}
public void getSubtree(Node node, int[] xs, int[] ys){
if (!SubTree.hasSubtree) {
SubTree.hasSubtree = true;
node.subtree = new RangeTree(ys, xs);
SubTree.hasSubtree = false;
}
}
public List<Node> twoDimRangeSearch(int x1, int x2, int y1, int y2){
List<Node> foundPoints = new ArrayList<>(); // contains all the points with a given range
List<Node> leftSequence = getNodeSequence(x1);
List<Node> rightSequence = getNodeSequence(x2);
Node leftBoundaryNode = leftSequence.get(leftSequence.size()-1);
Node rightBoundaryNode = rightSequence.get(rightSequence.size()-1);
search(leftSequence, rightSequence, x1, x2, y1, y2, foundPoints);
if (checkBounds(leftBoundaryNode, x1,x2,y1,y2)){
foundPoints.add(leftBoundaryNode);
}
if (checkBounds(rightBoundaryNode, x1,x2,y1,y2)){
foundPoints.add(rightBoundaryNode);
}
return foundPoints;
}
public void search(List<Node> leftSequence, List<Node> rightSequence, int x1, int x2, int y1, int y2, List<Node> points){
for (Node node : leftSequence) {
if (leftSequence.contains(node.left)) {
if (node.right.subtree != null) {
List<Node> result = rangeSearch(node.right.subtree, y1, y2);
points.addAll(result);
}else {
if (checkBounds(node.right, x1, x2, y1, y2)){
points.add(node.right);
}
}
}
}
for (Node node : rightSequence){
if (rightSequence.contains(node.right)) {
if (node.left.subtree != null) {
List<Node> result = rangeSearch(node.left.subtree, y1, y2);
points.addAll(result);
}else {
if (checkBounds(node.left, x1, x2, y1, y2)){
points.add(node.left);
}
}
}
}
}
public List<Node> getNodeSequence(int value){
Node node = root;
List<Node> nodeSequence = new ArrayList<>();
while (node.left != null && node.right != null){
if (value > node.value){
node = node.right;
}else{
node = node.left;
}
nodeSequence.add(node);
}
return nodeSequence;
}
public List<Node> rangeSearch(RangeTree tree, int begin, int end){
Node node = tree.root;
List<Node> leaves = new ArrayList<>();
while (node.right != null && node.left != null){
if (begin > node.value){
node = node.right;
}else {
node = node.left;
}
}
if (node.next == null && node.value < begin){
return leaves;
}
if (node.value < begin){
node = node.next;
}
while (node.value <= end){
leaves.add(node);
node = node.next;
if (node == null)break;
}
return leaves;
}
public boolean checkBounds(Node node, int x1, int x2, int y1, int y2){
return (node.point.xCoordinate >= x1) && (node.point.xCoordinate <= x2) && (node.point.yCoordinate >= y1) && (node.point.yCoordinate <= y2);
}
public int arrayCopy(int[] xs, int[] ys, int[] xPoints, int[] yPoints,int index){
for (int i = 0; i < xs.length; i++){
xs[i] = xPoints[index];
ys[i] = yPoints[index];
index++;
}
return index;
}
public void addLinksToLeafs(){
Node node = leaves.get(0);
node.next = leaves.get(1);
for (int i = 1; i < leaves.size()-1; i++){
node = leaves.get(i);
node.next = leaves.get(i+1);
node.prev = leaves.get(i-1);
}
node = leaves.get(leaves.size()-1);
node.prev = leaves.get(leaves.size()-2);
}
public int getValue(List<Node> list, int index){
return list.get(index+1).value == 0 ? list.get(index).value : ( list.get(index).value + list.get(index+1).value)/2;
}
public void checkArraySize(){
if (!isPowerOfTwo(xPoints.length)){
xPoints = changeArray(xPoints);
yPoints = changeArray(yPoints);
}
Array.sort(xPoints, yPoints);
}
public boolean isPowerOfTwo(int n){
while (n != 1){
if (n % 2 != 0){
return false;
}
n = n / 2;
}
return true;
}
public int[] changeArray(int[] array){
int[] newArray = new int[(getPower(array.length))];
for (int i = 0; i < array.length; i++){
newArray[i] = array[i];
}
return newArray;
}
public int getPower(int N){
int i = 1;
while (Math.pow(2,i) < N){
i++;
}
return (int) Math.pow(2,i);
}
public void printLinkedList(){
Node node = root;
while (node.left != null){
node = node.left;
}
System.out.println("Doubly LinkedList: ");
while (node != null){
System.out.print("("+node.point.xCoordinate + " " + node.point.yCoordinate + ") ");
node = node.next;
}
System.out.println();
}
public void traverse(Node node){
if (node == null){
return;
}
System.out.print(node.value + " ");
traverse(node.left);
traverse(node.right);
}
}
class Node{
Node left;
Node right;
Node prev;
Node next;
int value;
RangeTree subtree;
Point point;
public Node(int value, int x, int y){
this.value = value;
left = right = null;
prev = next = null;
subtree = null;
point = new Point(x,y);
}
public Node(int value){
this.value = value;
left = right = null;
prev = next = null;
subtree = null;
point = null;
}
@Override
public String toString() {
return "Node{" +
"value=" + value +
", coordinates: X=" + point.xCoordinate + " Y=" + point.yCoordinate +
'}';
}
}
class Point{
int xCoordinate;
int yCoordinate;
public Point(int x, int y) {
if (SubTree.subtreePoints) {
this.xCoordinate = x;
this.yCoordinate = y;
}else {
this.xCoordinate = y;
this.yCoordinate = x;
}
}
}
class SubTree{
static boolean hasSubtree = false;
static boolean subtreePoints = true;
}
class Array{
public static void sort(int[] xs, int[] ys){
mergeSort(xs, ys, 0, xs.length-1);
}
public static void mergeSort(int[] xs, int[] ys, int left, int right){
if (left < right) {
int middle = (left + right) / 2;
mergeSort(xs, ys, left, middle);
mergeSort(xs, ys,middle + 1, right);
merge(xs, ys, left, middle, right);
}
}
public static void merge(int[] xs, int[] ys, int left, int middle, int right){
int size1 = middle - left + 1;
int size2 = right - middle;
int[] leftSubArray = new int[size1];
int[] rightSubArray = new int[size2];
int[] leftSubArray1 = new int[size1];
int[] rightSubArray1 = new int[size2];
for (int i = 0; i < size1; i++){
leftSubArray[i] = xs[left+i];
leftSubArray1[i] = ys[left+i];
}
for (int j = 0; j < size2; j++){
rightSubArray[j] = xs[middle + 1 + j];
rightSubArray1[j] = ys[middle + 1 + j];
}
int i = 0, j = 0;
int k = left;
while (i < size1 && j < size2){
if (leftSubArray[i] <= rightSubArray[j]){
xs[k] = leftSubArray[i];
ys[k] = leftSubArray1[i];
i++;
}else {
xs[k] = rightSubArray[j];
ys[k] = rightSubArray1[j];
j++;
}
k++;
}
while (i < size1){
xs[k] = leftSubArray[i];
ys[k] = leftSubArray1[i];
i++;
k++;
}
while (j < size2){
xs[k] = rightSubArray[j];
ys[k] = rightSubArray1[j];
j++;
k++;
}
}
}