Repository navigation
Expand file tree
/
Copy pathRBTree.java
More file actions
655 lines (564 loc) · 15.8 KB
/
Copy pathRBTree.java
File metadata and controls
655 lines (564 loc) · 15.8 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
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
package study;
public class RBTree<T extends Comparable<T>> {
private RBNode<T> Root;
public RBTree(){
Root=null;
}
private RBNode<T> getParent(RBNode<T> node){
if (node!=null){
return node.parent;
}else{
return null;
}
}
private void setColor(RBNode<T>node, NodeColor color){
if(node!=null)
node.color=color;
}
private NodeColor getColor(RBNode<T> node){
if(node!=null)
return node.color;
else
return null;
}
private boolean isRed(RBNode<T> node){
if(node!=null && node.color==NodeColor.red){
return true;
}else
return false;
}
private boolean isBlack(RBNode<T> node){
if(node!=null && node.color==NodeColor.black){
return true;
}else
return false;
}
private void setRed(RBNode<T>node){
if(node!=null)
node.color=NodeColor.red;
}
private void setBlack(RBNode<T>node){
if(node!=null)
node.color=NodeColor.black;
}
private void setParent(RBNode<T>node, RBNode<T>parent){
if(node!=null)
node.parent=parent;
}
//pre order traverse
private void preOrder(RBNode<T> tree){
if(tree!=null){
System.out.println(tree.key+" ");
preOrder(tree.left);
preOrder(tree.right);
}
}
public void preOrder(){
preOrder(Root);
}
//in order traverse
private void inOrder(RBNode<T> tree){
if(tree!=null){
inOrder(tree.left);
System.out.println(tree.key+" ");
inOrder(tree.right);
}
}
public void inOrder(){
inOrder(Root);
}
//post order traverse
private void postOrder(RBNode<T> tree){
if(tree!=null){
postOrder(tree.left);
postOrder(tree.right);
System.out.println(tree.key+" ");
}
}
public void postOrder(){
postOrder(Root);
}
//recursive search
private RBNode recursiveSearch(RBNode<T> x, T key){
if(x==null) return null;
int cmp=key.compareTo(x.key);
if(cmp<0)
return recursiveSearch(x.left,key);
else if(cmp>0)
return recursiveSearch(x.right, key);
else
return x;
}
public RBNode<T> recursiveSearch(T key){
return recursiveSearch(Root, key);
}
//iterative search
private RBNode<T> iterativeSearch(RBNode<T> x, T key){
while(x!=null){
int cmp=key.compareTo(x.key);
if(cmp<0) x=x.left;
else if(cmp>0) x=x.right;
else return x;
}
return x;
}
public RBNode<T> iterativeSearch(T key){
return iterativeSearch(Root, key);
}
//find the minimum of tree
private RBNode<T> min(RBNode<T> tree){
if(tree==null) return null;
while(tree.left!=null)
tree=tree.left;
return tree;
}
public T minimum(){
RBNode<T> p=min(Root);
if(p!=null)
return p.key;
return null;
}
//find the maximum of tree
private RBNode<T> max(RBNode<T> tree){
if(tree==null) return null;
while(tree.right!=null)
tree=tree.right;
return tree;
}
public T maximum(){
RBNode<T> p=max(Root);
if(p!=null)
return p.key;
return null;
}
//find the minimum(leftmost) child bigger than current node: successor
/**
* O
* / \
* O O (<--x)
* / \ / \
* O O O O
* /\ /\ / \ / \
* O O O O O O O O
* (^)(^)
* (|)(|)
* (predecessor)(successor)
* */
public RBNode<T> successor(RBNode<T> x){
//case 1
//if node x has right child, then the successor is the leftmost child bigger than x which the subtree rooted at the node of x
//the successor is also the minimum node at the subtree rooted at x.right
if(x.right!=null)
return min(x.right);
//case 2
//if node x has no right child
//case 2.1: if x is a left leaf, its successor is the immediate parent node
RBNode<T> y1=x.parent;
if(x==y1.left && x.right==null && x.left==null)
return y1;
//case 2.2: if x is a right leaf, its successor is a parent node
//(upward in the tree)(also an lowest ancestor) whose leftchild is also an ancestor
// the successor is immediately bigger than x
RBNode<T> y2=x.parent;
while((x==y2.right) && (y2!=null)){
x=y2;
y2=y2.parent;
}
return y2;
}
//find the maximum node smaller than current node: predecessor
public RBNode<T> predecessor(RBNode<T> x){
//case 1
//if node x has left child, then the predecessor is the rightmost child smaller than x which the subtree rooted at the node of x
//the predecessor is also the maximum node at the subtree rooted at x.left
if(x.left!=null)
return max(x.left);
//case 2
//if node x has no left child
//case 2.1: if x is a right leaf, its predecessor is the immediate parent node
RBNode<T> y1=x.parent;
if(x==y1.right && x.right==null && x.left==null)
return y1;
//case 2.2: if x is a left leaf, its predecessor is a parent node
//(upward in the tree)(also an lowest ancestor)
// the predecessor is immediately smaller than x
RBNode<T> y2=x.parent;
while((x==y2.left) && (y2!=null)){
x=y2;
y2=y2.parent;
}
return y2;
}
//left rotate
// px px
// * / /
// * x y
// * / \ --(left rotate)-->. / \ #
// * lx y x ry
// * / \ / \
// * ly ry lx ly
// *
// *
// */
private void leftRotate(RBNode<T>x){
//step1: set y as x.right
RBNode<T> y=x.right;
//step2: set y.left-->x.right
x.right=y.left;
//step3: if y.left!=null, set x as the parent of y.left
if(y.left!=null)
y.left.parent=x;
//step4: set x.parent -->y.parent
y.parent=x.parent;
//step5: set y's position with original x's parent
if(x.parent==null){
this.Root=y;
}else{
if(x.parent.left==x)
x.parent.left=y;
else
x.parent.right=y;
}
//step6: set x as the y.left
y.left=x;
//step7: set y as x's parent
x.parent=y;
}
//right rotate
// px px
// * / /
// * x y
// * / \ <--(right rotate)-- / \ #
// * lx y x ry
// * / \ / \
// * ly ry lx ly
// *
// *
// */
private void rightRotate(RBNode<T>y){
//step1: set up y
RBNode<T> x=y.left;
//step2: set x.right to be y.left
y.left=x.right;
//step3: take care of the special node x.right
if(x.right!=null)
x.right.parent=y;
//step4: set y.parent to be the x.parent
x.parent=y.parent;
//step5: set y's position with original x's parent
if(y.parent==null){
this.Root=x;
}
else{
if(y==y.parent.right)
y.parent.right=x;
else
y.parent.left=x;
}
//step6: reset x and y relation
x.right=y;
y.parent=x;
}
//fixup red-black tree after insert a new node
private void insertFixup(RBNode<T> node){
RBNode<T> father;
RBNode<T> grandfather;
father=getParent(node);
while((father!=null) && isRed(father)){
grandfather=getParent(father);
//case1: if father is the grandfather.left
if(father==grandfather.left){
//1.1 the uncle is red
RBNode<T> uncle=grandfather.right;
if((uncle!=null) && (isRed(uncle))){
setBlack(uncle);
setBlack(father);
setRed(grandfather);
node=grandfather;
continue;
}
//1.2 the uncle is black, current node is father.right
if(isBlack(uncle) && (father.right==node)){
RBNode<T> tmp;
leftRotate(father);
tmp=father;
father=node;
node=tmp;
}
//1.3 uncle is black, and current node is father.left
setBlack(father);
setRed(grandfather);
rightRotate(grandfather);
}
//case 2: if father is the grandfather.right
else{
//case2.1:uncle is red
RBNode<T> uncle=grandfather.left;
if((uncle!=null)&&isRed(uncle)){
setBlack(uncle);
setBlack(father);
setRed(grandfather);
node=grandfather;
continue;
}
//case2.2:uncle is black, and current node is the left child of father
if((isBlack(uncle)) && (father.left==node)){
RBNode<T> tmp;
rightRotate(father);
tmp=father;
father=node;
node=tmp;
}
//case2.3:uncle is black, and current node is the right child of father
setBlack(father);
setRed(grandfather);
leftRotate(grandfather);
}
}
setBlack(this.Root);
}
//insert node to the tree
private void insert(RBNode<T>node){
int cmp;
RBNode<T>y=null;
RBNode<T>x=this.Root;
//insert a node
while(x!=null){
y=x;
cmp=node.key.compareTo(x.key);
if(cmp<0)
x=x.left;
else
x=x.right;
}
node.parent=y;
if(y!=null){
cmp=node.key.compareTo(y.key);
if(cmp<0)
y.left=node;
else
y.right=node;
}
else{
this.Root=node;
}
//set the node color
setRed(node);
//fixup the red-black tree
insertFixup(node);
}
public void insert(T key){
RBNode<T> node=new RBNode<T>(key, NodeColor.black, null, null, null);
if(node!=null)
insert(node);
}
//fixup red-black tree after delete a new node
private void deleteFixup(RBNode<T>node, RBNode<T> father){
RBNode<T> x;
while((node==null || isBlack(node))&&(node!=this.Root)){
//1.0 if current node is the father.left
if(node==father.left){
x=father.right;
//1.1: x's bother is red
if(isRed(x)){
setBlack(x);
setRed(father);
leftRotate(father);
x=father.right;
}
//1.2 x's brother is black, both child of x's brother is also black
if((x.left==null || isBlack(x.left))&&(x.right==null || isBlack(x.right))){
setRed(x);
node=father;
father=getParent(node);
}
else{
//1.3 x's brother is black, leftchild of x's brother is red, rightchild of x's brother is black
if(x.right==null || isBlack(x.right)){
setBlack(x.left);
setRed(x);
rightRotate(x);
x=father.right;
}
//1.4 x's brother is black, rightchild of x's brother is red, leftchild of x's brother is whatever
setColor(x, getColor(father));
setBlack(father);
setBlack(x.right);
leftRotate(father);
node=this.Root;
break;
}
}
//2.0 if current node is the father.right
else{
x=father.left;
//2.1
if(isRed(x)){
setBlack(x);
setRed(father);
rightRotate(father);
x=father.left;
}
//2.2
if((x.left==null ||isBlack(x.left))&&(x.right==null || isBlack(x.right))){
setRed(x);
node=father;
father=getParent(node);
}
else{
//2.3
if(x.left==null || isBlack(x.left)){
setBlack(x.left);
setRed(x);
leftRotate(x);
x=father.left;
}
//2.4
setColor(x,getColor(father));
setBlack(father);
setBlack(x.left);
rightRotate(father);
node=this.Root;
break;
}
}
}
if(node!=null)
setBlack(node);
}
//delete a node
private void delete(RBNode<T>node){
RBNode<T> son;
RBNode<T> father;
NodeColor color;
//case 1: node.left and node.right both are non-empty
if((node.left)!=null && (node.right)!=null){
RBNode<T> replace=node;
//get successor of current node
replace=replace.right;
while(replace.left!=null)
replace=replace.left;
if(getParent(node)!=null){
if(getParent(node).left==node)
getParent(node).left=replace;
else
getParent(node).right=replace;
}else{
this.Root=replace;
}
son=replace.right;
father=getParent(replace);
color=getColor(replace);
if(father==node){
father=replace;
}else{
if(son!=null)
setParent(son, father);
father.left=son;
replace.right=node.right;
setParent(node.right, replace);
}
replace.parent=node.parent;
replace.color=node.color;
replace.left=node.left;
node.left.parent=replace;
if(color==NodeColor.black)
deleteFixup(son,father);
node=null;
return;
}
//case 2:
if(node.left!=null)
son=node.left;
else
son=node.right;
//case 3:
father=node.parent;
color=node.color;
if(son!=null)
son.parent=father;
//case 4:
if(father!=null){
if(father.left==node)
father.left=son;
else
father.right=son;
}else{
this.Root=son;
}
//case 5
if(color==NodeColor.black)
deleteFixup(son, father);
node=null;
}
//delete a node
public void delete(T key){
RBNode<T> node;
if((node=recursiveSearch(Root, key))!=null)
delete(node);
}
private void destroy(RBNode<T> tree){
if(tree==null)
return;
if(tree.left!=null)
destroy(tree.left);
if(tree.right!=null)
destroy(tree.right);
tree=null;
}
public void clear(){
destroy(Root);
Root=null;
}
private void print(RBNode<T> tree, T key, int direction) {
if(tree != null) {
if(direction==0) // if root node
System.out.printf("%2d(B) is root\n", tree.key);
else // it non-root node
System.out.printf("%2d(%s) is %2d's %6s child\n", tree.key, isRed(tree)?"R":"B", key, direction==1?"right" : "left");
print(tree.left, tree.key, -1);//left child
print(tree.right,tree.key, 1);//right child
}
}
public void print() {
if (Root != null)
print(Root, Root.key, 0);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] a = {10, 40, 30, 60, 90, 70, 20, 50, 80};
RBTree<Integer> rbTree=new RBTree<Integer>();
System.out.println("the original data");
for(int i=0; i<a.length;i++)
System.out.print(a[i]+" ");
System.out.println(" ");
for(int i=0; i<a.length; i++){
rbTree.insert(a[i]);
}
rbTree.print();
System.out.println(" ");
}
}
class RBNode<T extends Comparable <T>>{
public NodeColor color;
T key;
RBNode<T> left;
RBNode<T> right;
RBNode<T> parent;
public RBNode(T key, NodeColor color, RBNode<T>parent, RBNode<T>left, RBNode<T>right){
this.key=key;
this.color=color;
this.parent=parent;
this.left=left;
this.right=right;
}
public T getKey(){
return key;
}
public String toString(){
return " "+key+this.color+" ";
}
}
enum NodeColor{
red, black;
}