Sub topic 10
Skip lists compared with balanced trees
This chapter assumes you have read the earlier chapters on skip list search, insertion, deletion and expected complexity. It also assumes you know what a binary search tree is and have heard of rotations in AVL or red-black trees.
What both structures offer
A skip list and a balanced binary search tree (BST) solve the same problem. Both keep keys sorted, and both support search, insert and delete in logarithmic time. The differences lie in how they keep that cost low, and what they cost in memory and code.
The central difference: guarantee versus probability
A red-black or AVL tree rebalances with rotations after every change. This gives a worst case of O(log n) for each operation, whatever the input order.
A skip list does no rebalancing. Its shape depends only on the random heights chosen at insertion. The cost is O(log n) in expectation, and the chance of a badly unbalanced list falls off very fast as n grows. Input order does not matter, so sorted input is not a bad case. An adversary who can predict your random numbers could build a slow list, so use an unpredictable random source if keys come from untrusted users.
Side-by-side comparison
| Property | Skip list | Balanced BST (AVL, red-black) |
|---|---|---|
| Search, insert, delete | O(log n) expected | O(log n) worst case |
| Rebalancing | None | Rotations and recoloring |
| Extra data per node | Random height | Color or height field |
| Pointers per node | About 1/(1-p), so 2 at p = 0.5 | 2 children, often a parent |
| Ordered scan | Follow `forward[0]` | In-order traversal |
| Code complexity | Low | Moderate to high |
| Concurrent updates | Local pointer splices | Rotations touch ancestors |
Memory use
A tree node always stores two child pointers. A skip list node has a random number of forward pointers. With promotion probability p, the expected count is 1/(1-p). You can check this by simulation:
import random
def random_level(p=0.5, max_level=16):
level = 1
while level < max_level and random.random() < p:
level += 1
return level
for p in (0.5, 0.25):
heights = [random_level(p) for _ in range(100_000)]
print(p, sum(heights) / len(heights))The average is near 2.0 for p = 0.5 and near 1.33 for p = 0.25. Lowering p saves pointers, but it makes searches visit more nodes per layer. A tree node also carries balance metadata, so the totals are close. A skip list with p = 0.25 can use fewer pointers than a tree.
Ordered traversal and range queries
The bottom layer of a skip list is an ordinary sorted linked list. To read all keys from 40 to 90, search for 40 and then follow forward[0] until a key exceeds 90. A tree needs an in-order traversal, which uses a stack, parent pointers or recursion. Both approaches work, but the skip list scan is simpler and walks a single list.
Implementation effort
Skip list insertion and deletion splice pointers using the update array. Red-black deletion has several cases, each with its own rotations and recolorings. Skip lists are much easier to write correctly, which is a real advantage in a small codebase.
Concurrency
A skip list update changes pointers only around one node. A tree rotation can change links near the root, which makes fine-grained locking harder. This is why concurrent ordered maps often use skip lists. The concurrency chapter covers this in detail.
Choosing between them
Summary of trade-offs
- Choose a balanced tree when you need a deterministic worst-case bound, such as in real-time code.
- Choose a skip list when you want simple code, cheap range scans, or concurrent updates.
- Choose a tree if you cannot protect the random source from an adversary.
Exercise
A skip list uses p = 0.25 instead of 0.5. What is the expected number of forward pointers per node, and what do you give up in return?
Show answer
The expected count is 1/(1-0.25), about 1.33 pointers per node, compared with 2 at p = 0.5. You give up search speed: fewer nodes appear in the upper layers, so a search walks further along each layer, and the constant factor in the O(log n) cost rises.