Sub topic 2
Adding Express Lanes: Layered Lists
This chapter assumes you know singly linked lists, Big-O notation, and basic probability. It builds on the previous chapter's point: a sorted linked list needs O(n) steps to find a key, because you can only move one node at a time. Here you add extra layers of pointers to cut that cost.
The express lane idea
Picture a subway line with local and express trains. The local train stops at every station. The express train stops at only some of them. To travel far, you ride the express until you are close, then switch to the local train for the last few stops.
A layered list works the same way. Take a sorted linked list and call it layer 0. It holds every key. Build a second list, layer 1, that holds only some of the keys. Each key in layer 1 is also in layer 0. You can build more layers on top, each holding a subset of the layer below.
Take the keys 1 through 8:
- Layer 0: 1, 2, 3, 4, 5, 6, 7, 8
- Layer 1: 2, 4, 6, 8
- Layer 2: 4, 8
Searching with layers
To find a key, start at the top layer, at the far left. Move right while the next key is smaller than your target. When the next key is too large (or missing), drop down one layer and continue. At layer 0, the next node is either your target or proof that it is absent.
Search for 7:
- At layer 2, the next key after the start is 4. It is less than 7, so move to 4. The next key is 8, which is not less than 7. Drop down.
- At layer 1, from 4 the next key is 6. Move to 6. The next key is 8. Drop down.
- At layer 0, from 6 the next key is 7. This is the target.
You visited about 5 nodes instead of walking 7 steps. With 8 keys the gain is small. With a million keys it is large.
Why the layers help
Suppose each layer keeps every second key of the layer below. Layer 0 has n keys, layer 1 has n/2, layer 2 has n/4, and so on. The number of layers is about log₂ n. On each layer you take at most one step before dropping, because the next key in the layer above already skipped over at most one key here. So the search costs about 2 · log₂ n steps, which is O(log n).
| Structure | Search cost | Extra pointers |
|---|---|---|
| Sorted linked list | O(n) | none |
| Layered list (every second key promoted) | O(log n) | about n more in total |
The extra space is n/2 + n/4 + n/8 + ... which adds up to less than n.
The catch: keeping the layers regular
The perfect "every second key" layout is fragile. Insert a key at the front and every later key shifts its position, so the promoted keys no longer alternate. Repairing the layout can take O(n) work. This is the same problem that balanced trees solve with rotations.
Skip lists avoid this with a different idea. Instead of enforcing the pattern, each new key decides its own height with a coin flip. On average, half the keys reach layer 1, a quarter reach layer 2, and so on. That matches the ideal shape in expectation, and no global repair is ever needed. A later chapter covers how those random heights are chosen.
Exercise
A layered list has 1,024 keys. Each layer keeps every second key of the layer below. Roughly how many layers are there, and how many keys does the top layer hold if you stop when a layer has 2 or fewer keys?
Show answer
Layer sizes are 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2. That is 10 layers (layers 0 to 9), about log₂ 1024 = 10. The top layer holds 2 keys.
Exercise
Using the example layers in this chapter (layer 2: 4, 8; layer 1: 2, 4, 6, 8; layer 0: 1 to 8), trace a search for the key 5. List the node you stand on at the end of each layer.
Show answer
At layer 2, move to 4 (next is 8, which is not less than 5), so you end on 4. At layer 1, from 4 the next key is 6, which is not less than 5, so you stay on 4. At layer 0, from 4 the next key is 5, which is the target.