Sub topic 15
B+ Tree Search, Range Queries, Insertion and Deletion
Assume that you know balanced search trees, binary search, logarithms, and that disk I/O happens one block at a time. A B+ tree is a balanced multiway index tree designed to reduce block accesses.
Structure and Search
A B+ tree has two kinds of nodes:
- Internal nodes contain separator keys and pointers to child nodes.
- Leaf nodes contain search keys and record pointers.
- Leaf nodes are linked from left to right.
- Every leaf is at the same depth.
Internal nodes usually do not contain complete record pointers. A separator key is commonly a copy of the smallest key in its right child. Therefore, a search for a key equal to a separator follows the right child.
For example, if the root contains [15], keys smaller than 15 go to the left child, and keys greater than or equal to 15 go to the right child.
To find a key, read the root, choose a child, and repeat until reaching a leaf. Then search the leaf for the key. If the tree has height h, the search normally needs about h block accesses, including the leaf. A final access may be needed to fetch the actual data record.
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:
node = node.children[bisect_right(node.keys, key)]
if key in node.keys:
return node.records[node.keys.index(key)]
return None
left = Node([5, 10], records=["record-5", "record-10"])
right = Node([15, 20], records=["record-15", "record-20"])
root = Node([15], children=[left, right])
print(search(root, 15)) # record-15Range Queries
A range query asks for keys such as 20 <= key <= 50.
- Search for the first key in the range.
- Scan the leaf from that key onward.
- Follow each leaf's next pointer.
- Stop after passing the upper bound.
This is efficient because the tree is used only to find the starting point. The remaining records are read sequentially. A range query costs approximately the tree height plus the number of leaf blocks containing matching entries.
> callout note > B+ trees are especially useful for range queries because all data entries are stored in sorted, linked leaves. A hash index is usually better for equality searches but does not naturally support ordered ranges.
Insertion
To insert a key:
- Search for the target leaf.
- Insert the key and record pointer in sorted order.
- If the leaf fits, update any affected separator.
- If it overflows, split it into two leaves.
- Copy the first key of the new right leaf into the parent.
- If the parent overflows, split the internal node and continue upward.
A split can reach the root. If the root splits, create a new root, increasing the tree height by one.
Deletion
To delete a key, remove it from its leaf. If the leaf still satisfies its minimum occupancy, update the parent separator if necessary.
If it underflows:
- Borrow an entry from a neighboring leaf when possible, then update the parent separator.
- Otherwise merge it with a neighbor and remove the corresponding child pointer and separator from the parent.
- If the parent underflows, repeat redistribution or merging upward.
- If the root has only one child after a merge, remove the root and make that child the new root.
Insertion and deletion preserve balance, so all searches continue to have similar cost.
Exercise
A B+ tree has height 3, counting the root and the leaf level. Approximately how many index block accesses are needed to locate a key, and why is a range scan cheaper than repeatedly starting new searches?
Show answer
Locating the key needs about 3 index block accesses. A range scan reuses the linked-leaf pointers after the first search, so it avoids repeating the root-to-leaf traversal for every matching key.