-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRBTree.java
547 lines (487 loc) · 16 KB
/
RBTree.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
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
//Implementation of Red-Black Tree
public class RBTree {
min_Heap min ;
private final int RED = 0;
private final int BLACK = 1;
private static HashMap<Integer, Integer> ptr = new HashMap<Integer, Integer>();
ArrayList<Integer> printBuildingRange = new ArrayList<Integer>();
static HashMap<Integer,RBNode> RBNode = new HashMap<Integer,RBNode>();
risingCity risingCity= new risingCity();
class RBNode {
Storage element = new Storage(-1,-1,-1);
int color = BLACK;
RBNode left = nil, right = nil, parent = nil;
RBNode(Storage element) {
this.element = element;
}
}
//default nil node for the RB Tree
private final RBNode nil = new RBNode(new Storage(-1,-1,-1));
//initialize root as nil initially and continue
private RBNode root = nil;
private RBNode right_node = nil;
//Checks if the node exists or not
private RBNode node_exist(RBNode noins, RBNode node) {
if (noins.element.Building_Numbers > node.element.Building_Numbers && (node.right != nil))
{
return node_exist(noins, node.right);
}
else if (noins.element.Building_Numbers < node.element.Building_Numbers && (node.left != nil))
{
return node_exist(noins, node.left);
}
else
{
if (noins.element.Building_Numbers == node.element.Building_Numbers)
{
return node;
}
else if (root == nil)
{
return null;
}
}
return null;
}
public RBNode insertion(Storage item)
{
RBNode node = new RBNode(item);
RBNode temp = root;
insert(node, temp);
RB_insfixup(node);
RBNode.put(node.element.Building_Numbers, node);
return node;
}
//insertion of the node into the tree
public void insert(RBNode node, RBNode temp)
{
//check if the root is empty or not
if(root != nil)
{
node.color = RED;
while (true)
{
if (node.element.Building_Numbers >= temp.element.Building_Numbers)
{
if(temp.right != nil)
{
temp = temp.right;
}
else
{
temp.right = node;
node.parent = temp;
break;
}
}
else if (node.element.Building_Numbers < temp.element.Building_Numbers)
{
if(temp.left != nil)
{
temp = temp.left;
}
else
{
temp.left = node;
node.parent = temp;
break;
}
}
}
}
//Insert at root if tree is empty
else
{
root = node;
right_node = root;
node.color = BLACK;
node.parent = nil;
}
}
// Fixing the tree in order to maintain its properties
private void RB_insfixup(RBNode node)
{
// Here the insertion is done and the color given=red. If the parent node also red then property violated.
while (node.parent.color == RED)
{
RBNode lfnode = nil;
/* Two cases. When :
1. Parent is left child of it parent - Rotate Right
2. Parent is right child of its parent - Rotate Left
*/
if (node.parent != node.parent.parent.left)
{
lfnode = node.parent.parent.left;
if (lfnode != nil && lfnode.color == RED)
{
node.parent.color = BLACK;
lfnode.color = BLACK;
node.parent.parent.color = RED;
node = node.parent.parent;
continue;
}
else if (node == node.parent.left) {
node = node.parent;
RR(node);
}
node.parent.color = BLACK;
node.parent.parent.color = RED;
LR(node.parent.parent);
}
else
{
lfnode = node.parent.parent.right;
if (lfnode != nil && lfnode.color == RED)
{
node.parent.color = BLACK;
lfnode.color = BLACK;
node.parent.parent.color = RED;
node = node.parent.parent;
continue;
}
else if (node == node.parent.right)
{
node = node.parent;
LR(node);
}
node.parent.color = BLACK;
node.parent.parent.color = RED;
RR(node.parent.parent);
}
}
root.color = BLACK;
}
//left rotation of the tree for balancing
void LR(RBNode node)
{
if (node.parent == nil)
{
// The root node is rotated to the left
RBNode right = root.right;
root.right = right.left;
right.left.parent = root;
root.parent = right;
right.left = root;
right.parent = nil;
root = right;
}
else
{
if (node == node.parent.left)
{
node.parent.left = node.right;
}
else
{
node.parent.right = node.right;
}
node.right.parent = node.parent;
node.parent = node.right;
if (node.right.left != nil)
{
node.right.left.parent = node;
}
node.right = node.right.left;
node.parent.left = node;
}
}
//right rotation of the tree for balancing
void RR(RBNode node)
{
if (node.parent == nil)
{
//the root node is rotated to the right
RBNode left = root.left;
root.left = root.left.right;
left.right.parent = root;
root.parent = left;
left.right = root;
left.parent = nil;
root = left;
}
else
{
if (node == node.parent.left)
{
node.parent.left = node.left;
}
else
{
node.parent.right = node.left;
}
node.left.parent = node.parent;
node.parent = node.left;
if (node.left.right != nil)
{
node.left.right.parent = node;
}
node.left = node.left.right;
node.parent.right = node;
}
}
// for replacing one subtree as a child of its parent with another subtree.
void rbTransfer(RBNode u, RBNode v){
if(u.parent == nil){
root = v;
}else if(u == u.parent.left){
u.parent.left = v;
}else
u.parent.right = v;
v.parent = u.parent;
}
public RBNode findBuildingNum(int Building_num)
{
return findBuildingNum(root, Building_num);
}
private RBNode findBuildingNum(RBNode root, int Building_num)
{
while (root != nil)
{
int rootvalue = root.element.Building_Numbers;
if (Building_num < rootvalue) {
root = root.left;
}
else if (Building_num > rootvalue)
root = root.right;
else
{
return root;
}
return root;
}
return null;
}
boolean deleteNode(int building_num)
{
RBNode node = RBNode.get(building_num);
deleteNode(node);
RBNode.remove(building_num);
return true;
}
/* deleting a node in the tree*/
boolean deleteNode(RBNode fin)
{
if((fin = node_exist(fin, root))==null)
{
return false;
}
RBNode upfix;
RBNode rbdel = fin;
int initial_color = rbdel.color;
if(fin.left == nil)
{
upfix = fin.right;
rbTransfer(fin, fin.right);
}
else if(fin.right == nil)
{
upfix = fin.left;
rbTransfer(fin, fin.left);
}
else
{
rbdel = minNode(fin.right);
initial_color = rbdel.color;
upfix = rbdel.right;
if(rbdel.parent != fin)
{
rbTransfer(rbdel, rbdel.right);
rbdel.right = fin.right;
rbdel.right.parent = rbdel;
}
else
{
upfix.parent = rbdel;
}
rbTransfer(fin, rbdel);
rbdel.left = fin.left;
rbdel.left.parent = rbdel;
rbdel.color = fin.color;
}
if(initial_color==BLACK)
{
rb_DeletionFix(upfix);
}
return true;
}
/* method to fix colors and RB Tree properties after deletion*/
void rb_DeletionFix(RBNode upfix){
while(upfix!=root && upfix.color == BLACK){
if(upfix != upfix.parent.left)
{
RBNode fixing = upfix.parent.left;
if(fixing.color == RED)
{
fixing.color = BLACK;
upfix.parent.color = RED;
RR(upfix.parent);
fixing = upfix.parent.left;
}
else if(fixing.left.color == RED)
{
fixing.color = upfix.parent.color;
upfix.parent.color = BLACK;
fixing.left.color = BLACK;
RR(upfix.parent);
upfix = root;
}
else if(fixing.right.color == BLACK)
{
if(fixing.left.color == BLACK)
{
fixing.color = RED;
upfix = upfix.parent;
continue;
}
}
else if(fixing.left.color == BLACK)
{
fixing.right.color = BLACK;
fixing.color = RED;
LR(fixing);
fixing = upfix.parent.left;
}
}
else
{
RBNode fixing = upfix.parent.right;
if(fixing.color == RED){
fixing.color = BLACK;
upfix.parent.color = RED;
LR(upfix.parent);
fixing = upfix.parent.right;
}
else if(fixing.left.color == BLACK )
{
if(fixing.right.color == BLACK)
{
fixing.color = RED;
upfix = upfix.parent;
continue;
}
}
else if(fixing.right.color == BLACK)
{
fixing.left.color = BLACK;
fixing.color = RED;
RR(fixing);
fixing = upfix.parent.right;
}
else if(fixing.right.color == RED)
{
fixing.color = upfix.parent.color;
upfix.parent.color = BLACK;
fixing.right.color = BLACK;
LR(upfix.parent);
upfix = root;
}
}
}
upfix.color = BLACK;
}
/*Finding the minimum element at the subtree root*/
RBNode minNode(RBNode subTreeRoot){
while(subTreeRoot.left!=nil){
subTreeRoot = subTreeRoot.left;
}
return subTreeRoot;
}
/* Method to search for an element in the Red Black Tree and for checking if there is a node with the given id */
private void Search(RBNode r)
{
if (r != nil)
{
char color = 'B';
if (r.color == 0)
color = 'R';
System.out.print(r.element.Building_Numbers +""+color+" ");
Search(r.left);
Search(r.right);
}
}
/*Printing the building number given, its total time and executed time*/
public void printBuilding (int BuildingNum)
{
if(ptr.containsKey(BuildingNum)) {
int position = ptr.get(BuildingNum);
min = risingCity.getHeapObj();
Storage node = min.fetchNode(position);
System.out.println("(" + node.getBuildingNums() + "," + node.getExecuteTime() + "," + node.getTotal_time() + ")");
}else {
System.out.println("("+0+","+0+","+0+")");
}
}
/*Printing the building numbers between the range */
public void printBuildRange(int building_num1, int building_num2)
{
printBuildRange(building_num1, building_num2, root);
int j = 1;
Collections.sort(printBuildingRange);
if(printBuildingRange.size() != 0) {
for(int i = 0; i < printBuildingRange.size() ; i++) {int heapIndex = 0;
try {
heapIndex = ptr.get(printBuildingRange.get(i));
}catch(Exception e)
{
// print();
Search(root);
System.out.println("error: " + printBuildingRange.get(i));
}
min = risingCity.getHeapObj();
Storage node = min.fetchNode(heapIndex);
if(j ==1) {
System.out.print("(" + node.getBuildingNums() + "," + node.getExecuteTime() + "," + node.getTotal_time() + ")");
j = 0;
}else {
System.out.print("," + "(" + node.getBuildingNums() + "," + node.getExecuteTime() + "," + node.getTotal_time() + ")");
}
}
printBuildingRange.clear();
}else {
System.out.print("("+0+","+0+","+0+")");
}
}
/*Printing the building numbers between the range */
private void printBuildRange(int building_num1, int building_num2, RBNode r)
{
if(r.element.Building_Numbers >= building_num1 && r.element.Building_Numbers <= building_num2)
{
printBuildingRange.add(r.element.Building_Numbers);
}
if(r.left != null && r.left.element.Building_Numbers >= building_num1)
{
printBuildRange(building_num1, building_num2, r.left);
}
if(r.right != null && r.right.element.Building_Numbers <= building_num2 )
{
printBuildRange(building_num1, building_num2, r.right);
}
}
/*for setting pointer*/
public void setptr(int buildingnum, int position)
{
ptr.put(buildingnum, position);
}
/*deleting pointer*/
public void deleteFromptr(int buildingnum)
{
if(!ptr.isEmpty())
{
ptr.remove(buildingnum);
}
}
/*getting value from pointer*/
public int getValueFromptr(int buildingNum) {
return ptr.get(buildingNum);
}
}