Sub topic 2
From Binary Search to the Problem with Binary Trees
This chapter assumes you know basic SQL, how binary search works on a sorted array, and what a tree is. It also assumes you know that disks are read in blocks (pages), which the previous chapter covered. Here you will see why binary search is the right idea but a binary tree is the wrong shape for data on disk.
Binary search on a sorted array
Binary search finds a key in a sorted array by repeatedly halving the range. For n keys it needs about log2(n) comparisons. For one million keys that is about 20 comparisons.
from bisect import bisect_left
keys = [2, 5, 8, 12, 16, 23, 38, 56, 72, 91]
def contains(sorted_keys, target):
i = bisect_left(sorted_keys, target)
return i < len(sorted_keys) and sorted_keys[i] == target
print(contains(keys, 23)) # True
print(contains(keys, 24)) # FalseA sorted array has two problems as a database index:
- Inserting a key in the middle shifts every later element. That costs O(n) moves.
- The array must sit in one contiguous block, which is hard to maintain for a table that keeps growing.
Binary search trees fix the insert cost
A binary search tree (BST) is a tree where each node holds one key. Keys in the left subtree are smaller, and keys in the right subtree are larger. Inserting adds a new leaf and moves nothing. A lookup follows one path from the root, so it costs about log2(n) node visits when the tree is balanced.
Self-balancing variants such as AVL and red-black trees keep the height near log2(n) by rotating nodes after inserts and deletes. In memory, they work well.
Why binary trees are a poor fit for disk
On disk, the cost that matters is the number of pages read, not the number of comparisons. A binary tree node holds one key and two child pointers, which is tiny compared with a page of 8 KB. Each node is likely to live on a different page, so each step down the tree is a separate page read.
The height of a balanced binary tree is about log2(n):
| Keys (n) | Binary tree height | Page reads if each node is on its own page |
|---|---|---|
| 1,000 | about 10 | about 10 |
| 1,000,000 | about 20 | about 20 |
| 100,000,000 | about 27 | about 27 |
The next code block estimates this and compares it with a tree whose nodes hold many keys.
import math
n = 100_000_000
binary_height = math.ceil(math.log2(n))
wide_height = math.ceil(math.log(n, 512)) # 512 keys per node
print(binary_height) # 27
print(wide_height) # 3The difference is the base of the logarithm. Each binary node splits the search space in two. A node that fills a whole page can split it into hundreds of parts. The same 100 million keys then need about 3 page reads instead of 27.
Other problems with binary trees
- Poor locality. Nodes created at different times end up scattered across the disk. Nearby keys are not stored near each other, so a range query touches many pages.
- Rebalancing cost. Rotations rewrite several nodes that may be on different pages, which means more writes.
- Wasted space. Each key carries pointer overhead, and pages are mostly empty if a node is the only thing stored there.
What the next chapter changes
The fix is to make each node as large as one page and to store many sorted keys in it. A node with k keys has k + 1 children, so the tree is wide and short. That structure is the B-tree.
Exercise
A balanced binary search tree holds 1,000,000 keys, and each node lives on its own page. Roughly how many page reads does a lookup need, and why does a tree whose nodes hold 100 keys need fewer?
Show answer
About 20 page reads, since the height is about log2(1,000,000). A node holding 100 keys has up to 101 children, so the height is about log base 101 of 1,000,000, which is about 3. Each page read narrows the search by a factor of about 100 instead of 2.