You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Red-Black Tree/README.markdown
+10-9
Original file line number
Diff line number
Diff line change
@@ -49,31 +49,32 @@ n n n n n b
49
49
50
50
We create a new node with the value to be inserted into the tree. The color of this new node is always red.
51
51
We perform a standard BST insert with this node. Now the three might not be a valid RBT anymore.
52
-
We now go through several insertion steps in order to make the tree valid again. We call the just inserted node n.
53
-
1. We check if n is the rootNode, if so we paint it black and we are done. If not we go to step 2.
52
+
We now go through several insertion steps in order to make the tree valid again. We call the just inserted node n.
53
+
54
+
**Step 1**: We check if n is the rootNode, if so we paint it black and we are done. If not we go to Step 2.
54
55
55
56
We now know that n at least has a parent as it is not the rootNode.
56
57
57
-
2. We check if the parent of n is black if so we are done. If not we go to step 3.
58
+
**Step 2**: We check if the parent of n is black if so we are done. If not we go to Step 3.
58
59
59
60
We now know the parent is also not the root node as the parent is red. Thus n also has a grandparent and thus also an uncle as every node has two children. This uncle may however be a nullLeaf
60
61
61
-
3. We check if n's uncle is red. If not we go to 4. If n's uncle is indeed red we recolor uncle and parent to black and n's grandparent to red. We now go back to step 1 performing the same logic on n's grandparent.
62
+
**Step 3**: We check if n's uncle is red. If not we go to Step 4. If n's uncle is indeed red we recolor uncle and parent to black and n's grandparent to red. We now go back to step 1 performing the same logic on n's grandparent.
62
63
63
64
From here there are four cases:
64
65
-**The left left case.** n's parent is the left child of its parent and n is the left child of its parent.
65
66
-**The left right case** n's parent is the left child of its parent and n is the right child of its parent.
66
67
-**The right right case** n's parent is the right child of its parent and n is the right child of its parent.
67
68
-**The right left case** n's parent is the right child of its parent and n is the left child of its parent.
68
69
69
-
4.Step 4 checks if either the **left right** case or the **right left** case applies to the current situation.
70
-
- If we find the **left right case**, we left rotate n's parent and go to step 5 while setting n to n's parent. (This transforms the **left right** case into the **left left** case)
71
-
- If we find the **right left case**, we right rotate n's parent and go to step 5 while setting n to n's parent. (This transforms the **right left** case into the **right right** case)
72
-
- If we find neither of these two we proceed to step 5.
70
+
**Step 4**: checks if either the **left right** case or the **right left** case applies to the current situation.
71
+
- If we find the **left right case**, we left rotate n's parent and go to Step 5 while setting n to n's parent. (This transforms the **left right** case into the **left left** case)
72
+
- If we find the **right left case**, we right rotate n's parent and go to Step 5 while setting n to n's parent. (This transforms the **right left** case into the **right right** case)
73
+
- If we find neither of these two we proceed to Step 5.
73
74
74
75
n's parent is now red, while n's grandparent is black.
75
76
76
-
5. We swap the colors of n's parent and grandparent.
77
+
**Step 5**: We swap the colors of n's parent and grandparent.
77
78
- We either right rotate n's grandparent in case of the **left left** case
78
79
- Or we left rotate n's grandparent in case of the **right right** case
0 commit comments