ENع
←2-3 and red-black trees
Lesson 17 · Search trees

2-3 trees and red-black trees

15 min10 steps5 questionsIntermediate
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.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 17 · Interactive
01 / 10

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
Loading figure…
or ← →
Read the lesson step by step
  1. 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
  2. A 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: 1
  3. A 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: 1
  4. Rotations 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: 2
  5. Splits 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: 2
  6. Same 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 2
  7. Insert 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: 2
  8. Sorted 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.9
  9. Three 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 time
  10. Search 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

Quiz Unlocks after the last step.