Sub topic 10
B-trees and Ordered Indexes on Disk
This chapter assumes you know Big-O notation, how a binary search tree works, and the ordered-map operations from earlier chapters (floor, ceiling, range views). It covers why trees built for memory perform poorly on disk, and how a B-tree fixes that.
Why disk changes the problem
A red-black tree has two children per node. Looking up one key in a million-entry tree follows about 20 pointers. In memory, each hop costs nanoseconds. On disk, storage is read in fixed-size blocks called pages, commonly 4 to 16 KB. Each page read costs far more than a memory access, and reading 1 byte costs about the same as reading the whole page.
So the cost to minimize is the number of pages touched, not the number of comparisons. A binary tree wastes each page read, because it uses one key and two pointers from a block that could hold hundreds.
The B-tree idea
A B-tree is a balanced search tree where each node holds many keys and is sized to fill one page. A node with k keys has k + 1 children. The keys inside a node are sorted, and they split the key space into ranges, one range per child.
To find a key:
- Read the root page.
- Binary search its keys to pick the child whose range contains the target.
- Read that child page and repeat until you reach a leaf.
If a node holds 200 keys, the tree has a branching factor of about 200. Three levels then address about 200³ = 8 million keys. Four levels address about 1.6 billion. A lookup costs 3 or 4 page reads, and the top levels are usually cached in memory.
Staying balanced
All leaves sit at the same depth. The tree stays balanced through two operations instead of rotations:
- Split. When an insert overflows a full node, the node splits into two halves. The middle key moves up into the parent. If the parent overflows, it splits too. Only a root split makes the tree one level taller.
- Merge or borrow. When a delete leaves a node under about half full, it borrows a key from a sibling or merges with it.
Because nodes stay at least about half full, the height remains O(log n) with a large logarithm base. Lookup, insert, and delete each cost O(log n) page reads.
B+ trees and range scans
Most databases and file systems use a variant called a B+ tree. Internal nodes hold only separator keys for routing. All entries live in the leaves, and the leaves are linked in a chain in key order.
This layout suits the range queries from earlier chapters. To run the equivalent of subMap(70, 90), you descend once to find the leaf containing 70. Then you follow the leaf links until you pass 90. The scan reads leaves sequentially, which disks handle well. Navigation operations such as ceiling are the same descent, stopped at the first key greater than or equal to the target.
Where you meet them
- Relational databases such as PostgreSQL and MySQL (InnoDB) use B+ trees for ordinary indexes.
- File systems such as NTFS, ext4 (directory indexes), and Btrfs use B-tree variants.
- SQLite stores both tables and indexes as B-trees.
An index on a column is an ordered map from column value to row location. That is why ORDER BY and BETWEEN queries can use it, while a hash index only supports equality.
Trade-offs
| Property | Binary tree (red-black) | B-tree / B+ tree |
|---|---|---|
| Node size | One key | Hundreds of keys |
| Height for 1 billion keys | About 30 | About 4 |
| Best medium | RAM | Disk or SSD |
| Range scan | Pointer chasing | Sequential leaf walk |
| Write cost | Rotations | Page splits and rewrites |
Exercise
A B+ tree has a branching factor of 100 and three levels (root, one internal level, leaves). Roughly how many keys can it index, and how many page reads does a lookup need with no caching?
Show answer
About 100 × 100 × 100 = 1,000,000 keys. A lookup reads one page per level, so 3 page reads.
Exercise
Why does a B+ tree answer a query for all keys between 70 and 90 with one descent plus a sequential walk, while a plain binary search tree needs more work?
Show answer
The B+ tree's leaves are linked in key order. After descending to the leaf holding 70, the scan follows the links until it passes 90. A plain binary tree has to move up and down between nodes through an in-order traversal, which touches scattered memory or pages.