Inserting and Deleting of Red-Black Trees
For I hate to remember so many cases, like RRr, RLb, RLr, following are some in-order methods covering every case for operations in Red-Black Trees.
Inserting:
1. Find Uncle.
2. If uncle is red, change the colors of parent, uncle, and grandparent.
3. If black, rotate to balanced tree, and make the second level RED.
Deleting:
1. Find Sibling’s red descendant (including itself having lowest priority, inner path having highest priority, and outer path).
2. If inner path, change to balanced tree. If outer path, rotate in sequence.
3. If fail to find red descendant, make sibling RED in order to make parent be the new deficient node, and recurse.
Explanation:
Uncle and sibling, obviously, are the other child of grandparent and parent, respectively. They are d and v in the cases following.
Rotate in sequence: the path has no polyline there, and move every node in path forward, keeping left and right positions the same.
Changing to a balanced tree, which is for inner path (or polyline path), make a node having smaller height to be the root of sub-tree.
|
Rotations |
Complexity |
Insert in AVL |
O(1) |
O(log n) |
Delete in AVL |
O(log n) |
O(log n) |
Insert in Red-Black Tree |
O(2) |
O(log n) |
Delete in Red-Black Tree |
O(3) |
O(log n) |