B-Trees
Dictionaries for very large files typically reside on secondary storage, such as a disk. The
dictionary is implemented as an index to the actual file and contains the key and record
address of data. To implement a dictionary we could use red-black trees, replacing pointers
with offsets from the beginning of the index file, and use random access to reference nodes
of the tree. However, every transition on a link would imply a disk access, and would be
prohibitively expensive. Recall that low-level disk I/O accesses disk by sectors (typically 256
bytes). We could equate node size to sector size, and group several keys together in each
node to minimize the number of I/O operations. This is the principle behind B-trees. Good
references for B-trees include Knuth [1998] and Cormen [1990]. For B
+
-trees, consult Aho
[1983].
Theory
Figure 4-3 illustrates a B-tree with 3 keys/node. Keys in internal nodes are surrounded by
pointers, or record offsets, to keys that are less than or greater than, the key value. For
example, all keys less than 22 are to the left and all keys greater than 22 are to the right. For
simplicity, I have not shown the record address associated with each key.
Figure 4-3: B-Tree
We can locate any key in this 2-level tree with three disk accesses. If we were to group 100
keys/node, we could search over 1,000,000 keys in only three reads. To ensure this property
holds, we must maintain a balanced tree during insertion and deletion. During insertion, we
examine the child node to verify that it is able to hold an additional node. If not, then a new
sibling node is added to the tree, and the child’s keys are redistributed to make room for the
new node. When descending for insertion and the root is full, then the root is spilled to new
children, and the level of the tree increases. A similar action is taken on deletion, where child
nodes may be absorbed by the root. This technique for altering the height of the tree
maintains a balanced tree.
B-Tree
B*-Tree
B
+
-Tree
B
++
-Tree
data stored in
any node
any node
leaf only
leaf only
on insert, split
1 x 1 –>2 x 1/2
2 x 1 –>3 x 2/3
1 x 1 –>2 x 1/2
3 x 1 –>4 x 3/4