TRT
Sign in
mediumAdvancedconcept

Why do databases use B-trees instead of binary search trees?

Explain page-based storage, B-tree fan-out and height, splits that grow the tree at the root, and B+ tree range scans.

TRT practice prompt — not a verified question from a named employer.

The Runtime Theory Team1 min read
Learn it firstB-trees and pagesPro — members onlyA B-tree stores hundreds of keys per node, sized to fit one disk page or a few cache lines, so a tree of billions of keys is only three or four levels tall. Nodes split as they fill, and the tree grows at the root, keeping every leaf at the same depth.

A strong answer

Storage is read in pages of several kilobytes, and a page read costs thousands to millions of times more than a comparison. A binary tree over a billion keys has about thirty levels, each potentially a separate page read. The cost that matters is page reads, not comparisons.

A B-tree makes each node a page holding hundreds of sorted keys and child pointers. Search binary-searches inside the node, then follows one child, so the height is about log base B of n: with 500 keys per page, three levels hold 125 million keys and four hold over 60 billion. The top levels are small and stay cached, so a lookup usually reads one or two pages from storage.

Inserts go into a leaf; a full leaf splits, pushing its middle key into the parent, and splits can cascade. When the root splits, a new root is created above it, so the tree grows at the top and all leaves stay at the same depth, with no rotations. Deletes borrow from or merge with siblings.

Databases use B+ trees: records live only in leaves, internal nodes hold routing keys for higher fan-out, and leaves are linked, so a range query descends once and then reads consecutive leaves. That is also why an index on (a, b) serves queries on a, or a and b, but not b alone. The same idea helps in memory, because a wide node costs one or a few cache misses.

What the interviewer is listening for

  • Page reads as the cost model, and fan-out reducing height.
  • Splits and growth at the root.
  • B+ tree leaves and range scans.

Common mistakes

  • Arguing in terms of comparisons only.
  • Describing B-trees as binary trees stored on disk.
  • Forgetting the cached upper levels in the cost estimate.

This answer walks

Practice follow-ups

  1. 01How tall is a B-tree with a billion keys and 500 keys per page?
  2. 02What happens when the root splits?
  3. 03Why does the column order in a composite index matter?
  4. 04Would you use a B-tree for an in-memory map? Why?

Not started

Sign in to save your learning progress.

Sign in to save