📓 定义

红黑树是一种含有红黑节点并能自平衡的二叉查找树

🌲二叉查找树

满足约束:左结点的值小于父结点,父结点的值小于右结点的值。

场景理解:假设二叉查找树建立在x-y笛卡尔坐标系中,则所有结点向x轴投影,值正好沿着x轴递增。


🔰 性质

  • 每个结点要么是黑色,要么是红色
  • 根节点是黑色
  • 每个叶子结点(NIL)是黑色的(虚结点)
  • 每个红色结点的两个子结点一定都是黑色
  • 任意一结点到每个叶子结点的路径都包含数量相同的黑结点(黑色完美平衡)

以上为最简性质,任何一条不可缺少,任意四条不能推出另外一条。


🌗 平衡

红黑树是非完美平衡二叉查找树,是完美黑色平衡二叉查找树。

⭕红黑树自平衡的最小单元


红黑树的自平衡

插入只考虑G-{P, U}-C三代,删除只考虑P-{C, B}-{CL, CR, BL, BR}三代

🔱红黑树自平衡的原子操作

包括:变色、左旋、右旋

旋转要有圆心,有方向。

旋转结点是父结点围绕子节点旋转(子节点为圆心)。

☬ 旋转结点 ☬

🌐变色:P-Black=>Red,CB-Red=>Black

☬ 变色操作 ☬

🌔左旋:旋转结点绕圆心逆时针方向旋转。基于最短路径来确定方向。

☬ 左旋操作 ☬

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
/**
* <p>左旋</p>
* <p>过程:父亲下沉,右子上升,右子的左子变为原父的右子</p>
* <p>
* 左旋X结点
* P P
* / /
* X Y
* / \ --(左旋)--> / \
* lX Y X rY
* / \ / \
* lY rY lX lY
* </p>
* @param x
*/
public void leftRotate(RBTNode<T, D> x) {
/* 右子结点 */
RBTNode<T, D> y = x.getRight();
/* 父亲结点 */
RBTNode<T, D> p = x.getParent();

/* Y的左子 变成 X的右子
* 若X不Y的左子不为空
* 则设置Y的左子的父亲为X */
x.setRight(y.getLeft());
if (y.getLeft() != null) {
y.getLeft().setParent(x);
}

/* 设置Y的父亲为P
* 1. P为空,则根节点设置为Y
* 2. X为P的左子, 则P的左子设置为Y
* 3. X为P的右子,则P的右子设置为Y */
y.setParent(p);
if (p == null) {
this.root = y;
} else {
if (p.getLeft() == x) {
p.setLeft(y);
} else {
p.setRight(y);
}
}

/* 将X的父亲设置为Y
* 将Y的左子设置为X */
x.setParent(y);
y.setLeft(x);
}

🌖右旋:旋转结点绕圆心顺时针方向旋转。基于最短路径确定方向。

☬ 右旋操作 ☬

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
/**
* <p>右旋</p>
* <p>过程:父亲下沉,左子上升,左子的右子变成原父的左子</p>
* <p>
* 右旋X结点
* P P
* / /
* X Y
* / \ --(右旋)--> / \
* Y rX lY X
* / \ / \
* lY rY rY rX
* </p>
* @param x
*/
public void rightRotate(RBTNode<T, D> x) {
/* 左子结点 */
RBTNode<T, D> y = x.getLeft();
/* 父亲结点 */
RBTNode<T, D> p = x.getParent();

/* Y的右子 变成 X的左子
* 若Y的右子不为空
* 则设置Y的右子的父亲为X */
x.setLeft(y.getRight());
if (y.getRight() != null) {
y.getRight().setParent(x);
}

/* 设置Y的父亲为P
* 1. P为空,则根节点设置为Y
* 2. X为P的左子, 则P的左子设置为Y
* 3. X为P的右子,则P的右子设置为Y */
y.setParent(p);
if (p == null) {
this.root = y;
} else {
if (p.getLeft() == x) {
p.setLeft(y);
} else {
p.setRight(y);
}
}

/* 将X的父亲设置为Y
* 将Y的右子设置为X */
x.setParent(y);
y.setRight(x);
}

🌀 增删

➕插入结点

新增结点默认为红色,避免破坏黑色完美平衡。

