Data Structures and Algorithms
What is the main reason that B-trees (including B+ trees), rather than binary search trees, are widely used for indexes in databases and file systems?
- AEach node holds many keys, which keeps the tree shallow and narrows the range greatly with each node read, reducing the number of block reads from disk or similar storage
- BEach node holds only one key, so fewer comparisons are needed than in a binary search tree
- CThe tree does not need to be reshaped on insertion, so it is simpler to implement than a binary search tree
Show answer and explanation
Answer: A. Each node holds many keys, which keeps the tree shallow and narrows the range greatly with each node read, reducing the number of block reads from disk or similar storage
Disks and SSDs read data in blocks of several kilobytes even when only one byte is needed, so reducing the number of reads matters. A B-tree puts hundreds of keys into one node (one block), so each node read branches hundreds of ways; even 100 million records can be reached in 3 to 4 levels. When an insertion or deletion overfills a node it is split, and when a node becomes too empty it is merged, keeping all leaves at the same depth. A binary search tree with 100 million records needs at least about 27 levels, and reading a different block at each level is slow.
- A. Correct. A high branching factor keeps the tree shallow and reduces the number of block reads.
- B. Holding many keys per node is what characterizes a B-tree.
- C. B-trees also reshape themselves by splitting and merging nodes, keeping all leaves at the same depth.