A red-black tree is a binary search tree whose nodes are red or black, with rules that keep its longest path at most twice its shortest, so its height stays O(log n). How it works, with step-by-step animations of the insertion cases from the VisiGrab app.
// how red-black trees work
A red-black tree is a binary search tree in which every node is either red or black. Five rules on these colours keep the longest path from the root at most twice the shortest, so search, insertion and deletion take O(log n) time. Rebalancing with recolouring and rotations restores the rules after an insertion or deletion.
Below is a red-black tree of seven keys, drawn as trees usually are. The root 38 is black, the red node 20 has black children, and the red nodes 41 and 76 have no children.
The rules speak of NIL leaves, which most drawings leave out. A NIL leaf stands for a missing child: it holds no key and counts as black. Drawn in full, the same tree has eight of them, two under each of 1, 27, 41 and 76.
The black height of a node counts the black nodes on a path from it down to a NIL leaf, without the node itself and with the NIL leaf. In the figure below, every path from the root 38 meets two black nodes: node 1 or 27 and a NIL leaf on the left, 43 and a NIL leaf on the right. Sources that also count a black start node get one more; sources that leave NIL leaves out get one less. Black depth, counted from the root down to a node, is a different measure.
Why NIL leaves? With them, every path from a node ends at a leaf of the same kind, so the black height property counts to the same end on every path. They also give a missing child a colour: in insertion cases 2 and 3 below, the uncle is missing, and as a NIL leaf it counts as black. In code, one shared black sentinel node usually stands in for all NIL leaves, so the fix-up reads the colour of a missing uncle like any other.
The leaf property concerns only NIL leaves; a key node with NIL children may be red, like 41 and 76.
With equal black counts and no two adjacent reds, the shortest possible path is all black and the longest alternates black and red, so it is at most twice as long. In the example below, step 1 shows the shortest path from the root 31 and step 2 the longest: both hold three black nodes, but one has three nodes and the other six.
For n keys, the height in key nodes is at most 2 log2(n + 1): at least half of the nodes on any path below the root, NIL leaf included, are black, and a subtree of black height b holds at least 2b − 1 keys.
The new key goes down as in a binary search tree and becomes a red leaf; a key already in the tree is refused. A black node would add a black to every path through it. A red one changes no black count, so it can only break the red node property, under a red parent. With a black parent, insertion is done. The first key of an empty tree is a red root, which breaks the root property, and is recoloured black.
A red parent starts the fix-up, which marks four nodes: C, the current node, first the new one; P, its parent; G, the grandparent; and U, the uncle, P's sibling. A missing uncle is a black NIL leaf, drawn as a NIL box marked U.
The fix depends on the uncle's colour and on where C and P sit: on opposite sides relative to G when one is a left child and the other a right child (a triangle), on the same side when both are left or both are right children (a line).
| Case | Uncle | C and P relative to G | Fix |
|---|---|---|---|
| 1 | red | either | recolour P and U black, G red; move C up to G |
| 2 | black | opposite sides | rotate P; case 3 follows |
| 3 | black | same side | swap the colours of P and G; rotate G |
A mirror image counts as the same case. This page numbers the cases as the animations and Cormen et al.'s Introduction to Algorithms do. Wikipedia numbers them I2, I5 and I6 and calls C an inner or outer grandchild; some tutorials say zig-zag and zig-zig.
Recolouring P and U black and G red keeps every black count, but G may now have a red parent, so G becomes C and the check repeats higher up. No other case moves up.
Here 79 is inserted as the left child of the red 80. With C = 79, P = 80, U = 90 (red) and G = 87, nodes 80 and 90 turn black and 87 red. 87's parent 93 is red, so case 1 repeats with C = 87, P = 93, U = 14 (red) and G = 77: 93 and 14 turn black, the root 77 red. The red root is then recoloured black. Because 93 and 14 turned black, the black height grows from 2 to 3. Step through it below.
P is rotated down, away from C, which takes its place. The old parent becomes C; now C and P are on the same side, so case 3 follows at once.
Here 25 is inserted as the right child of the red 24, the left child of 28: C = 25, P = 24, G = 28, and U is the NIL right child of 28. A left rotation on 24 lifts 25; case 3 then makes 25 black and 28 red and rotates 28 to the right. 25 ends black, with the red children 24 and 28. Step through it below.
P and G swap colours, and G is rotated down, away from C. P takes G's place, black with two red children, and the fix-up ends.
In the same tree as in case 2, 20 is inserted as the left child of 24: C = 20, P = 24, G = 28, and U is again the NIL right child of 28. 24 turns black and 28 red, and a right rotation on 28 leaves 24 with the red children 20 and 28. Step through it below.
Case 1 can leave a red G under a red parent, so another case can follow.
Here 66 is inserted as the right child of the red 64. With C = 66, P = 64, U = 82 (red) and G = 78, case 1 turns 64 and 82 black and 78 red. Then C = 78, P = 84 (red), G = 58, and U = 21, a black key node, not a NIL leaf. 78 is a left child and 84 a right child: case 2. A right rotation on 84 lifts 78; case 3 then makes 78 black and 58 red and rotates the root 58 to the left. 78 becomes the root, with the red children 58 and 84. Step through it below.
Case 3 alone is a single rotation, like the AVL LL and RR rotations; case 2 then case 3 is a double rotation, like LR and RL. Case 3 always ends the fix-up, so an insertion needs at most two rotations. Case 1 only recolours, but it can climb O(log n) levels.
Deletion starts as in a binary search tree: a node with two children is replaced by its in-order successor, which takes its colour, so the node that really leaves its position is the successor. A red one breaks nothing. A black one leaves its paths one black short: a red child that moves up is recoloured black, otherwise the empty NIL leaf is marked double black. Four cases, set by the sibling's colour and its children, absorb the extra black or pass it up, with at most three rotations.
| Measure | Worst case |
|---|---|
| Search | O(log n) |
| Insert | O(log n), at most 2 rotations |
| Delete | O(log n), at most 3 rotations |
| Height | at most 2 log2(n + 1) |
| Space | O(n) |
Both are approximately balanced, by different rules. An AVL tree limits the height difference at every node to one, which keeps its height below about 1.44 log2 n; a red-black tree limits the longest path to twice the shortest, about 2 log2 n. On deletion, a red-black tree rotates at most three times, an AVL tree possibly at every level up to the root. The stricter AVL rule gives shorter lookups.
A black node and its red children act as one node with up to three keys. Read this way, a red-black tree is a 2-3-4 tree, a B-tree of maximum degree 4, and case 1 corresponds to a node split.
TreeMap and TreeSet; since Java 8, HashMap also turns a bucket of more than eight entries into a red-black tree once the table has at least 64 buckets.std::map and std::set in libstdc++, libc++ and Microsoft's library, though the standard does not require it.// in the app
In VisiGrab you build the tree yourself, and every change plays out step by step.
Insert any key with “+”, tap a node to delete it, or start from a random tree. Every insert and delete is kept in a list.
Step through every recolouring and rotation, and step back through them. Show or hide the NIL leaves and the C, P, G, U markers.
The double black and its four cases, each with an example to step through.
The five rules, NIL leaves, black height and every insertion and deletion case, with interactive examples.
A complete red-black tree implementation, explained part by part.
Check what you have learned before moving on.
// build your own
Download VisiGrab and step through every insertion, deletion and case yourself.
The red-black tree is one of 30+ topics. See them all