Module 18 of 39

B-trees: page-based indexes and balanced search

See why B-trees use wide nodes and how splitting, borrowing, and merging keep them balanced.

A binary search tree makes one key decision at each node. That can work well in memory. When data is stored in pages on disk or another block device, reaching a new page may cost much more than comparing another key already on the page. A B-tree puts several sorted keys in one node, with child links between the ranges they separate. One page access can therefore rule out many keys.

Background for this page. You should understand sorted keys and search-tree child ranges. A page is a block read together from storage. Counting page accesses is a different cost model from counting comparisons. Related explanations: Binary search trees: search, insertion, and deletion · Augmenting search trees: subtree sizes and rank.

For a B-tree with minimum degree t, a non-root node has between t − 1 and 2t − 1 keys. An internal node with k keys has k + 1 children. All leaves are at the same depth. Those rules keep the tree balanced and wide. Its height grows like O(logt n), which means few node or page visits even for a large number of keys. Work within a page still counts; “few page visits” is not the same as “no work.”

A root page has keys 20 and 40; searching for 35 chooses the middle child page containing 27, 35, and 38.

To find a key, search the sorted keys in a node. If it is absent, follow the one child whose range can contain it. To insert, find the right leaf. If a node is full, split it and move a middle key up so the capacity rule survives. A common implementation splits full children on the way down, avoiding the need to climb back up afterward. Deletion may borrow a key from a sibling or merge nodes when a child would become too small. These rules are more involved than those of a binary search tree because every node must stay within its size limits.

The term B-tree names a family, not every detail of a database index. Many systems use a close relative called a B+ tree, which keeps data records at leaves and uses internal nodes mainly for navigation. The practical reason is the same: make each costly page read useful by storing many keys together.

The main invariant is worth remembering. Every key range has a child, all leaves have equal depth, and nodes stay within capacity bounds. If an insertion or deletion breaks any of these, a later search may miss a key or the tree may lose its low height. Use a tested storage library for real data; learning the structure helps you understand why the index behaves as it does.

Follow a split without losing a range

For minimum degree two, a node can hold at most three keys. Split a full node containing 10, 20, and 30 by promoting 20 to its parent. The two resulting nodes hold 10 and 30. All keys below 20 still belong on the left; all keys above 20 belong on the right. If the node had children, their ordered ranges must move with the appropriate half.

When a full root splits, a new root is created above it. Every old leaf moves one level farther from the root together, preserving equal leaf depth. This distinguishes balanced growth from simply attaching a longer branch.

A practical index also needs a page format, durability, and concurrent-update rules. The abstract B-tree invariants explain search structure but do not by themselves implement a database.

Practice. With minimum degree t = 3, how many keys may a non-root node contain? Why might such a wide node be better than a binary node when each node is stored on a separate page?

Answer. It may contain from 2 to 5 keys. One page read gives several comparisons and lets the search choose among up to six child ranges, reducing the number of page reads needed.

Try another case. For degree two, may a non-root node have zero keys after deletion finishes?

Reasoning. No. Its minimum is one. Deletion repair must borrow from a suitable sibling or merge nodes while preserving the occupancy rules.

View the full course and choose another topic.

Image preview

Fit