Red-Black Trees
A red-black tree is a self-balancing BST that encodes balance information in a single bit per node: its color. Five simple rules about those colors guarantee the tree never becomes more than twice as tall as necessary — giving O(log n) operations with fewer structural changes than AVL.
What is a red-black tree?
A red-black tree is a self-balancing binary search tree where every node is coloured red or black, and a few rules keep it roughly balanced: the root is black, red nodes can't have red children, and every path from a node to its leaves has the same number of black nodes. Insert and delete fix violations with recolouring and rotations, which guarantees O(log n) operations.
The 5 invariants
Every valid red-black tree satisfies all five of these simultaneously:
Every node is either RED or BLACK.
Two colors encode height balance compactly — one bit per node.
The root is always BLACK.
Simplifies recoloring rules; after insert the root is re-blacked if needed.
Every leaf (NIL sentinel) is BLACK.
NIL sentinels let algorithms treat missing children uniformly — no null checks.
No two adjacent red nodes (no red parent with a red child).
This prevents more than 2× height growth by bounding consecutive reds.
All paths from any node to its NIL descendants contain the same number of black nodes (black-height).
Equal black-heights mean the tree cannot be unbalanced — height is O(log n).
Example tree
Black-height = 2 on every path from root to NIL. No adjacent red nodes.
Root 13 is black. Its children 8 and 17 are red. Their children are all black. Black-height = 2 on every path from root to NIL.
Live demo — BST shape explorer
Build a BST to see how skewed inputs degrade performance. In a red-black tree the same insertions would trigger rotations and recolouring to maintain balance.
Tree is empty — insert a value or click a preset above
→ Insert values to build your BST
Insert fix-up pseudocode
Every new node is inserted as RED (to preserve black-height). If the parent is also red, invariant 4 is violated and fix-up runs:
1insertFixup(tree, z): // z is newly inserted red node2 while z.parent.color == RED: // while invariant 4 is broken3 if z.parent == z.grandparent.left:4 uncle <- z.grandparent.right5 if uncle.color == RED: // Case 1: uncle is red6 z.parent.color <- BLACK7 uncle.color <- BLACK8 z.grandparent.color <- RED9 z <- z.grandparent // move problem up10 else:11 if z == z.parent.right: // Case 2: z is right child12 z <- z.parent13 rotateLeft(tree, z) // convert to Case 314 z.parent.color <- BLACK // Case 3: z is left child15 z.grandparent.color <- RED16 rotateRight(tree, z.grandparent)17 else: // mirror for right side18 // symmetric cases (uncle on left)19 tree.root.color <- BLACK // ensure root is always black
AVL vs Red-Black
AVL Tree
- + Stricter balance: height ≤ 1.44 log n
- + Faster lookups due to shorter height
- − More rotations on insert/delete
- − Stores height integer per node
- Best for read-heavy workloads
Red-Black Tree
- + At most 2 rotations per insert
- + At most 3 rotations per delete
- + Only 1 extra bit per node (color)
- − Height up to 2 log n
- Best for write-heavy workloads
Where red-black trees are used
Time complexity
Logarithmic time
Constant time
Linear time
🎯 Quick Check
A new node is inserted and its parent is red. The uncle is also red. What does the fix-up algorithm do first?
Red-Black Trees: summary
Key points
- The longest path is at most twice the shortest, so the height is O(log n).
- Used in Java's TreeMap, C++
std::map, and the Linux scheduler. - Insertions need at most 2 rotations; deletions at most 3.
Frequently asked questions
›Do I need to implement a red-black tree in interviews?
Rarely. Know the rules, why they keep the tree balanced, and the O(log n) guarantees. Full implementations are long and error-prone.