Sub topic 3
Red-Black Insertion: Recolor or Rotate
Assume you know binary search tree operations, recursion, tree height, and Big-O notation. A red-black tree stores one extra bit, called a color, in each node.
Red-black insertion: recolor or rotate
A red-black tree follows five rules:
- Every node is red or black.
- The root is black.
- Every missing child, called a NIL leaf, is black.
- A red node cannot have a red child.
- Every path from a node to its NIL leaves contains the same number of black nodes.
Insertion first follows ordinary binary search tree insertion. The new node is red, so the black counts do not change. If its parent is black, the tree is still valid. If its parent is red, repair is required.
- If the uncle is red, recolor the parent and uncle black, recolor the grandparent red, and continue upward.
- If the uncle is black and the new node forms a triangle, rotate the parent to make a line.
- For a line, recolor and rotate the grandparent.
- Apply the mirror-image operations when the new node is on the right.
Red-black deletion and the double-black problem
Deleting a red node is usually simple. Deleting a black node can make every path through its former position contain one fewer black node. This temporary shortage is called double black.
Deletion repair examines the sibling:
- A red sibling is recolored and rotated, converting the case to one with a black sibling.
- A black sibling with black children is recolored, moving the shortage to the parent.
- A black sibling with a red child is rotated and recolored to restore equal black counts.
Deletion has more cases than insertion, which is a major implementation cost.
Why red-black trees have logarithmic height
Let the black-height be the number of black nodes on a root-to-leaf path. A tree with black-height b has at least 2^b - 1 internal nodes. Because red nodes cannot be adjacent, a path has at most twice as many total nodes as black nodes. Therefore:
height <= 2 log2(n + 1)
Search, insertion, and deletion are all O(log n).
AVL or red-black: choosing between them
| Workload | Better choice | Reason |
|---|---|---|
| Many searches and few updates | AVL | Tighter height gives shorter searches |
| Frequent insertions and deletions | Red-black | Usually needs less rebalancing |
| Predictable worst-case lookup | Either | Both have logarithmic height |
Checking invariants in tests
A validator should check BST ordering, root color, red-child rules, and equal black-height.
def check_rb(node, low=None, high=None):
if node is None:
return 1
if low is not None and node.key <= low:
raise ValueError("BST order violated")
if high is not None and node.key >= high:
raise ValueError("BST order violated")
if node.color == "R":
for child in (node.left, node.right):
if child is not None and child.color == "R":
raise ValueError("red child violation")
left = check_rb(node.left, low, node.key)
right = check_rb(node.right, node.key, high)
if left != right:
raise ValueError("black-height violation")
return left + (node.color == "B")Exercise
Why does recoloring a red parent and red uncle preserve black-height?
Show answer
Both nodes change from red to black, while their grandparent changes from black to red. Every path through either child gains one black node, and paths through the grandparent lose one, so the counts remain equal.