首先寻找新结点的插入位置,即找到新结点的父亲,然后决定将新结点插入到父亲的左边还是右边。

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
/**
* <p>插入结点</p>
* @param key
* @param data
*/
public void insertNode(T key, D data) {
int cmp;
RBTNode<T, D> x = this.root;
RBTNode<T, D> y = null;

/* 寻找新结点的插入位置 */
while (x != null) {
y = x;
cmp = key.compareTo(x.getKey());

if (cmp == 0) {
/* key已存在,直接更新 */
System.out.println(getCurrentTime() + " [WARN] key已存在");
System.out.println(getCurrentTime() + " [INFO] 更新value: " + get(key) + " => " + data);
return;
} else if (cmp > 0){
/* key较大,继续向右查询 */
x = x.getRight();
} else if (cmp < 0) {
/* key较小,继续向左查询 */
x = x.getLeft();
}
}

/* 生成一个新的结点 */
RBTNode<T, D> node = new RBTNode<>(RBTColor.red, key, data, null, null, null);
System.out.println(getCurrentTime() + " [INFO] 新增结点 (" + key + ", " + data + ")");
/* 总结点数量+1 */
this.count.incrementAndGet();
/* 设置新结点的父亲为Y */
node.setParent(y);

/* 再次比较决定新结点是y的左子还是右子*/
if (y == null) {
this.root = node;
} else {
cmp = key.compareTo(y.getKey());
if (cmp > 0) {
y.setRight(node);
} else {
y.setLeft(node);
}
}

/* 最后进行自平衡 */
balanceInsertion(node);
}

💉插入修复

插入结点为红色,因此只有当父亲结点为红色时才需要修复。

G-祖父、P-父亲、U-叔叔、C-插入。

我总结了五种情况以及解决口诀:

(1)叔叔为红

CASE 1

Description: 叔叔为红

Solution: GPU变色,若不满足红黑树约束则递归变色


(2)叔叔为黑

CASE 2

Description: 父为左子,GPC三点一线

Solution: 右旋祖父,GP变色

1
2
3
4
5
6
7
8
9
10
11
12
  (1) 右旋祖父结点
黑祖 红父
/ \ / \
红父 黑叔 --(右旋)--> 红插 黑祖
/ \
红插 黑叔
(2) 祖父和父亲变色
红父 黑父
/ \ / \
红插 黑祖 --(变色)--> 红插 红祖
\ \
黑叔 黑叔

CASE 3:

Description: 父为左子,GPC三角关系

Solution: 左旋父亲,交换PC,右旋祖父,GP变色

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
   (1) 左旋父亲结点,并且交换父子身份,此时GPC三点一线
黑祖 黑祖 黑祖
/ \ / \ / \
红父 黑叔 --(左旋)--> 红插 黑叔 --(交换)--> 红父 黑叔
\ / /
红插 红父 红插
(2) 右旋祖父结点
黑祖 红父
/ \ / \
红父 黑叔 --(右旋)--> 红插 黑祖
/ \
红插 黑叔
(3) 祖父和父亲变色
红父 黑父
/ \ / \
红插 黑祖 --(变色)--> 红插 红组
\ \
黑叔 黑叔

CASE 4

Description: 父为右子,GPC三点一线

Solution: 左旋祖父,GP变色

1
2
3
4
5
6
7
8
9
10
11
12
  (1) 左旋祖父结点
黑祖 红父
/ \ / \
黑叔 红父 --(右旋)--> 黑祖 红插
\ /
红插 黑叔
(2) 祖父和父亲变色
红父 黑父
/ \ / \
黑祖 红插 --(变色)--> 红祖 红插
/ /
黑叔 黑叔

CASE 5

Description: 父为右子,GPC三角关系

Solution: 右旋父亲,交换PC,左旋祖父,GP变色

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
   (1) 右旋父亲结点,并且交换父子身份,此时GPC三点一线
黑祖 黑祖 黑祖
/ \ / \ / \
黑叔 红父 --(右旋)--> 黑祖 红插 --(交换)--> 黑祖 红父
/ \ \
红插 红父 红插
(2) 左旋祖父
黑祖 红父
/ \ / \
黑叔 红父 --(左旋)--> 黑祖 红插
\ /
红插 黑叔
(3) 祖父和父亲变色
红父 黑父
/ \ / \
黑叔 红插 --(变色)--> 红祖 红插
/ /
黑叔 黑叔

➖删除结点

三种情况,解决方案主要为寻找后裔顶替自己

CASE 1

Description: 待删结点左子和右子都存在:

Solution: 替代结点为右子树的最左孩子,然后调整关系

CASE 2:

Description: 待删结点没有左子和右子:

Solution: 直接删除,然后调整关系

CASE 3:

Description: 待删结点只有左子或者右子:

Solution: 替代结点为存在的孩子,然后调整关系


💉删除修复

仅删除黑色结点需要修复,删除红色不需要。

P-父亲、D-删除、B-兄弟、BR-兄弟右子、BL-兄弟左子。

  • D为左子
    • B为红色:左旋父亲,父亲染红,兄弟染黑,然后continue
    • B为黑色
      • BL为黑色且BR黑色:兄弟染红,父亲回溯
      • BL为红色且BR为黑色:右旋兄弟,兄弟染红,BL染黑
      • BR为红色:左旋父亲,父亲的颜色给兄弟,父亲黑化,BR黑化,然后break
  • D为右子
    • B为红色:右旋父亲,父亲染红,兄弟染黑,然后continue
    • B为黑色:
      • BL为黑色且BR为黑色:兄弟染红,父亲回溯
      • BL为红色且BR为黑色:左旋兄弟,父亲染红,BR染黑
      • BL为红色:右旋父亲,父亲的颜色给兄弟,父亲黑化,BL黑化,然后break

