Sub topic 14
Computing Node Order and Tree Height from Block Size
Prerequisites and the Cost Model
Assume you know fixed-size disk blocks, logarithms, ceiling arithmetic, and the basic structure of balanced search trees. You should also know that a database usually counts block accesses, not individual comparisons. Reading one block brings many keys into memory.
The order of a B-tree is its maximum number of child pointers in an internal node. If an internal node has order \(p\), it can contain at most \(p-1\) keys.
Deriving B-tree Order
Let:
- \(B\) be the block size.
- \(K\) be the size of one search key.
- \(P\) be the size of one child pointer.
An internal node with \(p\) child pointers contains \(p-1\) keys. Therefore, the space condition is:
\[ pP + (p-1)K \leq B \]
Rearranging gives:
\[ p(K+P) \leq B+K \]
Thus, the largest valid order is:
\[ p = \left\lfloor \frac{B+K}{K+P} \right\rfloor \]
This calculation assumes that all keys and pointers are stored in the same block and that each pointer has the same size.
| Quantity | Formula |
|---|---|
| Maximum internal order | \(\left\lfloor\frac{B+K}{K+P}\right\rfloor\) |
| Maximum internal keys | \(p-1\) |
| Maximum children at height \(h\) | \(p^h\), approximately |
Some textbooks define B-tree order as the maximum number of keys rather than pointers. Always check the convention before solving a numerical question.
Deriving B+ Tree Order
A B+ tree keeps search keys and child pointers in internal nodes. The actual record pointers are stored in leaf nodes.
For an internal node, the same inequality applies:
\[ pP + (p-1)K \leq B \]
For a leaf containing \(pL\) entries, let \(PR\) be the size of a record pointer. If each leaf also stores one pointer to the next leaf, then:
\[ pL(K+PR) + P \leq B \]
Therefore:
\[ pL = \left\lfloor\frac{B-P}{K+PR}\right\rfloor \]
The leaf order and internal order can differ.
def bplus_orders(B, K, P, Pr):
internal_order = (B + K) // (K + P)
leaf_order = (B - P) // (K + Pr)
return internal_order, leaf_order
print(bplus_orders(512, 8, 6, 7))
# (37, 33)This tree has at most 37 child pointers in an internal node and 33 record entries in a leaf.
Computing Height
Suppose a B+ tree stores \(r\) records and each leaf holds at most \(p_L\) records. The number of leaf blocks required is:
\[ L = \left\lceil\frac{r}{p_L}\right\rceil \]
If the root-to-leaf path has \(h\) edges, the maximum number of leaf blocks is approximately:
\[ p^h \]
The smallest height satisfying the capacity requirement is:
\[ h = \left\lceil \log_p L \right\rceil \]
A search then reads approximately \(h+1\) blocks: one block for each internal level and one leaf block. If the root is counted as level 1 instead of level 0, the stated height may differ by one. State your convention.
Worked Strategy
For a numerical question:
- Write the exact space inequality.
- Solve using floor division.
- Compute the number of data or leaf blocks with a ceiling.
- Apply logarithms to the relevant fan-out.
- Check whether the question counts levels or block accesses.
Exercise
A B+ tree has block size 1024 bytes, key size 9 bytes, internal pointer size 6 bytes, and record pointer size 7 bytes. What are the maximum internal order and leaf order?
Show answer
Internal order = floor((1024 + 9) / (9 + 6)) = floor(1033 / 15) = 68. Leaf order = floor((1024 - 6) / (9 + 7)) = floor(1018 / 16) = 63.