7
7
*/
8
8
public class RedBlackBST {
9
9
10
- private final int red = 0 ;
11
- private final int black = 1 ;
10
+ private static final int RED = 0 ;
11
+ private static final int BLACK = 1 ;
12
12
13
13
private class Node {
14
14
15
15
int key = -1 ;
16
- int color = black ;
16
+ int color = BLACK ;
17
17
Node left = nil ;
18
18
Node right = nil ;
19
19
Node p = nil ;
@@ -31,15 +31,15 @@ public void printTree(Node node) {
31
31
return ;
32
32
}
33
33
printTree (node .left );
34
- System .out .print (((node .color == red ) ? " R " : " B " ) + "Key: " + node .key + " Parent: " + node .p .key + "\n " );
34
+ System .out .print (((node .color == RED ) ? " R " : " B " ) + "Key: " + node .key + " Parent: " + node .p .key + "\n " );
35
35
printTree (node .right );
36
36
}
37
37
38
38
public void printTreepre (Node node ) {
39
39
if (node == nil ) {
40
40
return ;
41
41
}
42
- System .out .print (((node .color == red ) ? " R " : " B " ) + "Key: " + node .key + " Parent: " + node .p .key + "\n " );
42
+ System .out .print (((node .color == RED ) ? " R " : " B " ) + "Key: " + node .key + " Parent: " + node .p .key + "\n " );
43
43
printTreepre (node .left );
44
44
printTreepre (node .right );
45
45
}
@@ -66,10 +66,10 @@ private void insert(Node node) {
66
66
Node temp = root ;
67
67
if (root == nil ) {
68
68
root = node ;
69
- node .color = black ;
69
+ node .color = BLACK ;
70
70
node .p = nil ;
71
71
} else {
72
- node .color = red ;
72
+ node .color = RED ;
73
73
while (true ) {
74
74
if (node .key < temp .key ) {
75
75
if (temp .left == nil ) {
@@ -94,44 +94,44 @@ private void insert(Node node) {
94
94
}
95
95
96
96
private void fixTree (Node node ) {
97
- while (node .p .color == red ) {
97
+ while (node .p .color == RED ) {
98
98
Node y = nil ;
99
99
if (node .p == node .p .p .left ) {
100
100
y = node .p .p .right ;
101
101
102
- if (y != nil && y .color == red ) {
103
- node .p .color = black ;
104
- y .color = black ;
105
- node .p .p .color = red ;
102
+ if (y != nil && y .color == RED ) {
103
+ node .p .color = BLACK ;
104
+ y .color = BLACK ;
105
+ node .p .p .color = RED ;
106
106
node = node .p .p ;
107
107
continue ;
108
108
}
109
109
if (node == node .p .right ) {
110
110
node = node .p ;
111
111
rotateLeft (node );
112
112
}
113
- node .p .color = black ;
114
- node .p .p .color = red ;
113
+ node .p .color = BLACK ;
114
+ node .p .p .color = RED ;
115
115
rotateRight (node .p .p );
116
116
} else {
117
117
y = node .p .p .left ;
118
- if (y != nil && y .color == red ) {
119
- node .p .color = black ;
120
- y .color = black ;
121
- node .p .p .color = red ;
118
+ if (y != nil && y .color == RED ) {
119
+ node .p .color = BLACK ;
120
+ y .color = BLACK ;
121
+ node .p .p .color = RED ;
122
122
node = node .p .p ;
123
123
continue ;
124
124
}
125
125
if (node == node .p .left ) {
126
126
node = node .p ;
127
127
rotateRight (node );
128
128
}
129
- node .p .color = black ;
130
- node .p .p .color = red ;
129
+ node .p .color = BLACK ;
130
+ node .p .p .color = RED ;
131
131
rotateLeft (node .p .p );
132
132
}
133
133
}
134
- root .color = black ;
134
+ root .color = BLACK ;
135
135
}
136
136
137
137
void rotateLeft (Node node ) {
@@ -234,67 +234,67 @@ boolean delete(Node z) {
234
234
y .left .p = y ;
235
235
y .color = z .color ;
236
236
}
237
- if (yorigcolor == black ) {
237
+ if (yorigcolor == BLACK ) {
238
238
deleteFixup (x );
239
239
}
240
240
return true ;
241
241
}
242
242
243
243
void deleteFixup (Node x ) {
244
- while (x != root && x .color == black ) {
244
+ while (x != root && x .color == BLACK ) {
245
245
if (x == x .p .left ) {
246
246
Node w = x .p .right ;
247
- if (w .color == red ) {
248
- w .color = black ;
249
- x .p .color = red ;
247
+ if (w .color == RED ) {
248
+ w .color = BLACK ;
249
+ x .p .color = RED ;
250
250
rotateLeft (x .p );
251
251
w = x .p .right ;
252
252
}
253
- if (w .left .color == black && w .right .color == black ) {
254
- w .color = red ;
253
+ if (w .left .color == BLACK && w .right .color == BLACK ) {
254
+ w .color = RED ;
255
255
x = x .p ;
256
256
continue ;
257
- } else if (w .right .color == black ) {
258
- w .left .color = black ;
259
- w .color = red ;
257
+ } else if (w .right .color == BLACK ) {
258
+ w .left .color = BLACK ;
259
+ w .color = RED ;
260
260
rotateRight (w );
261
261
w = x .p .right ;
262
262
}
263
- if (w .right .color == red ) {
263
+ if (w .right .color == RED ) {
264
264
w .color = x .p .color ;
265
- x .p .color = black ;
266
- w .right .color = black ;
265
+ x .p .color = BLACK ;
266
+ w .right .color = BLACK ;
267
267
rotateLeft (x .p );
268
268
x = root ;
269
269
}
270
270
} else {
271
271
Node w = x .p .left ;
272
- if (w .color == red ) {
273
- w .color = black ;
274
- x .p .color = red ;
272
+ if (w .color == RED ) {
273
+ w .color = BLACK ;
274
+ x .p .color = RED ;
275
275
rotateRight (x .p );
276
276
w = x .p .left ;
277
277
}
278
- if (w .right .color == black && w .left .color == black ) {
279
- w .color = red ;
278
+ if (w .right .color == BLACK && w .left .color == BLACK ) {
279
+ w .color = RED ;
280
280
x = x .p ;
281
281
continue ;
282
- } else if (w .left .color == black ) {
283
- w .right .color = black ;
284
- w .color = red ;
282
+ } else if (w .left .color == BLACK ) {
283
+ w .right .color = BLACK ;
284
+ w .color = RED ;
285
285
rotateLeft (w );
286
286
w = x .p .left ;
287
287
}
288
- if (w .left .color == red ) {
288
+ if (w .left .color == RED ) {
289
289
w .color = x .p .color ;
290
- x .p .color = black ;
291
- w .left .color = black ;
290
+ x .p .color = BLACK ;
291
+ w .left .color = BLACK ;
292
292
rotateRight (x .p );
293
293
x = root ;
294
294
}
295
295
}
296
296
}
297
- x .color = black ;
297
+ x .color = BLACK ;
298
298
}
299
299
300
300
public void insertDemo () {
0 commit comments