@@ -42,7 +42,12 @@ public Node getRight() {
42
42
43
43
@ Override
44
44
public String getText () {
45
- return String .valueOf (value );
45
+ return String .valueOf (value ) + "(" +(color ==RED ?"r" :"b" )+")" ;
46
+ }
47
+
48
+ @ Override
49
+ public String toString () {
50
+ return getText ();
46
51
}
47
52
48
53
}
@@ -111,7 +116,7 @@ public boolean insert(T value) {
111
116
112
117
for (Node node = root ;;) {
113
118
114
- int cmp = node . value .compareTo (value );
119
+ int cmp = value .compareTo (node . value );
115
120
116
121
// Left subtree.
117
122
if (cmp < 0 ) {
@@ -174,23 +179,36 @@ private void insertionRelabel(Node node) {
174
179
175
180
} else {
176
181
182
+ // System.out.println("HERE");
183
+
177
184
// Left-left case.
178
185
if (parentIsLeftChild && nodeIsLeftChild ) {
186
+ System .out .println ("Left-left case." );
179
187
grandParent = leftLeftCase (grandParent );
180
188
181
189
// Left-right case.
182
190
} else if (parentIsLeftChild && nodeIsRightChild ) {
191
+
192
+ System .out .println ("Left-right case" );
183
193
grandParent = leftRightCase (grandParent );
184
-
194
+ // System.out.println("GP: " + grandParent.getText());
195
+ // System.out.println( (grandParent.left==null?"null":grandParent.left.getText()) + " - " + (grandParent.right==null?"null":grandParent.right.getText()));
196
+
185
197
// Right-left case.
186
198
} else if (parentIsRightChild && nodeIsLeftChild ) {
199
+ System .out .println ("Right-left case." );
187
200
grandParent = rightLeftCase (grandParent );
188
201
189
202
// Right-right case.
190
203
} else {
204
+ System .out .println ("Right-right case." );
191
205
grandParent = rightRightCase (grandParent );
192
206
}
193
207
208
+ if (grandParent .parent == null ) {
209
+ root = grandParent ;
210
+ }
211
+
194
212
}
195
213
196
214
insertionRelabel (grandParent );
@@ -205,8 +223,7 @@ private Node leftLeftCase(Node node) {
205
223
206
224
private Node leftRightCase (Node node ) {
207
225
node .left = leftRotate (node .left );
208
- leftLeftCase (node );
209
- return node ;
226
+ return leftLeftCase (node );
210
227
}
211
228
212
229
private Node rightRightCase (Node node ) {
@@ -217,8 +234,7 @@ private Node rightRightCase(Node node) {
217
234
218
235
private Node rightLeftCase (Node node ) {
219
236
node .right = rightRotate (node .right );
220
- rightRightCase (node );
221
- return node ;
237
+ return rightRightCase (node );
222
238
}
223
239
224
240
private Node rightRotate (Node parent ) {
@@ -231,8 +247,6 @@ private Node rightRotate(Node parent) {
231
247
child .parent = grandParent ;
232
248
parent .parent = child ;
233
249
234
- updateParentChildLink (grandParent , parent , child );
235
-
236
250
return child ;
237
251
}
238
252
@@ -246,23 +260,21 @@ private Node leftRotate(Node parent) {
246
260
child .parent = grandParent ;
247
261
parent .parent = child ;
248
262
249
- updateParentChildLink (grandParent , parent , child );
250
-
251
263
return child ;
252
264
}
253
265
254
266
// Sometimes the left or right child node of a parent changes and the
255
267
// parent's reference needs to be updated to point to the new child.
256
268
// This is a helper method to do just that.
257
- private void updateParentChildLink (Node parent , Node oldChild , Node newChild ) {
258
- if (parent != null ) {
259
- if (parent .left == oldChild ) {
260
- parent .left = newChild ;
261
- } else {
262
- parent .right = newChild ;
263
- }
264
- }
265
- }
269
+ // private void updateParentChildLink(Node parent, Node oldChild, Node newChild) {
270
+ // if (parent != null) {
271
+ // if (parent.left == oldChild) {
272
+ // parent.left = newChild;
273
+ // } else {
274
+ // parent.right = newChild;
275
+ // }
276
+ // }
277
+ // }
266
278
267
279
// Helper method to find the leftmost node (which has the smallest value)
268
280
private Node findMin (Node node ) {
@@ -332,6 +344,14 @@ public void remove() {
332
344
333
345
public static void main (String [] args ) {
334
346
347
+ RedBlackTree <Integer > rbTree = new RedBlackTree <>();
348
+ int [] values = {9 ,8 ,4 ,3 ,7 ,6 ,5 ,2 ,1 ,0 };
349
+ for (int v : values ) {
350
+ rbTree .insert (v );
351
+ rbTree .display ();
352
+ System .out .println ("------------------------------------------------------------------------------" );
353
+ }
354
+
335
355
}
336
356
337
357
}
0 commit comments