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 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. 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

  1. Delete the node following normal BST delete rules.
  2. If the removed node (or the node that replaced it) was black, the black-height rule may now be broken.
  3. Look at the sibling of the affected position to decide which fix-up case applies (red sibling, black sibling with red/black children, etc.).
  4. Apply the matching recoloring and/or rotation to restore the equal-black-height rule.
  5. 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

  1. Removing the black leaf breaks the black-height rule at its parent
  2. Sibling is black with at least one red child → this triggers a rotation case
  3. Perform the rotation and recolor accordingly to restore equal black-height on both sides

Output: Tree rules restored with the minimum necessary rotations

Try your own input
Red-Black: Introduction
A self-balancing BST that colors each node red or black and follows a few coloring rules — this keeps the tree "balanced enough" without needing exact height checks like AVL.
50
30
20
40
70
60
80
Red Black Active Result
Red-Black: Introduction
Press ▶ Run to watch it step by step.
Python · Red-Black Trees in Code
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: delete
Input: Delete a black leaf node with a black sibling that has red children

Behind 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

TimeO(log n)
SpaceO(log n) recursive, O(1) extra work per fix-up step

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

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Ready for the next step?

See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.