1
1
package org .easyarch .myutils .algorithm .struct .tree .btree ;
2
2
3
+ import com .sun .org .apache .bcel .internal .generic .IF_ACMPEQ ;
3
4
import com .sun .org .apache .regexp .internal .RE ;
4
5
5
6
/**
@@ -9,14 +10,15 @@ public class RBTree<E extends Comparable> {
9
10
10
11
private static final boolean RED = false ;
11
12
12
- private static final boolean BLACK = false ;
13
+ private static final boolean BLACK = true ;
13
14
14
15
//所有叶子节点默认都是nil节点,黑色
15
- private final RBNode NIL = new RBNode <>(null ,null ,null ,null ,true );
16
+ private final RBNode NIL = new RBNode <>(null ,null ,null ,null ,BLACK );
16
17
17
18
private RBNode <E > root ;
18
19
19
20
public void add (E elem ){
21
+ System .out .println ("add elem:" +elem );
20
22
if (elem == null ){
21
23
return ;
22
24
}
@@ -35,39 +37,166 @@ private void add(RBNode<E> currentNode,E elem){
35
37
return ;
36
38
}
37
39
if (elem .compareTo (currentNode .elem ) > 0 ){
38
- if (currentNode .right == null ){
40
+ if (currentNode .right == NIL ){
39
41
currentNode .right = new RBNode (NIL ,elem ,NIL ,currentNode , RED );
40
-
42
+ rebalance ( currentNode . right );
41
43
}else {
42
44
add (currentNode .right ,elem );
43
45
}
44
46
}else if (elem .compareTo (currentNode .elem ) < 0 ){
45
- if (currentNode .left == null ){
47
+ if (currentNode .left == NIL ){
46
48
currentNode .left = new RBNode (NIL ,elem ,NIL ,currentNode ,RED );
47
-
49
+ rebalance ( currentNode . left );
48
50
}else {
49
51
add (currentNode .left ,elem );
50
52
}
51
53
}
52
54
}
53
55
56
+ /**
57
+ * @param currentNode 表示当前判定节点
58
+ */
54
59
private void rebalance (RBNode <E > currentNode ){
55
- //父亲节点为黑色则不作任何处理
56
- if (currentNode .color ){
60
+ if (currentNode == this .root ) {
61
+ //根节点为红色
62
+ if (!this .root .color ){
63
+ this .root .color = BLACK ;
64
+ }
57
65
return ;
58
66
}
59
- if (currentNode .parent != null ){
60
- RBNode <E > cParent = currentNode .parent ;
61
- if (currentNode == cParent .left ){
62
- RBNode <E > cUncle = cParent .right ;
63
- }else if (currentNode == cParent .right ){
64
- RBNode <E > cUncle = cParent .left ;
67
+ RBNode <E > cParent = currentNode .parent ;
68
+ if (cParent == null ){
69
+ System .out .println ("cParent is null,root is" +this .root .elem +",current is:" +currentNode .elem );
70
+ }
71
+ //父节点为黑色
72
+ if (cParent .color ){
73
+ return ;
74
+ }
75
+ if (cParent != null ){
76
+ //黑父,无需处理
77
+ if (cParent .color ){
78
+ return ;
79
+ }
80
+ RBNode <E > cUncle ;
81
+ RBNode <E > cGrand = cParent .parent ;
82
+ if (cGrand .left == cParent ){
83
+ cUncle = cGrand .right ;
65
84
}else {
66
-
85
+ cUncle = cGrand .left ;
86
+ }
87
+ //叔节点是红色
88
+ if (!cUncle .color ){
89
+ cParent .color = BLACK ;
90
+ cGrand .color = RED ;
91
+ cUncle .color = BLACK ;
92
+ rebalance (cGrand );
93
+ return ;
94
+ }
95
+ //叔节点是黑色
96
+ if (cParent == cGrand .left ){
97
+ //父节点在祖节点左边
98
+ if (currentNode == cParent .left ){
99
+ //新节点在父节点左边,此时右旋。祖父变红,父亲变黑,当前节点仍然是红色
100
+ currentNode .color = RED ;
101
+ cGrand .color = RED ;
102
+ cParent .color = BLACK ;
103
+ rightRotate (cGrand );
104
+ }else {
105
+ //新节点在父节点右边,先左旋再右旋。祖父变红,当前节点变黑,父亲仍然是红色
106
+ cParent .color = RED ;
107
+ cGrand .color = RED ;
108
+ currentNode .color = BLACK ;
109
+ leftRotate (cParent );
110
+ rightRotate (cGrand );
111
+ }
112
+ }else {
113
+ //父节点在祖节点右边
114
+ if (currentNode == cParent .right ){
115
+ //新节点在父节点右边,此时左旋.
116
+ cGrand .color = RED ;
117
+ cParent .color = BLACK ;
118
+ currentNode .color = RED ;
119
+ leftRotate (cGrand );
120
+ }else {
121
+ //新节点在父节点左边,先右旋再左旋
122
+ cParent .color = RED ;
123
+ cGrand .color = RED ;
124
+ currentNode .color = BLACK ;
125
+ rightRotate (cParent );
126
+ leftRotate (cGrand );
127
+ }
67
128
}
68
-
69
129
}
70
130
}
131
+ //
132
+ // private void rebalance(RBNode<E> currentNode,RBNode<E> newNode){
133
+ // //父亲节点为黑色则不作任何处理
134
+ // if (currentNode.color){
135
+ // return;
136
+ // }
137
+ // if (currentNode == this.root){
138
+ // //根节点为红色
139
+ // if (!this.root.color){
140
+ // this.root.color = BLACK;
141
+ // }
142
+ // }
143
+ // if (currentNode.parent != null){
144
+ // RBNode<E> cParent = currentNode.parent;
145
+ // //黑父,无需处理
146
+ // if (currentNode.color){
147
+ // return ;
148
+ // }
149
+ // //父是红色,此时要看叔节点的颜色
150
+ // RBNode<E> cUncle;
151
+ // if (currentNode == cParent.left){
152
+ // cUncle = cParent.right;
153
+ // }else{
154
+ // cUncle = cParent.left;
155
+ // }
156
+ // //叔节点是红色
157
+ // if (!cUncle.color){
158
+ // currentNode.color = BLACK;
159
+ // cParent.color = RED;
160
+ // cUncle.color = BLACK;
161
+ // rebalance(cParent,newNode);
162
+ // return;
163
+ // }
164
+ // //叔节点是黑色
165
+ // if (currentNode == cParent.left){
166
+ // //父节点在祖节点左边
167
+ // if (newNode == currentNode.left){
168
+ // //新节点在父节点左边,此时右旋。祖父变红,父亲变黑,当前节点仍然是红色
169
+ // cParent.color = RED;
170
+ // currentNode.color= BLACK;
171
+ // newNode.color = RED;
172
+ // rightRotate(cParent);
173
+ // }else{
174
+ // //新节点在父节点右边,先左旋再右旋。祖父变红,当前节点变黑,父亲仍然是红色
175
+ // cParent.color = RED;
176
+ // newNode.color = BLACK;
177
+ // currentNode.color = RED;
178
+ // leftRotate(currentNode);
179
+ // rightRotate(cParent);
180
+ // }
181
+ // }else{
182
+ // //父节点在祖节点右边
183
+ // if(newNode == currentNode.right){
184
+ // //新节点在父节点右边,此时左旋.
185
+ // cParent.color = RED;
186
+ // currentNode.color= BLACK;
187
+ // newNode.color = RED;
188
+ // leftRotate(cParent);
189
+ // }else{
190
+ // //新节点在父节点左边,先右旋再左旋
191
+ // cParent.color = RED;
192
+ // newNode.color = BLACK;
193
+ // currentNode.color = RED;
194
+ // rightRotate(currentNode);
195
+ // leftRotate(cParent);
196
+ // }
197
+ // }
198
+ // }
199
+ // }
71
200
72
201
private void leftRotate (RBNode <E > currentNode ){
73
202
RBNode <E > cRight = currentNode .right ;
@@ -83,15 +212,15 @@ private void leftRotate(RBNode<E> currentNode){
83
212
cRight .parent = cParent ;
84
213
}else {
85
214
//当前节点为根节点的情况
86
- cRight .parent = NIL ;
215
+ cRight .parent = null ;
87
216
this .root = cRight ;
88
217
}
89
218
//当前节点包括一个左分支的情况
90
219
if (cRight .left != null ){
91
220
currentNode .right = cRight .left ;
92
221
cRight .left .parent = currentNode ;
93
222
}else {
94
- currentNode .right = null ;
223
+ currentNode .right = NIL ;
95
224
}
96
225
cRight .left = currentNode ;
97
226
currentNode .parent = cRight ;
@@ -116,12 +245,25 @@ private void rightRotate(RBNode<E> currentNode){
116
245
currentNode .left = cLeft .right ;
117
246
cLeft .right .parent = currentNode ;
118
247
}else {
119
- currentNode .parent = null ;
248
+ currentNode .parent = NIL ;
120
249
}
121
250
cLeft .right = currentNode ;
122
251
currentNode .parent = cLeft ;
123
252
}
124
253
254
+ public void iterate (){
255
+ iterate (root );
256
+ }
257
+
258
+ private void iterate (RBNode node ){
259
+ if (node == null ){
260
+ return ;
261
+ }
262
+ System .out .println (node .elem );
263
+ iterate (node .left );
264
+ iterate (node .right );
265
+ }
266
+
125
267
private class RBNode <E extends Comparable > {
126
268
127
269
private RBNode <E > left ;
0 commit comments