📑 源码

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
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
package top.parak.DataStructures.RBTree;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;

/**
* @author KHighness
* @since 2020-11-05
*/

/** 红黑颜色 */
enum RBTColor {
red,
black
}

/** 红黑结点 */
class RBTNode<T extends Comparable<T>, D> {

/* 结点颜色 */
private RBTColor color;
/* 结点键值 */
private T key;
/* 结点数据 */
private D data;
/* 父亲结点 */
private RBTNode<T, D> parent;
/* 左子结点 */
private RBTNode left;
/* 右子结点 */
private RBTNode right;

public RBTNode(RBTColor color, T key, D data, RBTNode<T, D> parent, RBTNode left, RBTNode right) {
this.color = color;
this.key = key;
this.data = data;
this.parent = parent;
this.left = left;
this.right = right;
}

public RBTColor getColor() {
if (this != null) {
return color;
}
return null;
}

public void setColor(RBTColor color) {
this.color = color;
}

public void updateColor() {
if (this.color == RBTColor.red) {
this.color = RBTColor.black;
} else {
this.color = RBTColor.red;
}
}

public T getKey() {
if (this != null) {
return key;
}
return null;
}

public void setKey(T key) {
this.key = key;
}

public D getData() {
if (this != null) {
return data;
}
return null;
}

public void setData(D data) {
this.data = data;
}

public RBTNode<T, D> getParent() {
return parent;
}

public void setParent(RBTNode<T, D> parent) {
this.parent = parent;
}

public RBTNode getLeft() {
if (this != null) {
return left;
}
return null;
}

public void setLeft(RBTNode left) {
this.left = left;
}

public RBTNode getRight() {
if (this != null) {
return right;
}
return null;
}

public void setRight(RBTNode right) {
this.right = right;
}

@Override
public String toString() {
return "RBTNode[" +
"color=" + color +
", key=" + key +
", data=" + data +
']';
}
}

