@@ -53,7 +53,6 @@ public RedBlackTree(INodeCreator<T> creator) {
53
53
@ Override
54
54
protected Node <T > addValue (T id ) {
55
55
RedBlackNode <T > nodeAdded = null ;
56
- boolean added = false ;
57
56
if (root == null ) {
58
57
// Case 1 - The current node is at the root of the tree.
59
58
@@ -63,7 +62,6 @@ protected Node<T> addValue(T id) {
63
62
root .greater = this .creator .createNewNode (root , null );
64
63
65
64
nodeAdded = (RedBlackNode <T >) root ;
66
- added = true ;
67
65
} else {
68
66
// Insert node like a BST would
69
67
Node <T > node = root ;
@@ -77,7 +75,6 @@ protected Node<T> addValue(T id) {
77
75
node .greater = this .creator .createNewNode (node , null );
78
76
79
77
nodeAdded = (RedBlackNode <T >) node ;
80
- added = true ;
81
78
break ;
82
79
} else if (id .compareTo (node .id ) <= 0 ) {
83
80
node = node .lesser ;
@@ -87,7 +84,7 @@ protected Node<T> addValue(T id) {
87
84
}
88
85
}
89
86
90
- if (added == true ) {
87
+ if (nodeAdded != null ) {
91
88
balanceAfterInsert (nodeAdded );
92
89
size ++;
93
90
}
@@ -119,7 +116,7 @@ private void balanceAfterInsert(RedBlackNode<T> begin) {
119
116
}
120
117
121
118
RedBlackNode <T > grandParent = node .getGrandParent ();
122
- RedBlackNode <T > uncle = node .getUncle ();
119
+ RedBlackNode <T > uncle = node .getUncle (grandParent );
123
120
if (parent .color == RED && uncle .color == RED ) {
124
121
// Case 3 - If both the parent and the uncle are red, then both of
125
122
// them can be repainted black and the grandparent becomes
@@ -131,43 +128,44 @@ private void balanceAfterInsert(RedBlackNode<T> begin) {
131
128
grandParent .color = RED ;
132
129
balanceAfterInsert (grandParent );
133
130
}
134
- } else {
135
- if (parent .color == RED && uncle .color == BLACK ) {
136
- // Case 4 - The parent is red but the uncle is black; also, the
137
- // current node is the right child of parent, and parent in turn
138
- // is the left child of its parent grandparent.
139
- if (node .equals (parent .greater ) && parent .equals (grandParent .lesser )) {
140
- // right-left
141
- rotateLeft (parent );
142
- node = (RedBlackNode <T >) node .lesser ;
143
-
144
- grandParent = node .getGrandParent ();
145
- parent = (RedBlackNode <T >) node .parent ;
146
- uncle = node .getUncle ();
147
- } else if (node .equals (parent .lesser ) && parent .equals (grandParent .greater )) {
148
- // left-right
149
- rotateRight (parent );
150
- node = (RedBlackNode <T >) node .greater ;
151
-
152
- grandParent = node .getGrandParent ();
153
- parent = (RedBlackNode <T >) node .parent ;
154
- uncle = node .getUncle ();
155
- }
131
+ return ;
132
+ }
133
+
134
+ if (parent .color == RED && uncle .color == BLACK ) {
135
+ // Case 4 - The parent is red but the uncle is black; also, the
136
+ // current node is the right child of parent, and parent in turn
137
+ // is the left child of its parent grandparent.
138
+ if (node .equals (parent .greater ) && parent .equals (grandParent .lesser )) {
139
+ // right-left
140
+ rotateLeft (parent );
141
+
142
+ node = (RedBlackNode <T >) node .lesser ;
143
+ parent = (RedBlackNode <T >) node .parent ;
144
+ grandParent = node .getGrandParent ();
145
+ uncle = node .getUncle (grandParent );
146
+ } else if (node .equals (parent .lesser ) && parent .equals (grandParent .greater )) {
147
+ // left-right
148
+ rotateRight (parent );
149
+
150
+ node = (RedBlackNode <T >) node .greater ;
151
+ parent = (RedBlackNode <T >) node .parent ;
152
+ grandParent = node .getGrandParent ();
153
+ uncle = node .getUncle (grandParent );
156
154
}
155
+ }
157
156
158
- if (parent .color == RED && uncle .color == BLACK ) {
159
- // Case 5 - The parent is red but the uncle is black, the
160
- // current node is the left child of parent, and parent is the
161
- // left child of its parent G.
162
- parent .color = BLACK ;
163
- grandParent .color = RED ;
164
- if (node .equals (parent .lesser ) && parent .equals (grandParent .lesser )) {
165
- // left-left
166
- rotateRight (grandParent );
167
- } else if (node .equals (parent .greater ) && parent .equals (grandParent .greater )) {
168
- // right-right
169
- rotateLeft (grandParent );
170
- }
157
+ if (parent .color == RED && uncle .color == BLACK ) {
158
+ // Case 5 - The parent is red but the uncle is black, the
159
+ // current node is the left child of parent, and parent is the
160
+ // left child of its parent G.
161
+ parent .color = BLACK ;
162
+ grandParent .color = RED ;
163
+ if (node .equals (parent .lesser ) && parent .equals (grandParent .lesser )) {
164
+ // left-left
165
+ rotateRight (grandParent );
166
+ } else if (node .equals (parent .greater ) && parent .equals (grandParent .greater )) {
167
+ // right-right
168
+ rotateLeft (grandParent );
171
169
}
172
170
}
173
171
}
@@ -287,11 +285,13 @@ private boolean balanceAfterDelete(RedBlackNode<T> node) {
287
285
sibling .color = BLACK ;
288
286
if (node .equals (parent .lesser )) {
289
287
rotateLeft (parent );
288
+
290
289
// Rotation, need to update parent/sibling
291
290
parent = (RedBlackNode <T >) node .parent ;
292
291
sibling = node .getSibling ();
293
292
} else if (node .equals (parent .greater )) {
294
293
rotateRight (parent );
294
+
295
295
// Rotation, need to update parent/sibling
296
296
parent = (RedBlackNode <T >) node .parent ;
297
297
sibling = node .getSibling ();
@@ -301,64 +301,73 @@ private boolean balanceAfterDelete(RedBlackNode<T> node) {
301
301
}
302
302
}
303
303
304
- if (parent .color == BLACK && sibling .color == BLACK
304
+ if (parent .color == BLACK
305
+ && sibling .color == BLACK
305
306
&& ((RedBlackNode <T >) sibling .lesser ).color == BLACK
306
307
&& ((RedBlackNode <T >) sibling .greater ).color == BLACK
307
308
) {
308
309
// Case 3 - parent, sibling, and sibling's children are black.
309
310
sibling .color = RED ;
310
- boolean result = balanceAfterDelete (parent );
311
- if (!result ) return false ;
312
- } else if (parent .color == RED && sibling .color == BLACK
313
- && ((RedBlackNode <T >) sibling .lesser ).color == BLACK
314
- && ((RedBlackNode <T >) sibling .greater ).color == BLACK
311
+ return balanceAfterDelete (parent );
312
+ }
313
+
314
+ if (parent .color == RED
315
+ && sibling .color == BLACK
316
+ && ((RedBlackNode <T >) sibling .lesser ).color == BLACK
317
+ && ((RedBlackNode <T >) sibling .greater ).color == BLACK
315
318
) {
316
319
// Case 4 - sibling and sibling's children are black, but parent is red.
317
320
sibling .color = RED ;
318
321
parent .color = BLACK ;
319
- } else {
320
- if (sibling .color == BLACK ) {
321
- // Case 5 - sibling is black, sibling's left child is red,
322
- // sibling's right child is black, and node is the left child of
323
- // its parent.
324
- if (node .equals (parent .lesser )
325
- && ((RedBlackNode <T >) sibling .lesser ).color == RED
326
- && ((RedBlackNode <T >) sibling .greater ).color == BLACK
327
- ) {
328
- sibling .color = RED ;
329
- ((RedBlackNode <T >) sibling .lesser ).color = RED ;
330
- rotateRight (sibling );
331
- // Rotation, need to update parent/sibling
332
- parent = (RedBlackNode <T >) node .parent ;
333
- sibling = node .getSibling ();
334
- } else if (node .equals (parent .greater )
335
- && ((RedBlackNode <T >) sibling .lesser ).color == BLACK
336
- && ((RedBlackNode <T >) sibling .greater ).color == RED
337
- ) {
338
- sibling .color = RED ;
339
- ((RedBlackNode <T >) sibling .greater ).color = RED ;
340
- rotateLeft (sibling );
341
- // Rotation, need to update parent/sibling
342
- parent = (RedBlackNode <T >) node .parent ;
343
- sibling = node .getSibling ();
344
- }
345
- }
322
+ return true ;
323
+ }
346
324
347
- // Case 6 - sibling is black, sibling's right child is red, and node
348
- // is the left child of its parent.
349
- sibling .color = parent .color ;
350
- parent .color = BLACK ;
351
- if (node .equals (parent .lesser )) {
352
- ((RedBlackNode <T >) sibling .greater ).color = BLACK ;
353
- rotateLeft (node .parent );
354
- } else if (node .equals (parent .greater )) {
355
- ((RedBlackNode <T >) sibling .lesser ).color = BLACK ;
356
- rotateRight (node .parent );
357
- } else {
358
- System .err .println ("Yikes! I'm not related to my parent. " + node .toString ());
359
- return false ;
325
+ if (sibling .color == BLACK ) {
326
+ // Case 5 - sibling is black, sibling's left child is red,
327
+ // sibling's right child is black, and node is the left child of
328
+ // its parent.
329
+ if (node .equals (parent .lesser )
330
+ && ((RedBlackNode <T >) sibling .lesser ).color == RED
331
+ && ((RedBlackNode <T >) sibling .greater ).color == BLACK
332
+ ) {
333
+ sibling .color = RED ;
334
+ ((RedBlackNode <T >) sibling .lesser ).color = RED ;
335
+
336
+ rotateRight (sibling );
337
+
338
+ // Rotation, need to update parent/sibling
339
+ parent = (RedBlackNode <T >) node .parent ;
340
+ sibling = node .getSibling ();
341
+ } else if (node .equals (parent .greater )
342
+ && ((RedBlackNode <T >) sibling .lesser ).color == BLACK
343
+ && ((RedBlackNode <T >) sibling .greater ).color == RED
344
+ ) {
345
+ sibling .color = RED ;
346
+ ((RedBlackNode <T >) sibling .greater ).color = RED ;
347
+
348
+ rotateLeft (sibling );
349
+
350
+ // Rotation, need to update parent/sibling
351
+ parent = (RedBlackNode <T >) node .parent ;
352
+ sibling = node .getSibling ();
360
353
}
361
354
}
355
+
356
+ // Case 6 - sibling is black, sibling's right child is red, and node
357
+ // is the left child of its parent.
358
+ sibling .color = parent .color ;
359
+ parent .color = BLACK ;
360
+ if (node .equals (parent .lesser )) {
361
+ ((RedBlackNode <T >) sibling .greater ).color = BLACK ;
362
+ rotateLeft (node .parent );
363
+ } else if (node .equals (parent .greater )) {
364
+ ((RedBlackNode <T >) sibling .lesser ).color = BLACK ;
365
+ rotateRight (node .parent );
366
+ } else {
367
+ System .err .println ("Yikes! I'm not related to my parent. " + node .toString ());
368
+ return false ;
369
+ }
370
+
362
371
return true ;
363
372
}
364
373
@@ -449,17 +458,21 @@ protected RedBlackNode<T> getGrandParent() {
449
458
return (RedBlackNode <T >) parent .parent ;
450
459
}
451
460
452
- protected RedBlackNode <T > getUncle () {
453
- RedBlackNode <T > grandParent = getGrandParent ();
461
+ protected RedBlackNode <T > getUncle (RedBlackNode <T > grandParent ) {
454
462
if (grandParent == null ) return null ;
455
- if (grandParent .lesser != null && grandParent .lesser . equals ( parent ) ) {
463
+ if (grandParent .lesser != null && grandParent .lesser == parent ) {
456
464
return (RedBlackNode <T >) grandParent .greater ;
457
- } else if (grandParent .greater != null && grandParent .greater . equals ( parent ) ) {
465
+ } else if (grandParent .greater != null && grandParent .greater == parent ) {
458
466
return (RedBlackNode <T >) grandParent .lesser ;
459
467
}
460
468
return null ;
461
469
}
462
470
471
+ protected RedBlackNode <T > getUncle () {
472
+ RedBlackNode <T > grandParent = getGrandParent ();
473
+ return getUncle (grandParent );
474
+ }
475
+
463
476
protected RedBlackNode <T > getSibling () {
464
477
if (parent == null ) return null ;
465
478
if (parent .lesser .equals (this )) {
0 commit comments