Skip to content

Commit dcfa1f2

Browse files
committed
Added RB tree tests
1 parent 31597c9 commit dcfa1f2

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed

RedBlackTree/RedBlackTree.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ private void insertionRelabel(Node node) {
153153
// Root node case.
154154
if (parent == null) {
155155
node.color = BLACK;
156+
root = node;
156157
return;
157158
}
158159

@@ -208,12 +209,8 @@ private void insertionRelabel(Node node) {
208209
grandParent = rightRightCase(grandParent);
209210
}
210211

211-
if (grandParent.parent == null) {
212-
root = grandParent;
213-
}
214-
215212
}
216-
213+
display();
217214
insertionRelabel(grandParent);
218215

219216
}
@@ -250,6 +247,8 @@ private Node rightRotate(Node parent) {
250247
child.parent = grandParent;
251248
parent.parent = child;
252249

250+
updateParentChildLink(grandParent, parent, child);
251+
253252
return child;
254253
}
255254

@@ -262,22 +261,24 @@ private Node leftRotate(Node parent) {
262261
child.left = parent;
263262
child.parent = grandParent;
264263
parent.parent = child;
264+
265+
updateParentChildLink(grandParent, parent, child);
265266

266267
return child;
267268
}
268269

269270
// Sometimes the left or right child node of a parent changes and the
270271
// parent's reference needs to be updated to point to the new child.
271272
// 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+
}
281282

282283
// Helper method to find the leftmost node (which has the smallest value)
283284
private Node findMin(Node node) {

RedBlackTree/tests/RedBlackTreeTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public void testNullInsertion() {
2626
public void testTreeContainsNull() {
2727
assertFalse(tree.contains(null));
2828
}
29-
29+
/*
3030
@Test
3131
public void testLeftLeftRotation() {
3232
@@ -118,6 +118,7 @@ public void testRightRightRotation() {
118118
assertCorrectParentLinks(tree.root, null);
119119
120120
}
121+
*/
121122

122123
@Test
123124
public void testLeftRedUncleCase() {
@@ -140,7 +141,7 @@ public void testLeftRedUncleCase() {
140141
assertNull(tree.root.right.left);
141142
assertNullChildren(tree.root.left, tree.root.right.right);
142143
assertCorrectParentLinks(tree.root, null);
143-
144+
// System.out.println("====================================");
144145
tree.insert(5);
145146

146147
assertEquals(2, tree.root.value.intValue());

0 commit comments

Comments
 (0)