Sub topic 11
Comparing costs: lookup, insert, range scan and memory
This chapter assumes you know Big-O notation, how a sorted array with binary search works, and the basic ordered map operations (floor, ceiling, range views). It compares the four structures from the previous chapters on the costs that decide which one to use.
The cost table
Let n be the number of keys and k the number of results in a range scan. Costs for balanced trees, B-trees and skip lists are worst case for trees and expected for skip lists.
| Structure | Lookup | Insert / delete | Range scan | Memory per key |
|---|---|---|---|---|
| Sorted array | O(log n) | O(n) | O(log n + k) | Lowest, no pointers |
| Balanced BST (AVL, red-black) | O(log n) | O(log n) | O(log n + k) | Two child pointers plus balance data |
| Skip list | O(log n) expected | O(log n) expected | O(log n + k) | Several forward pointers per key, about 2 on average at p = 0.5 |
| B-tree | O(log n) | O(log n) | O(log n + k) | Low, keys packed in nodes |
All four find the start of a range in O(log n). The differences are in the constants, the insert cost, and how the data sits in memory.
Where the differences come from
Sorted array. Insert must shift every later element, so it costs O(n). The shift is a single block copy, and for a few thousand keys it is often faster than pointer chasing. The scan is fastest of all because elements sit next to each other in memory.
Balanced BST. Each node is a separate allocation. A lookup follows about log2(n) pointers, and each one can be a cache miss. Rotations keep the height at O(log n), which is why insert stays cheap.
Skip list. It has the same asymptotic costs as a tree but uses random levels instead of rotations. Its main advantage is simpler concurrent updates. Its cost is extra pointers and an expected rather than guaranteed bound.
B-tree. Each node holds many keys, so the tree is shallow. With 100 keys per node, a billion keys need only about 5 levels. That is why B-trees suit disks, where each level costs one page read.
Measuring the insert gap
The following Python script times building a sorted list with insort. It shows why O(n) inserts matter as n grows.
import time
from bisect import insort
import random
def build(n):
data = []
for _ in range(n):
insort(data, random.random())
return data
for n in (10_000, 40_000, 160_000):
start = time.perf_counter()
build(n)
print(n, round(time.perf_counter() - start, 3), "s")Each 4x increase in n makes the run take much more than 4x as long, because each insert shifts about n/2 elements on average. Exact times vary by machine.
Choosing by cost
- Read-heavy, rarely changed data: sorted array. Build once, then use binary search.
- Mixed reads and writes in memory: balanced BST. This is what
TreeMapand C++std::mapuse. - Concurrent access: skip list, as in Java's
ConcurrentSkipListMap. - Data larger than memory: B-tree.
Exercise
You have 50,000 keys that are loaded once at startup and then queried with range scans millions of times. Which structure would you pick, and why?
Show answer
A sorted array. Loading is a single sort, so the O(n) insert cost never applies. Range scans find the start with binary search in O(log n) and then read contiguous memory, which is the fastest scan and uses the least memory.
Exercise
Why does a B-tree with 100 keys per node need only about 5 levels for a billion keys, and why does this matter on disk?
Show answer
Each level multiplies the reachable keys by about 100, and 100^5 is 10 billion, which covers a billion keys. Each level costs one disk page read, so a lookup needs only a handful of reads instead of the roughly 30 a binary tree would need.