Chapter 13 · Balanced Trees

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:

1

Every node is either RED or BLACK.

Two colors encode height balance compactly — one bit per node.

2

The root is always BLACK.

Simplifies recoloring rules; after insert the root is re-blacked if needed.

3

Every leaf (NIL sentinel) is BLACK.

NIL sentinels let algorithms treat missing children uniformly — no null checks.

4

No two adjacent red nodes (no red parent with a red child).

This prevents more than 2× height growth by bounding consecutive reds.

5

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

138171111525

Black-height = 2 on every path from root to NIL. No adjacent red nodes.

BLACK nodeRED node

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.

Binary Search Tree
preset:
normalpathfounddelete

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:

rb-insert-fixup.pseudo
1insertFixup(tree, z): // z is newly inserted red node
2 while z.parent.color == RED: // while invariant 4 is broken
3 if z.parent == z.grandparent.left:
4 uncle <- z.grandparent.right
5 if uncle.color == RED: // Case 1: uncle is red
6 z.parent.color <- BLACK
7 uncle.color <- BLACK
8 z.grandparent.color <- RED
9 z <- z.grandparent // move problem up
10 else:
11 if z == z.parent.right: // Case 2: z is right child
12 z <- z.parent
13 rotateLeft(tree, z) // convert to Case 3
14 z.parent.color <- BLACK // Case 3: z is left child
15 z.grandparent.color <- RED
16 rotateRight(tree, z.grandparent)
17 else: // mirror for right side
18 // 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

C++ std::map and std::set
Java TreeMap and TreeSet
Linux CFS scheduler (task ordering)
Nginx timer wheel
Databases — many index structures
Compiler symbol tables

Time complexity

search, insert, delete — guaranteed by balance invariantsO(log n)

Logarithmic time

rotations per insert (at most 2) and per delete (at most 3)O(1)

Constant time

space — one extra bit per node for colorO(n)

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?

🔍Why industry picks Red-Black over AVL
Most standard library containers use red-black trees, not AVL. The reason: insert and delete require at most a constant number of rotations in a red-black tree, while AVL can require O(log n) rotations on delete. For containers with heavy mutation workloads, fewer structural changes means better cache performance.

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.