-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRedBlackTree.java
More file actions
347 lines (331 loc) · 8.43 KB
/
Copy pathRedBlackTree.java
File metadata and controls
347 lines (331 loc) · 8.43 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
package tree;
enum Color{
RED,
BLACK
}
class Node{
int val;
Node par;
Node left;
Node right;
Color color;
Node(int val,Node par,Color color){
this.val = val;
this.par = par;
this.left = null;
this.right = null;
this.color = color;
}
}
public class RedBlackTree{
private static final Color Black = Color.BLACK;
private static final Color Red = Color.RED;
private Node root;
public boolean insert(int x){
if(root == null){
root = new Node(x,null,Black);
return true;
}else{
for(Node cur = root;;){
if (x < cur.val){ //插入小节点
if (cur.left == null){ //找到
cur.left = new Node(x,cur,Red);
insertFix(cur.left);
return true;
}else
cur = cur.left;
}else if(cur.val < x){ //插入大节点
if (cur.right == null){ //找到
cur.right = new Node(x,cur,Red);
insertFix(cur.right);
return true;
}else
cur = cur.right;
}else //重复插入
return false;
}
}
}
private Node rightRotate(Node A){
Node P = A.par;
Node B = A.left;
Node D = B.right;
if(root == A) root = B;
if(P!=null && P.left == A) P.left = B;
if(P!=null && P.right == A) P.right = B;
B.par = P;
A.par = B;
B.right = A;
A.left = D;
if(D!=null) D.par = A;
return B;
}
private Node leftRotate(Node A){
Node P = A.par;
Node C = A.right;
Node D = C.left;
if(root == A) root = C;
if(P!=null && P.left == A) P.left = C;
if(P!=null && P.right == A) P.right = C;
C.par = P;
A.par = C;
C.left = A;
A.right = D;
if(D!=null) D.par = A;
return C;
}
private void insertFix(Node cur){
if(cur.par.color == Black) return; //case0:当前红,父黑,返回
while(cur.par != null && cur.par.color == Red){
Node P = cur.par;
Node G = cur.par.par;
Node U = P == G.left ? G.right : G.left;
if(U != null && U.color == Red){//case1:当前红,父红,叔红
P.color = Black;//case1: 父变黑
U.color = Black;//case1: 叔变黑
G.color = Red;//case1: 祖变红
cur = G;//case1: 递归祖
}else{//case2 or 3
if(P == G.left){//父是左儿子
if(cur == P.right){//case2: 当前红,父红,叔黑,当前是右儿子
cur = P; //case2: 父设为当前
leftRotate(cur); //case2:左旋父节点 转换为case3
}else{
//case3: 当前红,父红,叔黑,当前是左儿子
P.color = Black; //case3: 父变黑
G.color = Red; //case3: 祖变红
rightRotate(G); //case3: 右旋祖父节点
}
}else{//父是右儿子,左右互换
if(cur == P.left){
cur = P;
rightRotate(cur);
}else{
P.color = Black;
G.color = Red;
leftRotate(G);
}
}
}
}
root.color = Black;
}
public boolean delete(int x){
Node del = root;
while(del != null && del.val != x){
if(x < del.val)
del = del.left;
else if(del.val < x)
del = del.right;
}
if(del == null)
return false;
else{
Node P = del.par;
Node next = null;
if(del.left == null && del.right == null){//叶子节点
next = null; //接替节点为null
if(P == null) root = null;
if(P != null && P.left == del){ //是父左儿子
P.left = null;
}else if(P != null && P.right == del){ //是父右儿子
P.right = null;
}
del.par = null; //去掉父节点
}else if (del.left != null && del.right == null){//只有左儿子
next = del.left;
if(P == null) root = next;
if(P != null && P.left == del){//是父左儿子
P.left = del.left;
}else if(P != null && P.right == del){//是父右儿子
P.right = del.left;
}
del.left.par = P;
del.par = null;
del.left = null;
}else if (del.right != null && del.left == null){//只有右儿子
next = del.right;
if(P == null) root = next;
if(P != null && P.left == del){
P.left = del.right;
}else if(P != null && P.right == del){
P.right = del.right;
}
del.right.par = P;
del.par = null;
del.right = null;
}else{ //del.right != null && del.left != null
Node min = findMin(del.right);
del.val = min.val;
next = min.right;
P = min.par;
if (P==del)
P.right = min.right;
else
P.left = min.right;
if (next != null){
next.par = P;
}
min.right = null;
min.par = null;
del = min;
}
//min是一个孤独的点,已经被删了,next是一个补位点 :所以 next->del 红+黑 或 黑+黑
if (!isBlack(next) && isBlack(del)){
next.color = Black;
}else if (isBlack(next) && isBlack(del) && P != null){
deleteFix(next,P);
}
//any+红 不用
return true;
}
}
private boolean isBlack(Node x){
return x==null || x.color == Black;
}
private void deleteFix(Node cur,Node Par){
while(cur != root && isBlack(cur)){
Node P = (cur == null) ? Par : cur.par; //cur不是根 P肯定存在
Node B = null;
Node C1 = null;
Node C2 = null;
if(P.left == cur){ //cur是P的左儿子
B = P.right;
if(!isBlack(B)){//case1:变成case2
B.color = Black; //case1:兄弟变黑
P.color = Red;//case1:父变红
leftRotate(P);//case1:父左旋
//System.out.println("case1");
}else{//case2,3,4
C1 = B.left; //大侄子
C2 = B.right; //二侄子
if(isBlack(C1) && isBlack(C2)){//case2:递归父
B.color = Red; //case2:兄弟变红
cur = P; //case2:递归P
//System.out.println("case2");
}else if((!isBlack(C1)) && isBlack(C2)){//case3:变为case4 C1肯定为Red
C1.color = Black; //case3:大侄子变黑
B.color = Red; //case3:兄弟变红
rightRotate(B); //case3:兄弟右旋
//System.out.println("case3");
}else{ //case4:C1随意,C2为红
B.color = P.color; //case4:兄变父
P.color = Black; //case4:父变黑
C2.color = Black; //case4:二侄子变黑
leftRotate(P); //case4:父左旋
cur = root; //算法结束
//System.out.println("case4");
}
}
}else{//cur是P的右儿子
B = P.left;
if(!isBlack(B)){//case1:变成case2
B.color = Black; //case1:兄弟变黑
P.color = Red;//case1:父变红
rightRotate(P);//case1:父右旋
}else{//case2,3,4
C1 = B.right; //小侄子
C2 = B.left; //大侄子
if(isBlack(C1) && isBlack(C2)){//case2:递归父
B.color = Red; //case2:兄弟变红
cur = P; //case2:递归P
}else if((!isBlack(C1)) && isBlack(C2)){//case3:变为case4 C1肯定为Red
C1.color = Black; //case3:大侄子变黑
B.color = Red; //case3:兄弟变红
leftRotate(B); //case3:兄弟左旋
}else{ //case4:C1随意,C2为红
B.color = P.color; //case4:兄变父
P.color = Black; //case4:父变黑
C2.color = Black; //case4:二侄子变黑
rightRotate(P); //case4:父右旋
cur = root; //算法结束
}
}
}
}
cur.color = Black;
}
public Node findMin(Node root){
if (root == null) return null;
Node ans = root;
while(ans.left!=null)
ans = ans.left;
return ans;
}
public Node findMax(Node root){
if (root == null) return null;
Node ans = root;
while(ans.right!=null)
ans = ans.right;
return ans;
}
public Node find(int x){
Node ans = root;
while(ans!=null){
if (x < ans.val)
ans = ans.left;
else if(ans.val < x)
ans = ans.right;
else
return ans;
}
return null;
}
private void print(Node root,int dep){
if (root == null) return;
print(root.right,dep+1);
for(int i=0; i<dep; i++){
System.out.print('\t');
}
System.out.println(root.color + "_" + root.val);
print(root.left,dep+1);
}
public void print(){
print(root,0);
}
public static void main(String[] args) {
RedBlackTree tree = new RedBlackTree();
tree.insert(12);
tree.insert(1);
tree.insert(9);
tree.insert(2);
tree.insert(0);
tree.insert(11);
tree.insert(7);
tree.insert(19);
tree.insert(4);
tree.insert(15);
tree.insert(18);
tree.insert(5);
tree.insert(14);
tree.insert(13);
tree.insert(10);
tree.insert(16);
tree.insert(6);
tree.insert(3);
tree.insert(8);
tree.insert(17);
tree.print();
tree.delete(12);
tree.delete(1);
tree.delete(9);
tree.delete(2);
tree.delete(0);
tree.delete(11);
tree.delete(7);
tree.delete(19);
tree.delete(4);
tree.delete(15);
tree.delete(18);
tree.delete(5);
tree.delete(14);
tree.delete(13);
tree.delete(10);
tree.delete(16);
tree.delete(6);
tree.delete(3);
tree.delete(8);
tree.delete(17);
tree.print();
}
}