Sub topic 11
B-tree structure, order and balance rules
This chapter defines the B-tree, its order, and the rules that keep it balanced. It assumes you know the multilevel index idea from the previous chapter, binary search trees, and ceiling and floor arithmetic. It does not cover insertion or deletion, which come in the next chapter.
From multilevel index to B-tree
A multilevel index is a static structure. Inserting a record into the file forces you to rebuild or patch the index levels. A B-tree is a multiway search tree that reorganises itself locally as records come and go. The tree stays balanced, so every search costs about the same number of block accesses.
Each node of a B-tree occupies exactly one disk block. Reading a node is one block access, so the cost of a search is measured in nodes visited. A tree with a large fan-out (children per node) is shallow, and a shallow tree means few block accesses.
Anatomy of a node
A B-tree node of order p holds up to p tree pointers and up to p-1 keys. In the usual GATE definition, each key in a B-tree node is stored together with a record pointer (also called a data pointer), because every key in the tree, including those in internal nodes, points to a real record.
A node looks like this:
P1 | <K1, Pr1> | P2 | <K2, Pr2> | ... | P(p-1) | <K(p-1), Pr(p-1)> | Pp- Pi is a tree pointer to a child node. In a leaf, the tree pointers are null.
- Ki is a search key, and Pri is the record pointer for that key.
- Keys inside a node are sorted: K1 < K2 < ... < K(p-1).
- All keys in the subtree under Pi lie between K(i-1) and Ki.
Order and balance rules
The order p of a B-tree is the maximum number of tree pointers (children) a node can have. Some textbooks define order differently, for example as the minimum degree t (CLRS) or as the maximum number of keys. State your convention before you compute. The convention used here is the one GATE questions normally use: order means maximum children.
| Node type | Minimum children | Maximum children | Minimum keys | Maximum keys |
|---|---|---|---|---|
| Root, if it is a leaf | 0 | 0 | 1 | p - 1 |
| Root, if it is internal | 2 | p | 1 | p - 1 |
| Internal non-root | ceil(p/2) | p | ceil(p/2) - 1 | p - 1 |
| Leaf non-root | 0 | 0 | ceil(p/2) - 1 | p - 1 |
The balance rules are:
- Every leaf is at the same level. This is the property that makes search cost uniform.
- Every non-root node is at least half full: it has at least ceil(p/2) children.
- The root has at least 2 children unless it is a leaf. An empty tree has no root.
- A node with c children holds c - 1 keys.
The diagram shows a valid tree of order 3. Every node has at most 2 keys and 3 children. Every non-root node has at least 1 key, and all leaves sit at level 3... more precisely at level 2, directly below the root.
Check the rules against the diagram. The root has 3 children, which is at most p = 3. Each leaf has 1 or 2 keys, which is between ceil(3/2) - 1 = 1 and p - 1 = 2. The leaves are all one level below the root.
Deriving the order from the block size
A node must fit in one block of size B. Let P be the size of a tree pointer, K the size of a key, and Pr the size of a record pointer. A full node has p tree pointers and p - 1 pairs of key and record pointer:
p P + (p - 1) (K + Pr) <= B
Solving for p gives p <= (B + K + Pr) / (P + K + Pr). Take the floor, because p must be a whole number.
Worked example: B = 512, K = 8, P = 6, Pr = 7.
- p <= (512 + 8 + 7) / (6 + 8 + 7) = 527 / 21 = 25.09
- So p = 25.
- Check p = 25: 25 6 + 24 15 = 150 + 360 = 510, which is at most 512.
- Check p = 26: 26 6 + 25 15 = 156 + 375 = 531, which exceeds 512.
Always verify by substituting back. The two checks above confirm that 25 is the largest order that fits.
Counting keys by height
Count levels from the root as level 1. A tree with h levels has height h in the GATE convention. Some questions count height from 0, so read the question carefully.
Maximum keys. Every node is full. Level i has p^(i-1) nodes, each with p - 1 keys. The total is
(p - 1) * (1 + p + p^2 + ... + p^(h-1)) = p^h - 1
Minimum keys. Let t = ceil(p/2). The root has 1 key and 2 children. Every other node has t children and t - 1 keys.
- Level 1 has 1 node.
- Level 2 has 2 nodes.
- Level i, for i >= 2, has 2 * t^(i-2) nodes.
The total is 1 + (t - 1) 2 (1 + t + ... + t^(h-2)) = 1 + 2 * (t^(h-1) - 1), which simplifies to
2 * t^(h-1) - 1
Check this with p = 25, h = 2. Here t = 13, and the formula gives 2 * 13 - 1 = 25. By hand, the root has 1 key and each of 2 leaves has 12 keys, so 1 + 24 = 25.
For h = 3 and p = 25, the maximum is 25^3 - 1 = 15624 keys and the minimum is 2 * 13^2 - 1 = 337 keys.
Height range for n keys
Given n keys, the height is not fixed. A tree that is packed full is short, and a tree that is only half full is tall. The two bounds are:
- Minimum height: the smallest h with p^h - 1 >= n.
- Maximum height: the largest h with 2 * t^(h-1) - 1 <= n.
def btree_order(B, K, P, Pr):
# Largest p with p*P + (p-1)*(K+Pr) <= B
return (B + K + Pr) // (P + K + Pr)
def key_bounds(p, h):
t = -(-p // 2) # ceil(p/2)
return 2 * t ** (h - 1) - 1, p ** h - 1
def height_range(n, p):
t = -(-p // 2)
h_min = 1
while p ** h_min - 1 < n:
h_min += 1
h_max = 1
while 2 * t ** h_max - 1 <= n:
h_max += 1
return h_min, h_max
p = btree_order(B=512, K=8, P=6, Pr=7)
print(p) # 25
print(key_bounds(p, 3)) # (337, 15624)
print(height_range(1_000_000, p)) # (5, 6)The code uses integer loops instead of logarithms. Floating-point logarithms can land just below an integer and give an off-by-one height.
For one million keys at order 25, the tree has 5 or 6 levels. A search therefore costs at most 6 block accesses. It can cost fewer, because a B-tree search can stop at an internal node as soon as it finds the key.
Common errors
- Confusing order with the number of keys. Order 25 means up to 25 children and 24 keys.
- Forgetting the record pointer. In a B-tree, every key has one, including keys in internal nodes. This is why B-tree nodes hold fewer keys than B+ tree internal nodes of the same block size.
- Using ceil(p/2) for the root. The root only needs 2 children.
- Mixing height conventions. Decide whether the root is level 0 or level 1 before applying a formula.
- Using the wrong rounding. The order is a floor of the inequality. The minimum children is a ceiling.
Exercise
A B-tree node must fit in a block of 1024 bytes. A key is 10 bytes, a tree pointer is 8 bytes, and a record pointer is 8 bytes. Find the order p, the minimum number of keys in a non-root node, and the maximum number of keys in a tree of 3 levels.
Show answer
The inequality is p8 + (p-1)(10+8) <= 1024, so 26p <= 1042 and p = 40. Check: 408 + 3918 = 320 + 702 = 1022 <= 1024, and p = 41 gives 328 + 720 = 1048 > 1024. The minimum keys in a non-root node is ceil(40/2) - 1 = 19. The maximum keys in 3 levels is 40^3 - 1 = 63999.
Exercise
A B-tree of order 5 has height 3 (root at level 1). What are the minimum and maximum numbers of keys it can hold?
Show answer
Here t = ceil(5/2) = 3. The minimum is 2t^(h-1) - 1 = 29 - 1 = 17. As a check, the root has 1 key, level 2 has 2 nodes of 2 keys each (4 keys), and level 3 has 6 nodes of 2 keys each (12 keys), giving 1 + 4 + 12 = 17. The maximum is 5^3 - 1 = 124.