Left-Lean-Red-Black tree
data structures
tree deletion
algorithms
computer science

Deletion in a left-leaning red-black (LLRB) tree explained

Codemia Knowledge Hub ·

Deleting from a left-leaning red-black (LLRB) tree works by pushing red links down the search path so that the node you remove ends up red (and at the bottom of the tree). Removing a red node does not change any path's black height, so the tree stays balanced, and rotations and color flips on the way back up restore the invariants.

What an LLRB tree is

A red-black tree is a self-balancing binary search tree, which keeps insertion, deletion and search efficient. The LLRB variant simplifies rebalancing by using fewer algorithms and by requiring that red links, which represent 3-nodes, always lean left.

An LLRB tree keeps the usual red-black properties:

  1. Every node is either red or black.
  2. The root is always black.
  3. Red nodes cannot have red children (no consecutive red links).
  4. Every path from a node to its descendant leaves contains the same number of black nodes.

Deletion step by step

The general strategy is to move red nodes down the tree until the node to be deleted is a leaf.

  1. Move red links down:
    • If the node to delete is red, remove it like an ordinary binary search tree leaf.
    • If it is black, make sure the path to it contains a red node so it can be removed without violating any property.
  2. Keep balance while descending:
    • Turn edges red where needed as you move down the tree.
    • If both children of the current node are black, a rotation may be needed to provide a red link further down.
  3. Delete the node:
    • Once the node is red (or is a leaf), remove it.
  4. Restore the invariants:
    • After the deletion, apply rotations or color flips as needed so the tree satisfies every property again.

Walking through one deletion

To delete a node with value V:

  • Prepare: find the node N to delete and make N red through these manipulations.
  • Delete: remove N once it is a red leaf.
  • Rebalance: if the tree now violates a property, especially consecutive red links or uneven black depth, apply left rotations, right rotations or color flips.

Python implementation

Sedgewick's LLRB deletion; call it as root = delete_key(root, key):

python
1RED, BLACK = True, False
2
3class Node:
4    def __init__(self, key):
5        self.key, self.left, self.right, self.color = key, None, None, RED
6
7def is_red(h):
8    return h is not None and h.color == RED
9
10def rotate_left(h):
11    x = h.right
12    h.right, x.left = x.left, h
13    x.color, h.color = h.color, RED
14    return x
15
16def rotate_right(h):
17    x = h.left
18    h.left, x.right = x.right, h
19    x.color, h.color = h.color, RED
20    return x
21
22def flip_colors(h):
23    h.color = not h.color
24    h.left.color = not h.left.color
25    h.right.color = not h.right.color
26
27def fix_up(h):
28    if is_red(h.right) and not is_red(h.left):
29        h = rotate_left(h)
30    if is_red(h.left) and is_red(h.left.left):
31        h = rotate_right(h)
32    if is_red(h.left) and is_red(h.right):
33        flip_colors(h)
34    return h
35
36def move_red_left(h):
37    flip_colors(h)
38    if is_red(h.right.left):
39        h.right = rotate_right(h.right)
40        h = rotate_left(h)
41        flip_colors(h)
42    return h
43
44def move_red_right(h):
45    flip_colors(h)
46    if is_red(h.left.left):
47        h = rotate_right(h)
48        flip_colors(h)
49    return h
50
51def delete_min(h):
52    if h.left is None:
53        return None
54    if not is_red(h.left) and not is_red(h.left.left):
55        h = move_red_left(h)
56    h.left = delete_min(h.left)
57    return fix_up(h)
58
59def delete(h, key):
60    if key < h.key:
61        if not is_red(h.left) and not is_red(h.left.left):
62            h = move_red_left(h)
63        h.left = delete(h.left, key)
64    else:
65        if is_red(h.left):
66            h = rotate_right(h)
67        if key == h.key and h.right is None:
68            return None
69        if not is_red(h.right) and not is_red(h.right.left):
70            h = move_red_right(h)
71        if key == h.key:
72            m = h.right
73            while m.left:
74                m = m.left
75            h.key = m.key
76            h.right = delete_min(h.right)
77        else:
78            h.right = delete(h.right, key)
79    return fix_up(h)
80
81def delete_key(root, key):
82    if not is_red(root.left) and not is_red(root.right):
83        root.color = RED
84    root = delete(root, key)
85    if root is not None:
86        root.color = BLACK
87    return root

The rebalancing operations

  • Rotate left and rotate right: fix imbalances by rotating so that a red node comes back in line or a 3-node is split appropriately.
  • Color flip: recolor a set of linked nodes to balance black height across the tree.

Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms