24 - B+ Tree Index
667 words
3 min read
Visual companion
Python
Type and operator map
Python Week 1: the first filter for runtime behavior
View
Revision summary
What this note is really saying
Short form
# 24 - B+ Tree Index ## 🎯 Learning Objectives After reading this topic, you will be able to: - Explain B+ tree structure (internal vs. leaf nodes) - Search, insert, and delete in a B+ tree - Calculate fanout, height, and order - Compare B+ trees with B-trees ## 📖 Core Content ### 24.1 B+ Tree Structure *(Diagram)*...

24 - B+ Tree Index
🎯 Learning Objectives
After reading this topic, you will be able to:
- Explain B+ tree structure (internal vs. leaf nodes)
- Search, insert, and delete in a B+ tree
- Calculate fanout, height, and order
- Compare B+ trees with B-trees
📖 Core Content
24.1 B+ Tree Structure
(Diagram)
Key properties:
- All leaves are at the same depth (balanced)
- Leaves are linked together (for range scans)
- Internal nodes guide the search; leaves contain data pointers
- Fanout (order) = maximum number of children per node
24.2 Order and Capacity
For a B+ tree of order n:
| Node Type | Min Keys | Max Keys | Min Children | Max Children |
|---|---|---|---|---|
| Root | 1 | n | 2 | n |
| Internal | ⌈n/2⌉ | n | ⌈n/2⌉ | n |
| Leaf | ⌈(n−1)/2⌉ | n−1 | — | — |
24.3 Search
- Start at root
- At each internal node, find the smallest key > search key; go to that child
- At leaf, scan for the key; if found, follow pointer to data record Cost: Height of tree (typically 2-4 for millions of records).
24.4 Insertion
- Find the leaf where the key should go
- If leaf has room, insert
- If leaf is full, split it:
- First ⌈(n−1)/2⌉ keys stay
- Remaining keys go to new leaf
- Copy the middle key up to parent
- If parent is full, split parent (internal node split)
- If root splits, create new root (height increases by 1)
24.5 Deletion
- Find and remove the key from the leaf
- If leaf has enough keys (≥ min), done
- If leaf is underfull, try to borrow from sibling
- If no sibling can spare, merge with sibling
- May need to delete from parent (cascading)
24.6 B+ Tree vs. B-Tree
| Feature | B-Tree | B+ Tree |
|---|---|---|
| Data pointers | In all nodes | Only in leaves |
| Leaf linkage | None | Linked list |
| Height | Typically lower | Slightly higher |
| Search (point query) | May find key before leaf | Always goes to leaf |
| Range query | Slow (must traverse up/down) | Fast (follow leaf chain) |
| Insert/Delete | More complex | Simpler |
| Used in DBMS | Rarely | Almost always |
24.7 Worked Example
Given: B+ tree with order n=4 (max 3 keys in leaf, max 4 keys in internal).
Insert keys: 10, 20, 30, 40, 50, 60, 70
Step 1: Insert 10, 20, 30 → all in one leaf node [10, 20, 30] Step 2: Insert 40 → leaf full (3 keys max). Split: left [10, 20], right [30, 40]. Copy 30 to parent (new root). Step 3: Insert 50 → leaf [30, 40] has room → [30, 40, 50] Step 4: Insert 60 → leaf [30, 40, 50] full. Split: left [30, 40], right [50, 60]. Copy 50 to root. Root now [30, 50]. Step 5: Insert 70 → leaf [50, 60] has room → [50, 60, 70]
📝 Practice Questions
Q1. A B+ tree has order 5 (max 5 children). What's the minimum number of keys in a non-root internal node?
AnswerMin children = ⌈5/2⌉=3. Min keys = min children - 1 = 2.For a leaf: min keys = ⌈(5−1)/2⌉=⌈4/2⌉=2.
Q2. Why do DBMSs prefer B+ trees over B-trees?
AnswerB+ trees store all data in leaves and link leaves together, making range queries fast (just follow leaf pointers). B-trees store data in all nodes — range queries require multiple traversals up and down the tree. The slightly higher height of B+ trees is negligible compared to the range query advantage.
Q3. A B+ tree of order 32 can hold how many keys at the root (min and max)?
Answer
- Min: 1 key (and 2 children)
- Max: 32 - 1 = 31 keys (and 32 children)
🔗 Cross-References
- Previous Topic: 23 - Indexing
- Next Topic: 25 - Hashing
- Related: BSCS2002 (PDSA) — Balanced BST, tree traversal
- Textbook: Chapter 11 (Indexing and Hashing) Join Discord Previous23 - IndexingNext25 - Hashing