/* 红黑树 */
public class RedBlackTree<T extends Comparable<T>, D> {

/* 时间格式 */
private static final SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

/* 根节点 */
private RBTNode<T, D> root;

/* 树结点数量 */
private AtomicLong count = new AtomicLong(0);


/**
* <p>获取时间</p>
* @return
*/
private String getCurrentTime() {
return SDF.format(new Date());
}

/**
* <p>大小<p/>
* @return
*/
public long size() {
return count.get();
}

/**
* <p>查询</p>
* @param key
* @return
*/
public D get(T key) {
RBTNode<T, D> node = search(key, this.root);
return node == null ? null : node.getData();
}

/**
* <p>插入</p>
* @param key
* @param data
*/
public void add(T key, D data) {
insertNode(key, data);
}

/**
* <p>删除</p>
* @param key
*/
public void del(T key) {
RBTNode<T, D> node = search(key, this.root);
if (node != null) {
deleteNode(node);
} else {
System.out.println(getCurrentTime() + " [ERROR] " + "key为" + key + "的结点不存在");
}
}

/**
* <p>root-getter</p>
* @return
*/
public RBTNode<T, D> getRoot() {
return root;
}

/**
* <p>判断结点是否为红色</p>
* @param node
* @return
*/
public Boolean isRed(RBTNode<T, D> node) {
return (node != null && node.getColor() == RBTColor.red) ? true : false;
}

/**
* <p>判断结点是否为黑色</p>
* @param node
* @return
*/
public Boolean isBlack(RBTNode<T, D> node) {
return (node == null || node.getColor() == RBTColor.black) ? true : false;
}

/**
* <p>查询key值的结点</p>
* <p>递归查询: 比较key,相等直接返回,过大则继续向右,过小则继续向左</p>
* @param key
* @param node
* @return
*/
public RBTNode<T, D> search(T key, RBTNode<T, D> node) {
if (node != null) {
int cmp = key.compareTo(node.getKey());
if (cmp == 0) {
return node;
} else if (cmp > 0) {
return search(key, node.getRight());
} else if (cmp < 0) {
return search(key, node.getLeft());
}
}
return null;
}

/**
* <p>左旋</p>
* <p>过程:父亲下沉,右子上升,右子的左子变为原父的右子</p>
* <p>
* 左旋X结点
* P P
* / /
* X Y
* / \ --(左旋)--> / \
* lX Y X rY
* / \ / \
* lY rY lX lY
* </p>
* @param x
*/
private void leftRotate(RBTNode<T, D> x) {
/* 右子结点 */
RBTNode<T, D> y = x.getRight();
/* 父亲结点 */
RBTNode<T, D> p = x.getParent();

/* Y的左子 变成 X的右子
* 若X不Y的左子不为空
* 则设置Y的左子的父亲为X */
x.setRight(y.getLeft());
if (y.getLeft() != null) {
y.getLeft().setParent(x);
}

/* 设置Y的父亲为P
* 1. P为空,则根节点设置为Y
* 2. X为P的左子, 则P的左子设置为Y
* 3. X为P的右子,则P的右子设置为Y */
y.setParent(p);
if (p == null) {
this.root = y;
} else {
if (p.getLeft() == x) {
p.setLeft(y);
} else {
p.setRight(y);
}
}

/* 将X的父亲设置为Y
* 将Y的左子设置为X */
x.setParent(y);
y.setLeft(x);
}

/**
* <p>右旋</p>
* <p>过程:父亲下沉,左子上升,左子的右子变成原父的左子</p>
* <p>
* 右旋X结点
* P P
* / /
* X Y
* / \ --(右旋)--> / \
* Y rX lY X
* / \ / \
* lY rY rY rX
* </p>
* @param x
*/
private void rightRotate(RBTNode<T, D> x) {
/* 左子结点 */
RBTNode<T, D> y = x.getLeft();
/* 父亲结点 */
RBTNode<T, D> p = x.getParent();

/* Y的右子 变成 X的左子
* 若Y的右子不为空
* 则设置Y的右子的父亲为X */
x.setLeft(y.getRight());
if (y.getRight() != null) {
y.getRight().setParent(x);
}

/* 设置Y的父亲为P
* 1. P为空,则根节点设置为Y
* 2. X为P的左子, 则P的左子设置为Y
* 3. X为P的右子,则P的右子设置为Y */
y.setParent(p);
if (p == null) {
this.root = y;
} else {
if (p.getLeft() == x) {
p.setLeft(y);
} else {
p.setRight(y);
}
}

/* 将X的父亲设置为Y
* 将Y的右子设置为X */
x.setParent(y);
y.setRight(x);
}

/**
* <p>插入结点</p>
* @param key
* @param data
*/
private void insertNode(T key, D data) {
int cmp;
RBTNode<T, D> x = this.root;
RBTNode<T, D> y = null;

/* 寻找新结点的插入位置 */
while (x != null) {
y = x;
cmp = key.compareTo(x.getKey());

if (cmp == 0) {
/* key已存在,直接更新 */
System.out.println(getCurrentTime() + " [WARN] key已存在");
System.out.println(getCurrentTime() + " [INFO] 更新value: " + get(key) + " => " + data);
return;
} else if (cmp > 0){
/* key较大,继续向右查询 */
x = x.getRight();
} else if (cmp < 0) {
/* key较小,继续向左查询 */
x = x.getLeft();
}
}

/* 生成一个新的结点 */
RBTNode<T, D> node = new RBTNode<>(RBTColor.red, key, data, null, null, null);
System.out.println(getCurrentTime() + " [INFO] 新增结点 (" + key + ", " + data + ")");
/* 总结点数量+1 */
this.count.incrementAndGet();
/* 设置新结点的父亲为Y */
node.setParent(y);

/* 再次比较决定新结点是y的左子还是右子*/
if (y == null) {
this.root = node;
} else {
cmp = key.compareTo(y.getKey());
if (cmp > 0) {
y.setRight(node);
} else {
y.setLeft(node);
}
}

/* 最后进行自平衡 */
balanceInsertion(node);
}

/**
* <p>插入结点的自平衡操作</p>
* <p>由于插入节点默认颜色为红色,所以只有父结点为红色时候才需要修复
* 分三种情况讨论</p>
* <li>case1. 叔叔结点也为红色</li>
* <li>case2. 叔叔结点为空,且祖父子三点一线</li>
* <li>case3. 叔叔结点为空,且祖父子三角关系</li>
* <p>G-祖父、P-父亲、U-叔叔、C-插入</p>
* @param node
*/
private void balanceInsertion(RBTNode<T, D> node) {
/* 父亲 · 祖父 */
RBTNode<T, D> paren, grand;

/* 当父亲节点为黑色时,结束修复 */
while (((paren = node.getParent()) != null) && isRed(paren)) {
grand = paren.getParent();

/* 确定父亲和叔叔的左右关系 */

/* CASE: 父左叔右 */
if (grand.getLeft() == paren) {
RBTNode<T, D> uncle = grand.getRight();

/**
* case1: PU双红
* solution1: GPU变色
* 如果此时整棵树不满足约束,则递归进行GPU变色
*/
if (isRed(uncle)) {
grand.setColor(RBTColor.red);
paren.setColor(RBTColor.black);
uncle.setColor(RBTColor.black);
node = grand;
continue;
}
/*
* case2: P红U黑,父为左子,GPC三点一线
* solution2: 右旋祖父,GP变色
* (1) 右旋祖父结点
* 黑祖 红父
* / \ / \
* 红父 黑叔 --(右旋)--> 红插 黑祖
* / \
* 红插 黑叔
* (2) 祖父和父亲变色
* 红父 黑父
* / \ / \
* 红插 黑祖 --(变色)--> 红插 红祖
* \ \
* 黑叔 黑叔
*
*
* case3: P红U黑,父为左子,GPC三角关系
* solution3: 左旋父亲,交换PC,右旋祖父,GP变色
* (1) 左旋父亲结点,并且交换父子身份,此时GPC三点一线
* 黑祖 黑祖 黑祖
* / \ / \ / \
* 红父 黑叔 --(左旋)--> 红插 黑叔 --(交换)--> 红父 黑叔
* \ / /
* 红插 红父 红插
* (2) 右旋祖父结点
* 黑祖 红父
* / \ / \
* 红父 黑叔 --(右旋)--> 红插 黑祖y
* / \
* 红插 黑叔
* (3) 祖父和父亲变色
* 红父 黑
* / \ / \
* 红插 黑祖 --(变色)--> 红插 红组
* \ \
* 黑叔 黑叔
*
* attention:
* 三角关系经过一步旋转即可转换成三点一线
* 因此case3先经过一步处理到case2,再进行case2的处理
*/
else {
if (paren.getRight() == node) { // case3
leftRotate(paren);
RBTNode<T, D> temp = node;
node = paren;
paren = temp;
} // case2
rightRotate(grand);
grand.updateColor();
paren.updateColor();
}
}
/* CASE: 父右叔左 */
else {
RBTNode<T, D> uncle = grand.getLeft();

/**
* case1: PU双红(父亲和叔叔都为红色)
* solution1: GPU变色(祖父变为红色,父亲和叔叔都变为黑色)
* 如果此时整棵树不满足约束,则递归进行GPU变色
*/
if (isRed(uncle)) {
grand.setColor(RBTColor.red);
paren.setColor(RBTColor.black);
uncle.setColor(RBTColor.black);
node = grand;
continue;
}
/*
* case4: P红U黑,父为左子,GPC三点一线
* solution4: 左旋祖父,GP变色
* (1) 左旋祖父结点
* 黑祖 红父
* / \ / \
* 黑叔 红父 --(右旋)--> 黑祖 红插
* \ /
* 红插 黑叔
* (2) 祖父和父亲变色
* 红父 黑父
* / \ / \
* 黑祖 红插 --(变色)--> 红祖 红插
* / /
* 黑叔 黑叔
*
* case5: P红U黑,父为右子,GPC三角关系
* solution5: 右旋父亲,交换PC,左旋祖父,GP变色
* (1) 右旋父亲结点,并且交换父子身份,此时GPC三点一线
* 黑祖 黑祖 黑祖
* / \ / \ / \
* 黑叔 红父 --(右旋)--> 黑祖 红插 --(交换)--> 黑祖 红父
* / \ \
* 红插 红父 红插
* (2) 左旋祖父
* 黑祖 红父
* / \ / \
* 黑叔 红父 --(左旋)--> 黑祖 红插
* \ /
* 红插 黑叔
* (3) 祖父和父亲变色
* 红父 黑父
* / \ / \
* 黑叔 红插 --(变色)--> 红祖 红插
* / /
* 黑叔 黑叔
*
* attention:
* 三角关系经过一步旋转即可转换成三点一线
* 因此case3先经过一步处理到case2,再进行case2的处理
*/
else {
if (paren.getLeft() == node) { // case3
rightRotate(paren);
RBTNode<T, D> temp = node;
node = paren;
paren = temp;
} // case2
leftRotate(grand);
grand.updateColor();
paren.updateColor();
}
}
}

/* 保证根节点为黑色 */
if (root == node) {
node.setColor(RBTColor.black);
}
}

/**
* <p>删除结点</p>
* <p>三种情况</p>
* <li>case1. 待删结点左子和右子都存在</li>
* <li>case2. 待删结点没有左子和右子</li>
* <li>case3. 待删结点只有左子或者右子</li>
* @param node
*/
private void deleteNode(RBTNode<T, D> node) {
/* 父亲 · 儿子 · 继承者 */
RBTNode<T, D> paren, child, replace;
RBTColor color;

/*
* case1: 待删结点左子和右子都存在
* solution1:
* - 找到该结点的右子树中的最左子结点
* - 把它的值和要删除的结点的值进行交换
* - 然后删除这个结点即相当于删除所需删除结点
*/
if ((node.getLeft() != null) && (node.getRight() != null)) {

/* 获取其后继结点: 右子树中的最左子结点 */
replace = descendants(node);
paren = replace.getParent();
child = replace.getRight();
color = replace.getColor();

if (node == replace.getParent()) {
/**
* case:
* node replace
* \ \
* replace --> child
* \
* child
*/
paren = replace;
} else {
/**
* case:
* node replace
* \ \
* X X
* / \ / \
* paren X --> paren X
* / /
* replace child
* \
* child
*
*/
/* 建立替代结点的父亲与替换结点的右子的父子关系,即爷孙変父子 */
if (child != null) {
child.setParent(replace.getParent());
}
replace.getParent().setLeft(child);
/* 建立替代节点与待删节点的右子的父子关系 */
replace.setRight(node.getRight());
node.getRight().setParent(replace);
}

/* 待删节点的父亲设置为替代结点的父亲 */
replace.setParent(node.getParent());
/* 建立替换结点与待删节点左子的父子关系 */
replace.setLeft(node.getLeft());
node.getLeft().setParent(replace);
/* 替代结点沿用待删节点的颜色 */
replace.setColor(node.getColor());

/* 待删结点的父亲不为空,则调整左右子 */
if (node.getParent() != null) {
if (node.getParent().getLeft() == node) {
node.getParent().setLeft(replace);
} else {
node.getParent().setRight(replace);
}
}
/* 待删结点的父亲为空,则设置根结点 */
else {
this.root = replace;
}

/* 删除黑色结点需要调整平衡,红色不需要 */
if (color == RBTColor.black) {
balanceDeletion(child, paren);
}
}
/**
* case2: 待删结点没有左子和右子
* solution2: 直接删除结点
*/
else if((node.getLeft() == null) && (node.getRight() == null)) {
paren = node.getParent();
if (node == paren.getLeft()) {
paren.setLeft(node.getLeft());
} else {
paren.setRight(node.getRight());
}
}
/**
* case3: 待删结点只有左子或者右子
* solution3: 待删节点的父亲指向存在的子嗣
*/
else {
/* 确定替代结点 */
if (node.getLeft() != null) {
replace = node.getLeft();
} else {
replace = node.getRight();
}

/* 待删结点的父亲 */
paren = node.getParent();

/* 待删结点的父亲是否为空 */
if (paren != null) {
if (paren.getLeft() == node) {
paren.setLeft(replace);
} else {
paren.setRight(replace);
}
} else {
this.root = replace;
}

/* 待删节点的父亲指向替代结点 */
replace.setParent(paren);

color = node.getColor();
child = replace;

/* 删除黑色结点需要调整平衡,红色不需要 */
if (color == RBTColor.black) {
balanceDeletion(child, paren);
}
}

/* 结点数量-1 */
count.decrementAndGet();
System.out.println(getCurrentTime() + " [INFO] key为" + node.getKey() + "的结点删除成功");
}

/**
* <p>寻找继承的后裔</p>
* @param node
* @return
*/
public RBTNode<T, D> descendants(RBTNode<T, D> node) {
/* 查询大于该节点的最小结点,即右子树的最左结点 */
if (node.getRight() != null) {
RBTNode<T, D> right = node.getRight();
if (right.getLeft() == null) {
return right;
}
while (right.getLeft() != null) {
right = right.getLeft();
}
return right;
}

/* @deprecated */
RBTNode<T, D> paren = node.getParent();
while ((paren != null) && (paren.getRight() == node)) {
node = paren;
paren = paren.getParent();
}
return paren;
}


/**
* <p>删除结点的自平衡操作</p>
* <p>
* P-父亲、D-删除、B-兄弟、BR-兄弟右子、BL-兄弟左子。
*
* - D为左子
* - B为红色:左旋父亲,父亲染红,兄弟染黑,然后continue
* - B为黑色
* - BL为黑色且BR黑色:兄弟染红,父亲回溯
* - BL为红色且BR为黑色:右旋兄弟,兄弟染红,BL染黑
* - BR为红色:左旋父亲,父亲的颜色给兄弟,父亲黑化,BR黑化,然后break
* - D为右子
* - B为红色:右旋父亲,父亲染红,兄弟染黑,然后continue
* - B为黑色:
* - BL为黑色且BR为黑色:兄弟染红,父亲回溯
* - BL为红色且BR为黑色:左旋兄弟,父亲染红,BR染黑
* - BL为红色:右旋父亲,父亲的颜色给兄弟,父亲黑化,BL黑化,然后break
* </p>
*
* @param node
* @param paren
* <p>
* 入参情况:
* 1. node=替换节点 paren=替换节点的父亲节点
* 2. node=替换节点的孩子节点 paren=替换节点
* 3. node=替换节点的孩子节点 parent=替换节点的父节点
* </p>
*/
private void balanceDeletion(RBTNode<T, D> node, RBTNode<T, D> paren) {
RBTNode<T, D> broth;

while (isBlack(node) && node != this.root) {

if (paren.getLeft() == node) {
broth = paren.getRight();

/**
* case1: D为左子。B为红色
* solution1: 左旋父亲,父亲染红,兄弟染黑,然后continue
*/
if (isRed(broth)) {
leftRotate(paren);
paren.setColor(RBTColor.red);
broth.setColor(RBTColor.black);
continue;
} else {
/**
* case2: D为左子。B为黑色,BL为黑色且BR为黑色
* solution2: 兄弟染红,父亲回溯
*/
if (isBlack(broth.getLeft()) && isBlack(broth.getRight())) {
broth.setColor(RBTColor.red);
node = paren;
paren = paren.getParent();
}
/**
* case3: D为左子。B为黑色,BL为红色且BR为黑色
* solution3: 右旋兄弟,兄弟染红,BL染黑
*/
else if (isRed(broth.getLeft()) && isBlack(broth.getRight())) {
rightRotate(broth);
broth.setColor(RBTColor.red);
broth.getLeft().setColor(RBTColor.black);
}
/**
* case4: D为左子,B为黑色,BR为红色
* solution4: 左旋父亲,父亲的颜色给兄弟,父亲黑化,BR黑化,然后break
*/
else if (isRed(broth.getRight())) {
leftRotate(paren);
broth.setColor(paren.getColor());
paren.setColor(RBTColor.black);
broth.getRight().setColor(RBTColor.black);
break;
}
}
} else {
broth = paren.getLeft();

/**
* case5: D为右子。B为红色
* solution5: 右旋父亲,父亲染红,兄弟染黑,然后continue
*/
if (isRed(broth)) {
rightRotate(paren);
paren.setColor(RBTColor.red);
broth.setColor(RBTColor.black);
} else {
/**
* case6: D为右子。B为黑色,BL为黑色且BR为黑色
* solution6: 兄弟染红,父亲回溯
*/
if (isBlack(broth.getLeft()) && isBlack(broth.getRight())) {
broth.setColor(RBTColor.red);
node = paren;
paren = paren.getParent();
}
/**
* case7: D为右子。B为黑色,BL为红色且BR为黑色
* solution7: 左旋兄弟,父亲染红,BR染黑
*/
else if (isRed(broth.getLeft()) && isBlack(broth.getRight())) {
leftRotate(broth);
paren.setColor(RBTColor.red);
broth.getRight().setColor(RBTColor.black);
}
/**
* case8: D为右子,B为黑色,BR为红色
* solution8: 右旋父亲,父亲的颜色给兄弟,父亲黑化,BL黑化,然后break
*/
else if (isRed(broth.getRight())) {
rightRotate(paren);
broth.setColor(paren.getColor());
paren.setColor(RBTColor.black);
broth.getRight().setColor(RBTColor.black);
break;
}
}
}

}

/* node染成被删结点的颜色 */
node.setColor(RBTColor.black);
}

/**
* <p>层次遍历</p>
*/
public void levelOrder() {
List<List<RBTNode<T, D>>> levelList = levelOrder(this.root);
for (List<RBTNode<T, D>> list:levelList) {
for (RBTNode node : list) {
System.out.print(node.getKey() + " ");
}
System.out.println();
}
}

/**
* <p>层次遍历</p>
* @param node
* @return
*/
private List<List<RBTNode<T, D>>> levelOrder(RBTNode<T, D> node) {
List<List<RBTNode<T, D>>> res = new LinkedList<>();
Queue<RBTNode<T, D>> queue = new LinkedList<>();
queue.add(node);
while (!queue.isEmpty()) {
int count = queue.size();
List<RBTNode<T, D>> cur = new LinkedList<>();
while (count != 0) {
RBTNode<T, D> temp = queue.poll();
cur.add(temp);
if (temp.getLeft() != null) queue.add(temp.getLeft());
if (temp.getRight() != null) queue.add(temp.getRight());
count--;
}
res.add(cur);
}
return res;
}

/**
* <p>输出红黑树的层级结构</p>
*/
public void printRBTreeLevel() {
System.out.println(getCurrentTime() + " [INFO] 开始打印红黑树的层级结构");
ConcurrentHashMap<Integer, List<RBTNode>> map = showTree();
int size = map.size();

for (int i = 0; i < map.size(); i++) {
System.out.println();
for (int j = 0; j < map.get(i).size(); j++) {
System.out.print( makeSpace(size, i) +
(map.get(i).get(j).getKey() == null ? " " : (map.get(i).get(j).getKey()) + (map.get(i).get(j).getColor() == RBTColor.black ? "(黑)" : "(红)")) + makeSpace(size, i));
}
System.out.println();
}
System.out.println(getCurrentTime() + " [INFO] 红黑树的层级结构打印完毕");
}

/**
* <p>输出整棵树的Graphviz结构</p>
*/
public void printGraphviz(){
System.out.println(getCurrentTime() + " [INFO] 开始打印树的Graphviz结构");
ConcurrentHashMap<Integer, List<RBTNode>> map = showTree();
int size = map.size();
System.out.println("digraph {");
for (int i = 0; i < map.size(); i++) {
for (int j = 0; j < map.get(i).size(); j++) {
if(map.get(i).get(j).getKey() != null){
System.out.println(map.get(i).get(j).getKey() + " [color=" + (map.get(i).get(j).getColor()) + "] ");
}
}
}

for (int i = 0; i < map.size(); i++) {
for (int j = 0; j < map.get(i).size(); j++) {
if(map.get(i).get(j).getKey() != null){
if(map.get(i).get(j).getLeft() != null){
System.out.println(map.get(i).get(j).getKey() + "->" + map.get(i).get(j).getLeft().getKey() + "[label=left]");
}
if(map.get(i).get(j).getRight() != null){
System.out.println(map.get(i).get(j).getKey() + "->" + map.get(i).get(j).getRight().getKey() + "[label=right]");
}
}
}
}
System.out.println("}");
System.out.println(getCurrentTime() + " [INFO] 树的Graphviz结构打印完毕");
}

public String makeSpace(int size, int index){
StringBuilder builder = new StringBuilder();
for (int i = 0; i < 1 << (size - index); i++) {
builder.append(" ");
}
return builder.toString();
}

public ConcurrentHashMap<Integer, List<RBTNode>> showTree(){
ConcurrentHashMap<Integer, List<RBTNode>> map = new ConcurrentHashMap<>();
showTree(root, 0, map);
return map;
}

public void showTree(RBTNode root, int count, ConcurrentHashMap<Integer, List<RBTNode>> map){
if (map.get(count) == null){
map.put(count, new ArrayList<>());
}
map.get(count).add(root);

if (root.getLeft() != null){
showTree(root.getLeft(), count+1 , map);
} else{
if(map.get(count+1) == null){
map.put(count+1, new ArrayList<>());
}
map.get(count+1).add(new RBTNode(RBTColor.red, null, null, null, null, null));
}
if (root.getRight() != null){
showTree(root.getRight(), count+1 , map);
} else{
if(map.get(count+1) == null){
map.put(count+1, new ArrayList<>());
}
map.get(count+1).add(new RBTNode(RBTColor.red, null, null, null, null, null));
}
}


/**
* <p>菜单</p>
*/
public void RBT() {
RedBlackTree KTree = new RedBlackTree();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println(("┏━━━━━━━━━ ▶ ▶ ▶ ▶ ▶ RED ❤ BLACK ◀ ◀ ◀ ◀ ◀ ━━━━━━━━━━┓"));
System.out.println((" ✪━━━━━━━━━━━━━━━━━━━ 1. 插入节点 ━━━━━━━━━━━━━━━━━━━✪"));
System.out.println((" ✪━━━━━━━━━━━━━━━━━━━ 2. 查询节点 ━━━━━━━━━━━━━━━━━━━✪"));
System.out.println((" ✪━━━━━━━━━━━━━━━━━━━ 3. 删除结点 ━━━━━━━━━━━━━━━━━━━✪"));
System.out.println((" ✪━━━━━━━━━━━━━━━━━━━ 4. 查询数量 ━━━━━━━━━━━━━━━━━━━✪"));
System.out.println((" ✪━━━━━━━━━━━━━━━━━━━ 5. 层次结构 ━━━━━━━━━━━━━━━━━━━✪"));
System.out.println((" ✪━━━━━━━━━━━━━━━━━━━ 6. Graphviz ━━━━━━━━━━━━━━━━━━✪"));
System.out.println((" ✪━━━━━━━━━━━━━━━━━━━ 7. 退出系统 ━━━━━━━━━━━━━━━━━━━✪"));
System.out.println(("┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛"));

System.out.print(getCurrentTime() + " [input] 输入选择:");
int choice = scanner.nextInt();
int key;
String value;
switch (choice) {
case 1:
System.out.print(getCurrentTime() + " [INPUT] 输入键值:");
key = scanner.nextInt();
System.out.print(getCurrentTime() + " [INPUT] 输入数据:");
value = scanner.next();
KTree.add(key, value);
break;
case 2:
System.out.print(getCurrentTime() + " [INPUT] 输入键值:");
key = scanner.nextInt();
System.out.println(getCurrentTime() + " [INFO] 查询结果 value = " + KTree.get(key));
break;
case 3:
System.out.print(getCurrentTime() + " [INPUT] 输入键值:");
key = scanner.nextInt();
KTree.del(key);
break;
case 4:
System.out.println(getCurrentTime() + " [INFO] 查询结果 size = " + KTree.size());
break;
case 5:
KTree.printRBTreeLevel();
break;
case 6:
KTree.printGraphviz();
break;
case 7:
System.out.println(getCurrentTime() + " [INFO] 退出成功");
System.exit(0);
default:
System.out.println(getCurrentTime() + " [ERROR] 输入错误");
break;
}
}
}


public static void main(String[] args) {
RedBlackTree redBlackTree = new RedBlackTree();
redBlackTree.RBT();
}
}