Red Black tree deletion involves following cases:
Case 2: When sibling and both of its children are BLACK (or children or leaf nodes):
Case 3: When uncle is BLACK and its left child is RED and right child is BLACK.
Then we just do some pointer change and color change of new sibling and old sibling d and c of a, which was having double black problem, thus the case will be converted to case 4 and we will apply inverse of zig-zig rotation and our tree will be balanced.
Once again let me remind you sibling could be present on other side of the tree also so make the similar cases, just replace left with right and vice-versa.
Note : After case 3 we must have to call case 4.
Case 4: When sibling is BLACK and its right child is BLACK (left child could be R/B).
- If node to delete is a RED leaf node then just delete it.
- If node to delete is a BLACK node and has one red child then, just replace it with child and retain color of node to be deleted.
- If node is having more than two children then just replace the node with in-order predecessor and call the delete routine at in-order predecessor recursively.
- But the problem arises when node to delete is a BLACK node and it has only one BLACK child or node to be deleted is a BLACK leaf node. Then DOUBLE BLACK problem arises.
Note: Points form 1 to 3 are easy to implement and can be understood using the code given below.
To resolve DOUBLE BLACK problem we have following four cases:
Why double black problem:
Suppose we have deleted a BLACK node and the number of nodes in the path going through it is reduced by 1. So the BLACK replacement will carry one extra BLACK color to retain property 5 of RB Tree that in any path from root to leaf the number of BLACK nodes must be same.
NOTE: "X" shown in below diagram is replacement of deleted node, it could be NULL also, so we have to be careful with our DOUBLE BLACK fix routine.
Rectangular boxes under x could be leaf nodes or trees, as in future when the DOUBLE BLACK problem propagates then these boxes will make sense in the diagram.
Case 1: When sibling has color RED, we simply do inverse of zig-zig rotation:
Similarly when sibling is in left side we can do the same process.
Case 2: When sibling and both of its children are BLACK (or children or leaf nodes):
Then apply the color change and if the parent is BLACK then call the DOUBLE BLACK fix routine again at new X. Otherwise just stop.
Yellow color shows that the node could have either RED or BLACK color.
When parent is RED then change color of parent to BLACK and stop, other wise move x to parent and call DOUBLE BLACK fix routine again,
Here the parent was RED so we stop the routine.
ELSE: call DOUBLE BLACK routine again,
Case 3: When uncle is BLACK and its left child is RED and right child is BLACK.
Then we just do some pointer change and color change of new sibling and old sibling d and c of a, which was having double black problem, thus the case will be converted to case 4 and we will apply inverse of zig-zig rotation and our tree will be balanced.
Note : After case 3 we must have to call case 4.
Case 4: When sibling is BLACK and its right child is BLACK (left child could be R/B).
We will apply inverse of zig-zig rotation and will change the color in following manner:
- Color x's new grandfather with same color as x'father.
- Color new uncle BLACK.
Now we have learned the cases to resolve DOUBLE BLACK problem, lets see some code in action:
There are two classes and one interface:
- RBTreeFunctions: Interface
- RBTree
- RBTreeTest
References:
https://www.cs.usfca.edu/~galles/visualization/RedBlack.html
http://www.stolerman.net/studies/cs521/red_black_trees.pdf
Comments
Post a Comment