@@ -153,6 +153,7 @@ private void insertionRelabel(Node node) {
153
153
// Root node case.
154
154
if (parent == null ) {
155
155
node .color = BLACK ;
156
+ root = node ;
156
157
return ;
157
158
}
158
159
@@ -208,12 +209,8 @@ private void insertionRelabel(Node node) {
208
209
grandParent = rightRightCase (grandParent );
209
210
}
210
211
211
- if (grandParent .parent == null ) {
212
- root = grandParent ;
213
- }
214
-
215
212
}
216
-
213
+ display ();
217
214
insertionRelabel (grandParent );
218
215
219
216
}
@@ -250,6 +247,8 @@ private Node rightRotate(Node parent) {
250
247
child .parent = grandParent ;
251
248
parent .parent = child ;
252
249
250
+ updateParentChildLink (grandParent , parent , child );
251
+
253
252
return child ;
254
253
}
255
254
@@ -262,22 +261,24 @@ private Node leftRotate(Node parent) {
262
261
child .left = parent ;
263
262
child .parent = grandParent ;
264
263
parent .parent = child ;
264
+
265
+ updateParentChildLink (grandParent , parent , child );
265
266
266
267
return child ;
267
268
}
268
269
269
270
// Sometimes the left or right child node of a parent changes and the
270
271
// parent's reference needs to be updated to point to the new child.
271
272
// This is a helper method to do just that.
272
- // private void updateParentChildLink(Node parent, Node oldChild, Node newChild) {
273
- // if (parent != null) {
274
- // if (parent.left == oldChild) {
275
- // parent.left = newChild;
276
- // } else {
277
- // parent.right = newChild;
278
- // }
279
- // }
280
- // }
273
+ private void updateParentChildLink (Node parent , Node oldChild , Node newChild ) {
274
+ if (parent != null ) {
275
+ if (parent .left == oldChild ) {
276
+ parent .left = newChild ;
277
+ } else {
278
+ parent .right = newChild ;
279
+ }
280
+ }
281
+ }
281
282
282
283
// Helper method to find the leftmost node (which has the smallest value)
283
284
private Node findMin (Node node ) {
0 commit comments