2-3 trees and red-black trees
About this lesson
The Software lesson "Trees" showed a binary search tree turning into a line when keys arrive in sorted order. Here keys 1 to 7 arrive in exactly that order, and the tree stays only 2 levels deep. A 2-3 tree does it by letting a node hold two keys and splitting full nodes upward. A red-black tree stores the same thing with ordinary binary nodes, using red links, rotations and colour flips. The lesson "AVL trees and rotations" in this module balances with rotations too; the Databases lesson "B-trees" takes the 2-3 idea to wide nodes on disk.
One key, two drawings
On the right is a 2-3 tree; on the left, a red-black tree that stores the same keys. Insert 1: both hold one node. A new red-black node always starts red, then the root is painted black.
insert 1 (* = red node, its link from the parent is red) insert 1 1* root 1 turns black1 2-3 tree: [1] height: 0
Read the lesson step by step
One key, two drawings
On the right is a 2-3 tree; on the left, a red-black tree that stores the same keys. Insert 1: both hold one node. A new red-black node always starts red, then the root is painted black.
insert 1 (* = red node, its link from the parent is red) insert 1 1* root 1 turns black1 2-3 tree: [1] height: 0A red link glues a 3-node
Insert 2. In the 2-3 tree it joins the leaf: [1 2] is a 3-node, two keys with room for three children. The red-black tree glues 2 to 1 with a red link. Red links must lean left, so rotate_left turns it.
insert 2 (* = red node, its link from the parent is red) insert 2 1(-,2*) rotate_left(1) 2(1*,-) 2-3 tree: [1 2] height: 1A colour flip is a split
Insert 3. The leaf [1 2 3] has one key too many, so it splits: 2 moves up and the tree gains a level at the top. In the red-black tree, a node with two red links is that full leaf, and flip_colors splits it.
insert 3 (* = red node, its link from the parent is red) insert 3 2(1*,3*) flip_colors(2) 2*(1,3) root 2 turns black2(1,3) 2-3 tree: [2] / [1] [3] height: 1Rotations keep the order
Insert 4. It joins the leaf [3], making [3 4]. The red link leans right, so rotate_left lifts 4 and hangs 3 below it. Keys only move up or down, so left to right they stay sorted. The rotation leaves the 2-3 tree unchanged.
insert 4 (* = red node, its link from the parent is red) insert 4 2(1,3(-,4*)) rotate_left(3) 2(1,4(3*,-)) 2-3 tree: [2] / [1] [3 4] height: 2Splits push keys up
Insert 5. Leaf [3 4 5] splits and 4 moves up into the root, making [2 4]. In the red-black tree, flip_colors turns the link to 4 red, so 4 joins 2. That link leans right, so rotate_left at the root lifts 4.
insert 5 (* = red node, its link from the parent is red) insert 5 2(1,4(3*,5*)) flip_colors(4) 2(1,4*(3,5)) rotate_left(2) 4(2*(1,3),5) 2-3 tree: [2 4] / [1] [3] [5] height: 2Same black links on every path
Splits only push keys up, so every 2-3 leaf sits at the same depth. In the red-black tree, every path from the root to an empty link crosses the same number of black links. Red links do not count: they sit inside a 2-3 node.
tree now: 4(2*(1,3),5) 2-3 tree: [2 4] / [1] [3] [5] black links from the root down to each node: 4 → 2 is red (2 and 4 are one 2-3 node) 4 → 2 → 1 1 black link 4 → 2 → 3 1 black link 4 → 5 1 black link null links count as black, so every root-to-null path has 2Insert 6 and 7
Insert 6: one more rotate_left. Insert 7: leaf [5 6 7] splits and pushes 6 up into the root, now [2 4 6]. The root splits too, and 4 becomes the new root. In the red-black tree that is two colour flips in a row.
insert 6, 7 (* = red node, its link from the parent is red) insert 6 4(2*(1,3),5(-,6*)) rotate_left(5) 4(2*(1,3),6(5*,-)) insert 7 4(2*(1,3),6(5*,7*)) flip_colors(6) 4(2*(1,3),6*(5,7)) flip_colors(4) 4*(2(1,3),6(5,7)) root 4 turns black4(2(1,3),6(5,7)) 2-3 tree: [4] / [2] [6] / [1] [3] [5] [7] height: 2Sorted keys, short tree
Seven keys came in sorted order, the worst case for a plain binary search tree: it would be a line of height 6. Here the height is 2. Red links never come two in a row, so the height stays at most 2 log₂ n.
final tree: 4(2(1,3),6(5,7)) 2-3 tree: [4] / [2] [6] / [1] [3] [5] [7] height (links on the longest root-to-node path): 2 plain BST, keys 1..7 in order: 1 → 2 → ... → 7, height 6 algs4 §3.3: height of a red-black BST with N nodes ≤ 2 lg N N = 7: 2 lg 7 ≈ 5.61 N = 1,000,000: 2 lg N ≈ 39.9Three checks do the work
put inserts like a plain BST, then calls fix on every node on the way back up. A right-leaning red link: rotate left. Two red links in a row: rotate right (inserting 3, 2, 1 needs it). Two red children: flip colours.
1 def fix(h): 2 if red(h.right) and not red(h.left): 3 h = rotate_left(h) 4 if red(h.left) and red(h.left.left): 5 h = rotate_right(h) 6 if red(h.left) and red(h.right): 7 flip_colors(h) 8 return h inserting 3, 2, 1 runs: root black, rotate_right(3), flip_colors(2), root black each check and each fix is a few pointer changes: constant timeSearch ignores colour
Search is plain BST search; colours never matter. Find 5: 5 > 4, go right; 5 < 6, go left; found after 3 comparisons. Because the height stays at most 2 log₂ n, search and insert take O(log n) steps.
search(root, 5) 5 > 4 go right 5 < 6 go left 5 = 5 found (3 comparisons) search(h, key) never reads h.red
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01In a 2-3 tree, what happens when you insert a key into a leaf that already holds two keys?
A 2-3 node holds at most two keys. Three keys split into two 2-nodes, and the middle key moves up. If that fills the parent, it splits too; a split at the root adds a level at the top.
02Why does every leaf of a 2-3 tree stay at the same depth?
Inserting never adds a node below a leaf. The only way to gain height is a root split, which lengthens every path by one at the same time.
03A node in a left-leaning red-black tree has a red link to its right child and a black link to its left child. Which fix runs?
Red links must lean left. The first check in fix, red(h.right) and not red(h.left), sees this and calls rotate_left, which turns the red link to the left without changing the key order.
04What does flip_colors do, seen as a 2-3 tree?
A node with two red children is a 2-3 node holding three keys. Turning both links black and the link above red splits it: the two outer keys become separate nodes and the middle key joins its parent.
05You insert 1, 2, 3, 4, 5, 6, 7 in that order. What is the height of the red-black tree, and of a plain binary search tree?
The red-black tree ends as 4 at the root, 2 and 6 below, and 1, 3, 5, 7 at the bottom: height 2. A plain BST puts each key to the right of the last one: a line with height 6.
Glossary
- 2-node
- A 2-3 tree node with one key and up to two children: keys smaller go left, larger go right.
- 3-node
- A 2-3 tree node with two keys and up to three children: smaller, between the two keys, and larger.
- Perfect balance
- Every path from the root down to an empty link has the same length. 2-3 trees always have it.
- Red link
- In a red-black tree, a link that glues two nodes into one 2-3 node. In a left-leaning red-black tree it always leans left.
- Black height
- The number of black links on a path from the root down to an empty link. It is the same for every path.
- Rotation
- A local change of three links that lifts one node above its parent. The keys stay in sorted order from left to right.
- Colour flip
- Turning a node's two red child links black and the link above it red. In the 2-3 tree this is a split.
Notes and sources
- This lesson uses the left-leaning red-black tree of Sedgewick and Wayne (Algorithms, §3.3), which matches 2-3 trees one to one. Classic red-black trees (CLRS chapter 13) also allow red links that lean right; they match 2-3-4 trees instead. The guarantees are the same.
- Books colour either the links or the nodes. They mean the same thing: a node is red when the link from its parent is red. The figure shows both: the link colour, and a dot on each node.
- algs4 §3.3 proves that a red-black BST with N nodes has height at most 2 lg N. Every path to a null link has the same number of black links, and red links never come two in a row, so no path is more than twice as long as another.
- Deleting from a red-black tree also takes O(log n) time, but the code is longer; algs4 RedBlackBST.java has it.
- The code is real Python 3. insert(root, k) for k = 1 to 7 builds 4(2(1,3),6(5,7)), all black, matching the figure.
- Sedgewick and Wayne, Algorithms 4th ed., §3.3 Balanced Search Trees
- Sedgewick and Wayne, RedBlackBST.java (left-leaning red-black BST)
- MIT 6.046J Introduction to Algorithms (Fall 2005), Lecture 10: Red-black Trees, Rotations, Insertions, Deletions
- Cormen, Leiserson, Rivest and Stein, Introduction to Algorithms, 4th ed. (MIT Press, 2022), chapter 13
- Python Language Reference: Assignment statements
- Python Tutorial: Classes
Quiz Unlocks after the last step.