Sub topic 5
Choosing node heights with coin flips
This chapter assumes you know singly linked lists, Big-O notation, expected value, and basic Python. It covers how a skip list decides how tall each new node is, and why that decision needs no rebalancing.
The problem a height solves
Each node in a skip list sits on layer 1 and may also sit on some higher layers. Its height (or level) is the number of layers it appears on, which is also the length of its forward list.
A perfectly balanced skip list would give every second node height 2, every fourth node height 3, and so on. Keeping that pattern exact is hard, because every insert or delete would shift the pattern for the nodes after it. A skip list avoids this by choosing each height at random when the node is created. The heights are then independent of the other nodes and of the order in which keys arrive.
The coin-flip rule
Every node starts at height 1. Then you flip a biased coin. With probability p the coin says "promote", and the height goes up by one. You keep flipping until the coin says stop or you reach the maximum level.
import random
def random_level(p=0.5, max_level=16):
level = 1
while level < max_level and random.random() < p:
level += 1
return levelThe same logic appears as _random_level in the SkipList class, using self.P and self.MAX_LEVEL.
The resulting distribution
A node reaches height k only if the first k - 1 flips all succeed. So:
- P(height ≥ k) = p^(k−1)
- P(height = k) = p^(k−1) · (1 − p), as long as k is below the maximum
This is a geometric distribution. With p = 0.5, you can expect these counts from 100,000 nodes:
| Height | Probability | Expected nodes |
|---|---|---|
| 1 | 1/2 | 50,000 |
| 2 | 1/4 | 25,000 |
| 3 | 1/8 | 12,500 |
| 4 | 1/16 | 6,250 |
Run the sampler to check this against real output.
import random
from collections import Counter
def random_level(p=0.5, max_level=16):
level = 1
while level < max_level and random.random() < p:
level += 1
return level
counts = Counter(random_level() for _ in range(100_000))
for lvl in sorted(counts):
print(lvl, counts[lvl])Your numbers will differ slightly on each run. Each count should stay close to the table, and each row should be about half the one above it.
Why this gives express lanes
Each layer holds about a fraction p of the nodes on the layer below it. With p = 0.5, layer 2 has about n/2 nodes, layer 3 about n/4, and so on. These are the same proportions a perfectly balanced structure would have, but they come from chance. A search can skip about half the remaining nodes at each layer, which is what makes expected O(log n) search possible. The next chapters cover the full analysis.
The expected height of one node is 1 / (1 − p), which is 2 for p = 0.5. A node has one forward pointer per layer, so the total pointer count is about 2n. This is why space is O(n).
The role of the maximum level
The cap MAX_LEVEL has two jobs. It sets the size of the head sentinel's forward array, and it stops a very unlucky run of flips from creating a huge node. With p = 0.5 and a cap of 16, the structure is sized for about 2^16 = 65,536 keys. It still works with more keys, but the top layer then holds more than a couple of nodes and search slows toward a longer walk along the top layer.
Common mistakes
- Using
<=or flipping with the wrong sense. The loop must continue while the random number is belowp. If you reverse it, most nodes become tall. - Forgetting to raise
self.level. When a new node is taller than the current list height,insertmust updateself.level. Otherwise the new top layers are never searched. - Letting
levelpassMAX_LEVEL. The checklevel < self.MAX_LEVELis what prevents an index error on the head'sforwardlist.
Exercise
With p = 0.5, what is the probability that a new node has height at least 4, and about how many of 1,000,000 nodes would you expect to reach that height?
Show answer
P(height ≥ 4) = 0.5^3 = 1/8 = 0.125. So you would expect about 125,000 nodes of height 4 or more.
Exercise
If you change p from 0.5 to 0.25, what happens to the expected height of a node, and what happens to the total number of forward pointers?
Show answer
The expected height becomes 1 / (1 − 0.25) ≈ 1.33, down from 2. Total pointers drop to about 1.33n, so the list uses less memory. Each layer has fewer nodes, so a search must walk further along each layer.