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:
- Every node is either red or black.
- The root is always black.
- Red nodes cannot have red children (no consecutive red links).
- 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.
- 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.
- 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.
- Delete the node:
- Once the node is red (or is a leaf), remove it.
- 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
Nto delete and makeNred through these manipulations. - Delete: remove
Nonce 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):
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.
Related reading
- Can anyone explain this algorithm for calculating large factorials?
- Can anyone simplify this Algorithm for me?
- Can anyone suggest good illumination normalization algos for face authentication. I have tried basic algos like DoG, LBP, ..?
- Can bottom-up dynamic programming be done in Lisp?
- Can Dijkstra's Algorithm work on a graph with weights of 0?
- Can Dijkstra's Single Source Shortest Path Algorithm detect an infinite cycle in a graph?
- Can diff be beaten at its own game?
- Can hash tables really be O1?

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