-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredBlackTrees.java
718 lines (591 loc) · 16.6 KB
/
redBlackTrees.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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
class Node{
// Red Black Tree Visualisation Link : https://www.cs.usfca.edu/~galles/visualization/RedBlack.html
public Node()
{
this.left = null;
this.right = null;
this.parent = null;
}
int key;
Node left;
Node right;
Node parent; // parent
int color; // 0 for RED, 1 for BLACK
}
public class redBlackTrees{
Node root = new Node();
public redBlackTrees()
{
this.root = null;
}
//**************************************************** Rotations ***************************************************************
// LEFT ROTATION
void leftRotate(redBlackTrees t, Node x)
{
Node y;
y = x.right; // set y
x.right = y.left; // right of x would point to left subtree of y
if(y.left != null)
y.left.parent = x;
y.parent = x.parent ; // link x's parent to y
if(x.parent == null)
t.root = y;
else if(x == x.parent.left)
x.parent.left = y;
else
x.parent.right = y;
y.left = x; // put x on y's left
x.parent = y;
}
// RIGHT ROTATION
void rightRotate(redBlackTrees t, Node y)
{
Node x;
x = y.left;
y.left = x.right;
if(x.right != null)
x.right.parent = y;
x.parent = y.parent;
if(y.parent == null)
t.root = x;
else if(y == y.parent.left)
y.parent.left = x;
else
y.parent.right = x;
x.right = y;
y.parent = x;
}
// *************************************************************** INSERTION *********************************************************
void insert(redBlackTrees t, Node z)
{
Node y = null;
Node x = t.root;
while(x != null)
{
y = x ;
if(z.key > x.key)
x = x.right;
else
x = x.left;
}
z.parent = y ;
if(y == null)
t.root = z;
else if( z.key > y.key )
y.right = z;
else
y.left = z;
z.left = null;
z.right = null;
z.color = 0; // coloring newly inserted node red (safe color)
solveDoubleRed(t, z); // calling the insert fixup method to balance the red black tree
}
void solveDoubleRed(redBlackTrees t, Node z)
{
Node y;
while((z.parent != null)&&(z.parent.color == 0)) // double red problem
{
if(z.parent == z.parent.parent.left) // z's parent is in left subtree
{
y = z.parent.parent.right; // uncle of z and is right child of z's grandparent
if((y != null) && (y.color == 0)) // CASE 1: Uncle is red
{
z.parent.color = 1; // z's parent black
y.color = 1 ; // setting uncle's color black
z.parent.parent.color = 0; // setting grandparent's color black
z = z.parent.parent;
}
else // CASE 2: Uncle is black and z is right child of it's parent
{
if(z == z.parent.right)
{
z = z.parent;
leftRotate(t, z);
}
if(z.parent != null)
{
z.parent.color = 1; // coloring the parent black // CASE 3: Uncle is black and z is left child of it's parent
if(z.parent.parent != null)
{
z.parent.parent.color = 0; // coloring the grandparent red
}
}
if(z.parent != null && z.parent.parent != null)
{
rightRotate(t, z.parent.parent);
}
}
}
else // SYMMETRIC CASE : z.parent = z.parent.parent.right
{
y = z.parent.parent.left;
if( (y!=null) && (y.color == 0)) // CASE 1: Uncle is red
{
z.parent.color = 1;
y.color = 1;
z.parent.parent.color = 0;
z = z.parent.parent;
}
else // CASE 2: Uncle is black and z is left child of it's parent
{
if(z == z.parent.left)
{
z = z.parent;
rightRotate(t, z);
}
if( z.parent != null)
{
z.parent.color = 1; // CASE 3: Uncle is black and z is right child of it's parent
if(z.parent.parent != null)
{
z.parent.parent.color = 0;
}
}
if((z.parent != null) && (z.parent.parent != null))
{
leftRotate(t, z.parent.parent);
}
}
}
}
t.root.color = 1; // Coloring the root black
}
// **********************************************************************************************************************************
// **************************************************** DELETION ********************************************************************
// Replaces the subtree rooted at u by subtree rooted at v
void replaceSubTree(redBlackTrees t, Node u, Node v)
{
if(u.parent == null)
t.root = v ;
else if(u == u.parent.left)
u.parent.left = v;
else
u.parent.right = v;
if(v != null)
v.parent = u.parent;
}
Node findMinimum(redBlackTrees t, Node x)
{
while( x.left != null)
{
x = x.left ;
}
return x;
}
Node findMaximum(redBlackTrees t, Node x)
{
while( x.right != null)
{
x = x.right ;
}
return x;
}
void delete(redBlackTrees t, int key)
{
// searching the node in the tree]
Node z = new Node();
Node temp = t.root;
while(temp != null)
{
if(key == temp.key)
{
z = temp;
break;
}
else
{
if(key > temp.key)
{
temp = temp.right;
}
else
{
temp = temp.left;
}
}
}
System.out.println("After Search: z: " + z.key + " z's lc: "+ z.left.key+ " z's rc: " + z.right.key);
Node y = z ;
Node x;
int yOriginalColor = y.color; // Noting down the original color
if(z.left == null) // with only one child i.e. right child
{
x = z.right;
replaceSubTree(t, z, z.right); // replacement with left child
}
else if(z.right == null) // with only one child i.e. left child
{
x = z.left;
replaceSubTree(t, z, z.left); // replacement with left child
}
else
{
//y = findMinimum(t, z.right); // finding out the inorder successor of z
y = findMaximum(t, z.left); // finding out the inorder predecessor of z
System.out.println("Inorder succ: "+ y.key);
yOriginalColor = y.color;
//x = y.right;
x = y.left;
if((y.parent == z) && (x != null))
x.parent = y;
else
{
//replaceSubTree(t, y, y.right);
replaceSubTree(t, y, y.left);
/*y.right = z.right;
y.right.parent = y;*/
y.left = z.left;
y.left.parent = y;
}
replaceSubTree(t, z, y);
/*y.left = z.left;
y.left.parent = y;*/
y.right = z.right;
y.right.parent = y;
y.color = z.color;
}
System.out.println("Inorder : ");
displayTree(t.root);
System.out.println("PreOrder: ");
displaypreOrder(t.root);
if((yOriginalColor == 1) && (x != null)) // If color of y was BLACK, delete fixup is called
{
System.out.println("Fixup called! ");
solveDoubleBlack(t, x); // DoubleBlack
}
}
void solveDoubleBlack(redBlackTrees t, Node x)
{
Node w ;
while((t.root != x) && (x.color == 1))
{
if(x == x.parent.left)
{
w = x.parent.right; // x's sibling
if(w.color == 0) // CASE 1: x's sibling is RED in color
{
w.color = 1; // setting the sibling's color to black
x.parent.color = 0; // setting the parent's color to red
leftRotate(t, x.parent);
w = x.parent.right; // setting the new sibling
}
if((w.left.color == 1) && (w.right.color == 1)) // CASE 2: x's sibling is BLACK in color and both the children of w are also BLACK
{
w.color = 0; // setting the x's sibling color to RED
x = x.parent; // setting new x to its parent
}
else // CASE 3: x's sibling color is BLACK and right child of sibling is BLACK
{
if(w.right.color == 1)
{
w.left.color = 1; // setting the sibling's left child color to BLACK
w.color = 0; // setting the sibling's color to RED
rightRotate(t, w);
w = x.parent.right; // setting the new sibling to the new sibling of x after right rotation
}
// CASE 4: When sibling of x is BLACK and right child of sibling is RED
w.color = x.parent.color; // swapping the colors between x's sibling and x's parent
w.right.color = 1; // setting the right child of x's sibling to BLACK
x.parent.color = 1; // setting the color of x's parent to BLACK
leftRotate(t, w);
x = t.root; // setting x to ROOT and terminating the while loop
}
}
else // SYMMETRIC CASE where x's sibling is the left child of its parent
{
w = x.parent.left;
if(w.color == 0) // CASE 1: sibling's color is RED
{
w.color = 1;
x.parent.color = 0;
rightRotate(t, x.parent);
w = x.parent.left;
}
if((w.right.color == 1) && (w.left.color == 1)) // CASE 2: x's xibling is BLACK and both children of x are BLACK
{
w.color = 0;
x = x.parent;
}
else
{
if(w.left.color == 1) // CASE 3: x's sibling is BLACK and left child of x's sibling is BLACK
{
w.right.color = 1;
w.color = 0;
leftRotate(t, w);
w = x.parent.left;
}
// CASE 4: x's sibling is black and left child of x's sibling is RED
w.color = x.parent.color;
w.left.color = 1;
x.parent.color = 1;
rightRotate(t, w);
x = t.root;
}
}
}
x.color = 1; // setting the root's color BLACK
}
// **********************************************************************************************************************************
// DISPLAY THE TREE
void displayTree(Node root)
{
//System.out.println("key= "+root.key+" color="+root.color);
//System.out.println("key= "+root.left.key+" color="+root.left.color);
if(root != null)
{
displayTree(root.left);
System.out.println(root.key + " C: " + root.color + " ");
displayTree(root.right);
}
}
void displaypreOrder(Node root)
{
if(root != null)
{
System.out.println(root.key);
displaypreOrder(root.left);
displaypreOrder(root.right);
}
}
// ************************************************************** MAIN METHOD ********************************************************
public static void main(String[] args)
{
redBlackTrees rb = new redBlackTrees();
// Dataset for INSERTION
Node x1 = new Node();
x1.key = 26;
System.out.println("Inserting 26 ");
rb.insert(rb, x1);
System.out.println("Inorder: ");
rb.displayTree(rb.root);
System.out.println("PreOrder: ");
rb.displaypreOrder(rb.root);
Node x2 = new Node();
x2.key = 3;
System.out.println("Inserting 3 ");
rb.insert(rb, x2);
System.out.println("Inorder: ");
rb.displayTree(rb.root);
System.out.println("PreOrder: ");
rb.displaypreOrder(rb.root);
Node x3 = new Node();
x3.key = 7;
System.out.println("Inserting 7 ");
rb.insert(rb, x3);
System.out.println("Inorder: ");
rb.displayTree(rb.root);
System.out.println("PreOrder: ");
rb.displaypreOrder(rb.root);
Node x4 = new Node();
x4.key = 11;
System.out.println("Inserting 11 ");
rb.insert(rb, x4);
System.out.println("Inorder: ");
rb.displayTree(rb.root);
System.out.println("PreOrder: ");
rb.displaypreOrder(rb.root);
Node x5 = new Node();
x5.key = 10;
System.out.println("Inserting 10 ");
rb.insert(rb, x5);
System.out.println("Inorder: ");
rb.displayTree(rb.root);
System.out.println("PreOrder: ");
rb.displaypreOrder(rb.root);
Node x6 = new Node();
x6.key = 22;
System.out.println("Inserting 22 ");
rb.insert(rb, x6);
System.out.println("Inorder: ");
rb.displayTree(rb.root);
System.out.println("PreOrder: ");
rb.displaypreOrder(rb.root);
Node x7 = new Node();
x7.key = 23;
System.out.println("Inserting 23 ");
rb.insert(rb, x7);
System.out.println("Inorder: ");
rb.displayTree(rb.root);
System.out.println("PreOrder: ");
rb.displaypreOrder(rb.root);
Node x8 = new Node();
x8.key = 8;
System.out.println("Inserting 8 ");
rb.insert(rb, x8);
System.out.println("Inorder: ");
rb.displayTree(rb.root);
System.out.println("PreOrder: ");
rb.displaypreOrder(rb.root);
Node x9 = new Node();
x9.key = 1;
System.out.println("Inserting 1 ");
rb.insert(rb, x9);
System.out.println("Inorder: ");
rb.displayTree(rb.root);
System.out.println("PreOrder: ");
rb.displaypreOrder(rb.root);
Node x10 = new Node();
x10.key = 2;
System.out.println("Inserting 2 ");
rb.insert(rb, x10);
System.out.println("Inorder: ");
rb.displayTree(rb.root);
System.out.println("PreOrder: ");
rb.displaypreOrder(rb.root);
Node x11 = new Node();
x11.key = 54;
System.out.println("Inserting 54 ");
rb.insert(rb, x11);
System.out.println("Inorder: ");
rb.displayTree(rb.root);
System.out.println("PreOrder: ");
rb.displaypreOrder(rb.root);
Node x12 = new Node();
x12.key = 44;
System.out.println("Inserting 44 ");
rb.insert(rb, x12);
System.out.println("Inorder: ");
rb.displayTree(rb.root);
System.out.println("PreOrder: ");
rb.displaypreOrder(rb.root);
Node x13 = new Node();
x13.key = 9;
System.out.println("Inserting 9 ");
rb.insert(rb, x13);
System.out.println("Inorder: ");
rb.displayTree(rb.root);
System.out.println("PreOrder: ");
rb.displaypreOrder(rb.root);
Node x14 = new Node();
x14.key = 6;
System.out.println("Inserting 6 ");
rb.insert(rb, x14);
System.out.println("Inorder: ");
rb.displayTree(rb.root);
System.out.println("PreOrder: ");
rb.displaypreOrder(rb.root);
Node x15 = new Node();
x15.key = 25;
System.out.println("Inserting 25 ");
rb.insert(rb, x15);
System.out.println("Inorder: ");
rb.displayTree(rb.root);
System.out.println("PreOrder: ");
rb.displaypreOrder(rb.root);
Node x16 = new Node();
x16.key = 88;
System.out.println("Inserting 88 ");
rb.insert(rb, x16);
System.out.println("Inorder: ");
rb.displayTree(rb.root);
System.out.println("PreOrder: ");
rb.displaypreOrder(rb.root);
Node x17 = new Node();
x17.key = 30;
System.out.println("Inserting 30 ");
rb.insert(rb, x17);
System.out.println("Inorder: ");
rb.displayTree(rb.root);
System.out.println("PreOrder: ");
rb.displaypreOrder(rb.root);
Node x18 = new Node();
x18.key = 76;
System.out.println("Inserting 76 ");
rb.insert(rb, x18);
System.out.println("Inorder: ");
rb.displayTree(rb.root);
System.out.println("PreOrder: ");
rb.displaypreOrder(rb.root);
Node x19 = new Node();
x19.key = 100;
System.out.println("Inserting 100 ");
rb.insert(rb, x19);
System.out.println("Inorder: ");
rb.displayTree(rb.root);
System.out.println("PreOrder: ");
rb.displaypreOrder(rb.root);
Node x20 = new Node();
x20.key = 0;
System.out.println("Inserting 0 ");
rb.insert(rb, x20);
System.out.println("Inorder: ");
rb.displayTree(rb.root);
System.out.println("PreOrder: ");
rb.displaypreOrder(rb.root);
//System.out.println("key= "+rb.root.key+" color="+rb.root.color);
/*System.out.println("Inorder: ");
rb.displayTree(rb.root);
System.out.println("PreOrder: ");
rb.displaypreOrder(rb.root);
// DATASET for DELETION
/*Node x1 = new Node();
x1.key = 50;
System.out.println("Inserting 50 ");
rb.insert(rb, x1);
System.out.println("Inorder: ");
rb.displayTree(rb.root);
System.out.println("PreOrder: ");
rb.displaypreOrder(rb.root);
Node x2 = new Node();
x2.key = 25;
System.out.println("Inserting 25 ");
rb.insert(rb, x2);
System.out.println("Inorder: ");
rb.displayTree(rb.root);
System.out.println("PreOrder: ");
rb.displaypreOrder(rb.root);
Node x3 = new Node();
x3.key = 75;
System.out.println("Inserting 75 ");
rb.insert(rb, x3);
System.out.println("Inorder: ");
rb.displayTree(rb.root);
System.out.println("PreOrder: ");
rb.displaypreOrder(rb.root);
Node x4 = new Node();
x4.key = 48;
System.out.println("Inserting 48 ");
rb.insert(rb, x4);
System.out.println("Inorder: ");
rb.displayTree(rb.root);
System.out.println("PreOrder: ");
rb.displaypreOrder(rb.root);
Node x5 = new Node();
x5.key = 80;
System.out.println("Inserting 80 ");
rb.insert(rb, x5);
System.out.println("Inorder: ");
rb.displayTree(rb.root);
System.out.println("PreOrder: ");
rb.displaypreOrder(rb.root);
Node x6 = new Node();
x6.key = 27;
System.out.println("Inserting 27 ");
rb.insert(rb, x6);
System.out.println("Inorder: ");
rb.displayTree(rb.root);
System.out.println("PreOrder: ");
rb.displaypreOrder(rb.root);
Node x7 = new Node();
x7.key = 28;
System.out.println("Inserting 28 ");
rb.insert(rb, x7);
System.out.println("Inorder: ");
rb.displayTree(rb.root);
System.out.println("PreOrder: ");
rb.displaypreOrder(rb.root);*/
rb.delete(rb, 11);
System.out.println("After deleting 11 ");
System.out.println("Inorder: ");
rb.displayTree(rb.root);
System.out.println("PreOrder: ");
rb.displaypreOrder(rb.root);
rb.delete(rb, 44);
System.out.println("After deleting 44 ");
System.out.println("Inorder: ");
rb.displayTree(rb.root);
System.out.println("PreOrder: ");
rb.displaypreOrder(rb.root);
}
}