Sub topic 10
Red-black deletion and the double-black problem
This chapter assumes you know the five red-black properties, red-black insertion fixup, and rotations. Deletion is the hardest operation on a red-black tree, but it follows a small set of cases.
Why deletion is harder than insertion
Insertion adds a red node, which can break only the "no red node has a red child" rule. Deletion can break the black-height rule instead. If you remove a black node, every path through that spot now has one black node fewer than the paths beside it.
First delete as in an ordinary BST. If the node has two children, copy in the in-order successor's key and delete the successor, which has at most one child. So you only ever remove a node with zero or one child. There are three outcomes:
- The removed node is red. No black count changes, so you are done.
- The removed node is black and its child is red. Put the child in its place and color it black. The black count is restored.
- The removed node is black and its replacement is black or nil. One path is now short a black node. The replacement carries an extra black, called double-black.
Only the third outcome needs a repair.
Moving the extra black upward
The repair either removes the extra black with a rotation or pushes it up one level. The node x is double-black, s is its sibling, and p is its parent. The near nephew is the child of s on the same side as x. The far nephew is the child on the opposite side.
The cases work as follows:
- Red sibling. Rotate at
ptowardxand swap the colors ofpands. The black counts do not change, andxnow has a black sibling. Continue with cases 2 to 4. - Black sibling, both nephews black. Color
sred. This removes one black froms's side, which balances the missing black onx's side. The extra black moves up top. Ifpis red, color it black and stop. Otherwisepis double-black, and you repeat the analysis one level up. - Black sibling, near nephew red, far nephew black. Rotate at
saway fromxand swap the colors ofsand the near nephew. The far nephew is now red, which is case 4. - Black sibling, far nephew red. Rotate at
ptowardx. Givesthe old color ofp. Colorpand the far nephew black. The extra black is absorbed and the loop ends.
Cost
Case 1 leads to case 2, 3 or 4. Cases 3 and 4 finish immediately. Only case 2 can repeat, and it climbs one level each time. So deletion does O(log n) recoloring steps and at most three rotations. This is why red-black trees are cheap to rebalance, as the AVL comparison later will show.
Encoding the decision
The helper below names the case from the sibling's color and the nephews' colors. It is a small aid for tracing by hand and for tests.
RED, BLACK = "R", "B"
def delete_case(sibling_color, near_red, far_red):
"""Name the double-black repair case for a given sibling and nephews."""
if sibling_color == RED:
return "case 1: rotate at parent, swap colors, re-examine"
if far_red:
return "case 4: rotate at parent, finish"
if near_red:
return "case 3: rotate at sibling, then case 4"
return "case 2: recolor sibling red, push double-black up"
print(delete_case(BLACK, near_red=True, far_red=False))
# case 3: rotate at sibling, then case 4
print(delete_case(BLACK, near_red=False, far_red=False))
# case 2: recolor sibling red, push double-black upExercise
A black leaf x is deleted. Its sibling s is black, s has a red left child, and s has no right child. x was the right child of its parent p, and p is red. Which case applies, and what is the final color of p?
Show answer
x was the right child, so the near nephew is s's right child (nil, black) and the far nephew is s's left child (red). That is case 4. Rotate right at p, give s the old color of p (red), and color p and the far nephew black. After the rotation, p is black.