Sub topic 3
B+ tree structure: internal nodes, linked leaves
This chapter assumes you know binary search trees, the idea that a disk is read in fixed-size blocks, and how to compute a ceiling and a logarithm. A B+ tree is a balanced search tree whose nodes each fit in one disk block, so that a lookup reads only a few blocks.
A B+ tree has two kinds of nodes. Internal nodes hold separator keys and child pointers, and they store no record data. Leaf nodes hold the keys with pointers to the records, and every leaf sits at the same depth. Each leaf also has a pointer to the next leaf, so the leaves form a linked list in key order.
Computing node order and tree height from block size
The order of a node is the number of child pointers it can hold. With block size B, key size K, block pointer size P, and record pointer size Pr, the two limits are:
- Internal node: p × P + (p − 1) × K ≤ B
- Leaf node: p_leaf × (K + Pr) + P ≤ B
The code below computes both orders and the height of a tree for a given number of records.
import math
def bplus_orders(B, K, P, Pr):
internal_order = (B + K) // (K + P)
leaf_order = (B - P) // (K + Pr)
return internal_order, leaf_order
def bplus_height(n_records, leaf_order, internal_order):
nodes = math.ceil(n_records / leaf_order)
height = 1 # the leaf level
while nodes > 1:
nodes = math.ceil(nodes / internal_order)
height += 1
return height
internal, leaf = bplus_orders(B=512, K=8, P=6, Pr=7)
print(internal, leaf)
print(bplus_height(100000, leaf, internal))With these values the output is 37 33 and then 4.
B+ tree search, range queries, insertion and deletion
Search starts at the root and follows one child pointer per level until it reaches a leaf. Each level costs one block access. A range query finds the first key in the range, then follows the leaf links until the key passes the upper bound. Insertion goes to the correct leaf. If the leaf overflows, it splits, and the separator key moves up. Deletion removes the key from the leaf. If the leaf falls below half full, it borrows from a sibling or merges with one.
:::
> Note: A key equal to a separator goes to the right child, because separators are copies of the smallest key in the right subtree.
:::
B-tree versus B+ tree: fan-out, range scans, storage
A B-tree stores record pointers in internal nodes too, so each internal node holds fewer keys. A B+ tree keeps internal nodes small, so its fan-out is higher and its height is lower. Its leaves are linked, so a range scan reads consecutive leaves without going back up the tree. Database systems prefer B+ trees for these reasons. The cost is that internal nodes repeat some keys, which uses a little extra storage.
GATE problem patterns and worked strategies
GATE questions on this topic usually ask for one of three things: the maximum number of keys in a tree of a given height, the minimum height for a given number of keys, or the number of block accesses for a search. For maximum keys, use the order: a tree of height h with order p holds at most (p^h − 1) keys when all nodes are full, so the count is the sum of the levels. For block accesses, count one access per level from the root to the leaf, plus any data block read.
Exercise:
Exercise
With B = 512, K = 8, P = 6, and Pr = 7, what is the leaf order of the B+ tree?
Show answer
The leaf order is (512 − 6) // (8 + 7) = 506 // 15 = 33 record pointers per leaf.