Sub topic 14
Computing node order and tree height from block size
This chapter turns block size and field sizes into the order of a B+ tree node. It then turns the order into a range of possible tree heights and a block-access cost. I assume you know what a B+ tree is (internal nodes with separator keys, linked leaves holding record pointers) and how to compute a blocking factor.
The idea: one node fills one block
A tree node is stored in one disk block, so a node can be as large as the block and no larger. The order is the largest number of entries that satisfy the block-size inequality. Everything else in the chapter follows from two numbers: the internal order p and the leaf order p_leaf.
The symbols used below:
| Symbol | Meaning |
|---|---|
| B | Block size in bytes |
| K | Size of a search key |
| P | Size of a block (tree) pointer |
| Pr | Size of a record pointer |
| p | Order of an internal node: maximum number of tree pointers |
| p_leaf | Order of a leaf: maximum number of (key, record pointer) pairs |
Deriving the two inequalities
An internal node of a B+ tree holds up to p tree pointers and p - 1 keys. It carries no record pointers. The condition is:
p * P + (p - 1) * K <= BRearranging gives p * (P + K) <= B + K, so p = floor((B + K) / (P + K)).
A leaf holds p_leaf pairs of a key and a record pointer, plus one pointer to the next leaf:
p_leaf * (K + Pr) + P <= BSo p_leaf = floor((B - P) / (K + Pr)).
A plain B-tree node differs because every node stores record pointers next to its keys:
p * P + (p - 1) * (K + Pr) <= Bdef bplus_orders(B, K, P, Pr):
internal_order = (B + K) // (K + P)
leaf_order = (B - P) // (K + Pr)
return internal_order, leaf_order
def btree_order(B, K, P, Pr):
# p*P + (p-1)*(K+Pr) <= B -> p <= (B + K + Pr) / (P + K + Pr)
return (B + K + Pr) // (P + K + Pr)
print(bplus_orders(B=512, K=8, P=6, Pr=7)) # (37, 33)
print(btree_order(B=512, K=8, P=6, Pr=7)) # 25With the same block, the B+ internal node reaches order 37 and the B-tree node only 25. The B+ internal node does not spend space on record pointers, so its fan-out is higher.
Occupancy rules and capacity per height
The balance rules fix how empty a node may be. These are the usual textbook rules, and I use them throughout:
- A non-root internal node has at least
ceil(p / 2)children. - The root has at least 2 children, unless it is also the only leaf.
- A leaf holds at least
ceil(p_leaf / 2)keys. - All leaves are on the same level.
Count levels from the root, so a tree with h levels has its leaves at level h. The record capacity is then:
| Quantity | Formula for h levels |
|---|---|
| Maximum leaves | p^(h-1) |
| Maximum records | p^(h-1) * p_leaf |
| Minimum leaves | 2 * ceil(p/2)^(h-2) |
| Minimum records | 2 * ceil(p/2)^(h-2) * ceil(p_leaf/2) |
The minimum formulas apply for h >= 2. The root contributes the factor 2, and every other internal level contributes ceil(p/2).
Worked example
Take B = 512, K = 8, P = 6, Pr = 7, which gives p = 37 and p_leaf = 33. The file has 1,000,000 records.
Maximum capacity for each number of levels:
- 3 levels: 37^2 * 33 = 45,177 records.
- 4 levels: 37^3 * 33 = 1,671,549 records.
Three levels cannot hold 1,000,000 records, so the tree has at least 4 levels.
Minimum records, with ceil(37/2) = 19 and ceil(33/2) = 17:
- 5 levels: 2 19^3 17 = 233,206 records.
- 6 levels: 2 19^4 17 = 4,430,914 records.
A 6-level tree needs at least 4,430,914 records, so one million records cannot need that many levels. The tree has 4 or 5 levels.
A search reads one block per level. If the leaf holds a record pointer, it reads one more block for the data record. The cost is therefore 5 to 6 block accesses, with 5 when the tree is as full as possible.
def bplus_levels(n, p, leaf):
m = (p + 1) // 2 # ceil(p/2)
lmin = (leaf + 1) // 2 # ceil(leaf/2)
def min_records(h):
return lmin if h == 1 else 2 * m ** (h - 2) * lmin
h_min = 1
while leaf * p ** (h_min - 1) < n:
h_min += 1
h_max = 1
while min_records(h_max + 1) <= n:
h_max += 1
return h_min, h_max
print(bplus_levels(1_000_000, 37, 33)) # (4, 5)Edge cases that change the answer
- Pointer sizes. The question may give a record pointer and a block pointer with different sizes. Do not use one
Pfor both. Tree pointers useP, and leaf entries usePr. - Header bytes. Some questions reserve space in each node for a count or flag. Subtract it from
Bbefore applying the formulas. - Key count, not pointer count. "Order" can mean maximum pointers (used here), maximum keys, or maximum children. Check the definition. If a question gives "order m = maximum keys", then the internal node has
m + 1pointers. - Root versus other nodes. The root's minimum of 2 children is why the minimum formula starts with a factor of 2 instead of
ceil(p/2). - Floor, not round. The order is the largest integer satisfying the inequality, so always round down. A node that does not fit in the block is not allowed.
Useful checks
- Verify the final order by substituting it back into the inequality, and check that
p + 1fails. The code example above passes this: 37 gives 510 bytes, and 38 gives 524. - To find the number of leaf blocks for
rrecords, divide by the average fill, not the maximum. If the question says nodes are 69% full, use0.69 * p_leafentries per leaf. - An index with
rentries needsceil(r / p_leaf)leaves at full packing. Each level above hasceil(count / p)nodes, until one node remains. This bottom-up count gives the number of levels directly and also gives the total number of index blocks.
Exercise
A block is 1024 bytes. A key is 10 bytes, a block pointer is 8 bytes, and a record pointer is 9 bytes. Find the internal order and the leaf order of a B+ tree.
Show answer
Internal: p 8 + (p - 1) 10 <= 1024, so p <= 1034 / 18 = 57.4, and p = 57 (check: 456 + 560 = 1016 <= 1024). Leaf: pleaf * (10 + 9) + 8 <= 1024, so pleaf <= 1016 / 19 = 53.5, and p_leaf = 53 (check: 1007 + 8 = 1015 <= 1024).
Exercise
Using the tree from the previous exercise (internal order 57, leaf order 53), what is the maximum number of records a tree with 3 levels can index, and what is the minimum number of records it must hold?
Show answer
Maximum: 57^2 53 = 3249 53 = 172,197 records. Minimum: the root has 2 children, each non-root internal node has at least ceil(57/2) = 29 children, and each leaf has at least ceil(53/2) = 27 keys. That gives 2 29 27 = 1,566 records.