Two drawings of one tree
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 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 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 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 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.
Why it stays balanced
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.
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.
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.
Code and cost
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.
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.
What happens
;; step 1/10 · One key, two drawings insert 1 (* = red node, its link from the parent is red) insert 1 1* root 1 turns black1 2-3 tree: [1] height: 0
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.