Sub topic 13
B+ tree structure: internal nodes, linked leaves
This chapter assumes you know what a block is, how blocking factor works, what a sparse index is, and how a multilevel index lets you reach a record in a few block reads. If any of that is unclear, review the earlier chapters on blocking and multilevel indexes first. Here we fix the exact shape of a B+ tree, because every GATE calculation on it (order, height, minimum and maximum keys) comes from that shape.
What a B+ tree is
A B+ tree is a balanced, multiway search tree used as a multilevel index. Each node is sized to fit exactly one disk block, so reading a node costs one block access. It has two kinds of nodes with different jobs:
- Internal nodes hold only separator keys and tree pointers. They route a search downward and store no record pointers.
- Leaf nodes hold every search key, each paired with a record pointer (or the record itself). Leaves also hold one pointer to the next leaf in key order.
Every key that exists in the file appears in a leaf. Some keys also appear in an internal node as a copy, acting as a signpost. This duplication is the main difference from a B-tree, where each key appears once in the whole tree.
Node layouts
An internal node of order p looks like this:
P1 | K1 | P2 | K2 | ... | K(p-1) | Pp
Pointer Pi leads to the subtree whose keys lie between K(i-1) and Ki. With the convention used in the code below, a key equal to a separator goes to the right subtree. So Pi leads to keys k with K(i-1) ≤ k < Ki.
A leaf node of order p_leaf looks like this:
(K1, R1) | (K2, R2) | ... | (Kq, Rq) | next
Here Ri is the record pointer for Ki, and next points to the neighbouring leaf on the right.
| Property | Internal node | Leaf node |
|---|---|---|
| Contents | Separator keys and tree pointers | Keys with record pointers, plus one next-leaf pointer |
| Maximum fill | p tree pointers and p-1 keys | p_leaf key and record-pointer pairs |
| Minimum fill (non-root) | ceil(p/2) tree pointers | ceil(p_leaf/2) entries |
| Root minimum | 2 children | 1 entry (if the root is a leaf) |
| Stores record pointers | No | Yes |
The order can differ between the two node types. A leaf entry is a key plus a record pointer, so it is usually larger than an internal entry. Therefore p_leaf is normally smaller than p.
Balance rules
- All leaves are at the same depth. Insertions grow the tree at the root, never at one side, so this always holds.
- Every non-root node is at least half full, by the minimums in the table.
- Keys inside every node are sorted.
- Leaves form a singly linked list in ascending key order.
Rules 1 and 2 give a bounded height, as shown in the next two sections.
A small example
The tree below has a root with one separator, 15, and two leaves. Key 15 is in the root as a copy and also in the right leaf, where its record pointer lives.
Searching for 15 goes to the root. Because 15 equals the separator, the search moves right and finds 15 in the second leaf. The cost is 2 block accesses: the root and one leaf. Every search in a B+ tree reaches a leaf, so the cost is always the number of levels, with no early exit from an internal node.
Searching and range scanning in code
The code builds three leaves under one root and runs a range query. The scan finds the first leaf by descending the tree once, then follows next pointers without returning to the root.
from bisect import bisect_right
class Leaf:
def __init__(self, keys, records):
self.keys = keys
self.records = records
self.next = None
class Internal:
def __init__(self, keys, children):
self.keys = keys
self.children = children
def find_leaf(root, key):
node = root
while isinstance(node, Internal):
# a key equal to a separator goes to the right child
node = node.children[bisect_right(node.keys, key)]
return node
def range_scan(root, lo, hi):
leaf = find_leaf(root, lo)
out = []
while leaf is not None:
for k, r in zip(leaf.keys, leaf.records):
if k > hi:
return out
if k >= lo:
out.append((k, r))
leaf = leaf.next
return out
l1 = Leaf([5, 10], ["r5", "r10"])
l2 = Leaf([15, 20], ["r15", "r20"])
l3 = Leaf([25, 30], ["r25", "r30"])
l1.next = l2
l2.next = l3
root = Internal([15, 25], [l1, l2, l3])
print(range_scan(root, 10, 25))The output is [(10, 'r10'), (15, 'r15'), (20, 'r20'), (25, 'r25')]. The cost of this query is the tree height to reach the first leaf, plus one block access for each further leaf read. A B-tree has no leaf chain, so it must climb back up and down through internal nodes to do the same scan.
Deriving the order from block size
The order is the largest value that fits in one block. For internal nodes of order p with key size K and block-pointer size P:
p·P + (p−1)·K ≤ B, so p = floor((B + K) / (K + P))
For leaves with record-pointer size Pr and one next-leaf pointer:
pleaf·(K + Pr) + P ≤ B, so pleaf = floor((B − P) / (K + Pr))
Take B = 512, K = 8, P = 6, Pr = 7:
- p = floor(520 / 14) = 37. Check: 37·6 + 36·8 = 510 ≤ 512.
- p_leaf = floor(506 / 15) = 33. Check: 33·15 + 6 = 501 ≤ 512.
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(B=512, K=8, P=6, Pr=7))This prints (37, 33).
Height and capacity
With the orders above, count levels from the root. A tree with a root, one internal level and a leaf level has 3 levels, so a search costs 3 block accesses.
Maximum entries. The root has 37 children, each of those has 37 children, and so there are 37 × 37 = 1369 leaves, each holding up to 33 entries. Total: 1369 × 33 = 45177.
Minimum entries. The root has at least 2 children. Each node at the next level has at least ceil(37/2) = 19 children. That gives 2 × 19 = 38 nodes at level 2 and 38 × 19 = 722 leaves. Each leaf holds at least ceil(33/2) = 17 entries. Total: 722 × 17 = 12274.
So a 3-level tree under these parameters holds between 12274 and 45177 entries. For a file with N entries, find the smallest number of levels whose maximum capacity is at least N. Use the minimum capacity to find the largest possible height.
Moderate orders keep the tree short. A fan-out of about 37 already reaches tens of thousands of entries in three accesses. Real page sizes of 4 KB or more give fan-outs in the hundreds, so a table with hundreds of millions of rows needs about 4 levels.
Common errors
- Searching stops at an internal node. It does not. The record pointer is only in the leaf, so the cost is always the full height.
- Treating separators as the only copy of a key. Deleting a key from a leaf may leave a stale copy in an internal node. That is allowed, because the copy still works as a valid signpost.
- Using the internal order for leaf capacity. The two orders come from different inequalities and normally differ.
- Applying the minimum fill to the root. The root is exempt. An internal root needs only 2 children, and a leaf root needs only 1 entry.
- Forgetting the data block read. If leaves hold record pointers, finding a record costs the tree height plus 1 access for the data block.
Exercise
A B+ tree is stored in blocks of 1024 bytes. The search key is 10 bytes, a block pointer is 8 bytes, and a record pointer is 9 bytes. Each leaf also has one next-leaf block pointer. Compute (a) the internal order p, (b) the leaf order p_leaf, and (c) the maximum number of entries a 2-level tree (a root that is a leaf-parent plus leaves) can index.
Show answer
(a) p·8 + (p−1)·10 ≤ 1024 gives p = floor((1024 + 10) / 18) = floor(57.44) = 57. Check: 57·8 + 56·10 = 1016 ≤ 1024. (b) pleaf·(10 + 9) + 8 ≤ 1024 gives pleaf = floor(1016 / 19) = floor(53.47) = 53. Check: 53·19 + 8 = 1015 ≤ 1024. (c) The root has up to 57 children, which are leaves, each with up to 53 entries: 57 × 53 = 3021 entries.
Exercise
Why can a range query such as "all keys from 100 to 500" be cheaper in a B+ tree than in a B-tree with the same node size?
Show answer
In a B+ tree, one descent finds the leaf containing 100, and the scan then follows the next-leaf pointers across consecutive leaves, which hold all keys in sorted order. A B-tree stores keys in internal nodes as well, with no leaf chain, so the scan must move up and down the tree through internal nodes to visit keys in order, which costs extra block accesses.