SKIP LISTS & TRIES

Two ways to search without a balanced tree

L215 MINALGORITHMS
PREDICT

A skip list is a sorted linked list where coin flips let some nodes also join higher express lanes (levels), and search starts in the top lane. In a 32-key skip list, every node ends up at level 1 only. How many forward hops does searching for the largest key take?

A skip list is a sorted linked list with express lanes. Every node is on level 1. A coin decides how many more levels it joins: keep flipping while heads, so about half the nodes reach level 2, a quarter level 3. Search starts in the top lane, runs right until the next key is too big, then drops a level. Expected cost is O(log n), with no rotations.

A trie skips comparisons altogether. It walks the key one character at a time, so a lookup costs the key’s length, however many keys are stored.

▶ SIMULATION
SKIP LIST
keys 0 · levels 1 · pointers/node —
nodes per level: —
SEARCH COST
last: —
worst: 0 / bound 12.0
mean over all keys: —
level 1 (key^height)—
✓ search hops <= 2·log2 n + 2✓ trie lookup visits <= key length nodes
SCHEDULE
01[LIST]config: every node gets level 1 (no express lanes)
02[INSERT]insert the first half of the keys in order
03[INSERT]insert the rest
04[SEARCH]search(largest key)
05[SEARCH]search(middle key)
06[SEARCH]search(every key): record the worst
07[SEARCH]report
Start of sorted linked list scenario. 7 steps.
function randomLevel(rng, p, max) {
  return 1;   // every node on level 1: a sorted linked list, O(n) search
}

Watch the SEARCH COST box and the hops invariant. SORTED LINKED LIST runs the same inserts and searches and needs 32 hops against a bound of 12. SKIP LIST draws its levels from a seeded coin and its worst search over all 32 keys is 8. The bound is a probabilistic one: this seed meets it, and a different coin could miss it by a few hops. TRIE AUTOCOMPLETE shows the second invariant: every lookup visits at most as many nodes as the key has characters. That holds by construction, since a trie descends one node per character; what matters is that it never grows with the number of keys stored. Autocomplete then pays separately to list every key in the subtree under the prefix.

TUNE IT NOT YET

A plain sorted list costs n hops; the skip list bound grows as 2·log2 n + 2. Find the largest KEYS value at which the plain list still meets the bound.

GOAL: the plain list meets the bound with n >= 8

FIX IT NOT YET

This randomLevel flips the coin once, so no node is taller than 2. Make it geometric: start at 1, add a level for every heads (rng() < p), stop at the first tails or at max. The grader builds three 1024-key skip lists from your levels and searches every key.

Mental model:

a skip list buys balance with randomness instead of rotations: the levels are a coin, not a function of the keys, so no insert order can make it degenerate. Only a bad coin can. A trie buys speed by never comparing whole keys: its cost is the key length, not the number of keys.

IN PRODUCTION

Large Redis sorted sets (ZSET) are a skip list plus a hash table. LevelDB and RocksDB memtables are skip lists, because concurrent inserts are easier than in a balanced tree. Tries (and their compressed form, radix trees) back IP routing tables, autocomplete, and the Linux page cache index (a radix tree, now the XArray).

IN INTERVIEWS

“Design autocomplete” (trie, then top-k per node) or “why does Redis use a skip list for sorted sets?” Say: expected O(log n) search, simple insert with no rebalancing, easy range scans along level 1, and that the guarantee is probabilistic rather than worst case.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.
NEXT: TIMER WHEELS →