Data Structures · Visualization
Red-Black Tree: Delete
Red-Black Tree
Deleting from a red-black tree is the most involved operation: it starts with a normal BST delete, but removing a black node can break the rule that every path must have the same number of black nodes. Time: O(log n). Space: O(log n) recursive, O(1) extra work per fix-up step.
Big-O: Operations O(log n); height ≤ 2·log₂(n+1).
Red-Black Tree: Delete
Red-Black TreeDeleting from a red-black tree is the most involved operation: it starts with a normal BST delete, but removing a black node can break the rule that every path must have the same number of black nodes. The tree fixes this by treating the spot as having a 'double black' deficiency and resolving it through a careful sequence of recoloring and rotations based on the sibling's color and its children's colors.
How Delete Works
- Delete the node following normal BST delete rules.
- If the removed node (or the node that replaced it) was black, the black-height rule may now be broken.
- Look at the sibling of the affected position to decide which fix-up case applies (red sibling, black sibling with red/black children, etc.).
- Apply the matching recoloring and/or rotation to restore the equal-black-height rule.
- Repeat up the tree if the deficiency propagates further.
Delete Worked Example
Input: Delete a black leaf node with a black sibling that has red children
- Removing the black leaf breaks the black-height rule at its parent
- Sibling is black with at least one red child → this triggers a rotation case
- Perform the rotation and recolor accordingly to restore equal black-height on both sides
Output: Tree rules restored with the minimum necessary rotations
Press ▶ Run to watch it step by step.
def rb_delete_fixup(T, x):
while x is not T.root and x.color == BLACK:
if x is x.parent.left:
w = x.parent.right # sibling
if w.color == RED: # case 1: red sibling
w.color = BLACK
x.parent.color = RED
left_rotate(T, x.parent)
w = x.parent.right
if w.left.color == BLACK and w.right.color == BLACK:
w.color = RED # case 2: both kids black
x = x.parent
else:
if w.right.color == BLACK: # case 3: right kid black
w.left.color = BLACK
w.color = RED
right_rotate(T, w)
w = x.parent.right
w.color = x.parent.color # case 4: red right kid
x.parent.color = BLACK
w.right.color = BLACK
left_rotate(T, x.parent)
x = T.root
else:
# mirror image (x is a right child)
w = x.parent.left
if w.color == RED:
w.color = BLACK; x.parent.color = RED
right_rotate(T, x.parent); w = x.parent.left
if w.right.color == BLACK and w.left.color == BLACK:
w.color = RED; x = x.parent
else:
if w.left.color == BLACK:
w.right.color = BLACK; w.color = RED
left_rotate(T, w); w = x.parent.left
w.color = x.parent.color
x.parent.color = BLACK
w.left.color = BLACK
right_rotate(T, x.parent)
x = T.root
x.color = BLACK
# example: deleteBehind the Scenes Execution
Press ▶ Run in the visualizer — the call stack, registers and memory will replay here in sync with the code, line by line.
Delete Time & Space Complexity
When to Use Delete
- Maintaining guaranteed performance in standard library ordered containers after removals
- Long-running systems (schedulers, databases) that repeatedly insert and delete
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Ready for the next step?
See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.