Sub topic 13
B+ Tree Structure: Internal Nodes and Linked Leaves
A B+ tree is a balanced search tree designed for storage in disk blocks. Assume you already know arrays, linked lists, binary search, balanced search trees, logarithms, and the idea that a disk reads a whole block at a time.
A B+ tree has two kinds of nodes:
- Internal nodes guide the search.
- Leaf nodes store search keys and pointers to the matching records.
All leaves are at the same depth. Therefore, every search takes a similar number of block accesses.
Internal nodes
An internal node contains sorted separator keys and pointers to child nodes. If a node has p child pointers, it normally has p - 1 separator keys.
For example:
[15]
/ \
[5, 10] [15, 20]The key 15 separates the two subtrees. Keys smaller than 15 go to the left child. Keys greater than or equal to 15 go to the right child.
A separator key in an internal node is a copy used for navigation. The actual key and its record pointer are stored in a leaf. Internal nodes usually contain no record pointers. This lets them hold many child pointers and gives the tree a high fan-out, or number of children per node.
The root is a special case. It may have fewer children than other internal nodes. Every other internal node must satisfy the minimum occupancy rule for the chosen tree order.
Leaf nodes and linked leaves
Leaf nodes contain:
- Sorted search keys.
- Pointers to records, or to buckets of records.
- A pointer to the next leaf node.
The leaf-to-leaf pointers form a linked list:
The linked leaves make range searches efficient. To find all keys from 15 through 30, the database searches for 15 once. It then follows the leaf links and reads consecutive leaves until it passes 30. It does not need to return to the root for every key.
Searching a B+ tree
Search begins at the root. At each internal node, compare the target key with the separator keys and choose one child. The search stops at a leaf. The leaf is then searched for the key and its record pointer.
from bisect import bisect_right
class Node:
def __init__(self, keys, children=None, records=None):
self.keys = keys
self.children = children
self.records = records
def search(root, key):
node = root
while node.children is not None:
# Equal separator keys go to the right child.
node = node.children[bisect_right(node.keys, key)]
if key in node.keys:
return node.records[node.keys.index(key)]
return None
leaf_left = Node([5, 10], records=["record-5", "record-10"])
leaf_right = Node([15, 20], records=["record-15", "record-20"])
root = Node([15], children=[leaf_left, leaf_right])
print(search(root, 15))The output is:
record-15The search cost is approximately one block access for each level, including the leaf. The exact cost also depends on whether the root or upper levels are cached in memory.
Exercise
Why do linked leaves make a B+ tree efficient for a range query such as keys 100 through 200?
Show answer
After locating the leaf containing 100, the search follows next-leaf pointers and scans consecutive leaves until it passes 200.