Quiz 2

24 - B+ Tree Index

667 words
3 min read
Python Week 1: the first filter for runtime behavior
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 nn:
Node TypeMin KeysMax KeysMin ChildrenMax Children
Root1nn2nn
Internaln/2\lceil n/2 \rceilnnn/2\lceil n/2 \rceilnn
Leaf(n1)/2\lceil (n-1)/2 \rceiln1n-1
  1. Start at root
  2. At each internal node, find the smallest key > search key; go to that child
  3. 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

  1. Find the leaf where the key should go
  2. If leaf has room, insert
  3. If leaf is full, split it:
    • First (n1)/2\lceil (n-1)/2 \rceil keys stay
    • Remaining keys go to new leaf
    • Copy the middle key up to parent
  4. If parent is full, split parent (internal node split)
  5. If root splits, create new root (height increases by 1)

24.5 Deletion

  1. Find and remove the key from the leaf
  2. If leaf has enough keys (≥ min), done
  3. If leaf is underfull, try to borrow from sibling
  4. If no sibling can spare, merge with sibling
  5. May need to delete from parent (cascading)

24.6 B+ Tree vs. B-Tree

FeatureB-TreeB+ Tree
Data pointersIn all nodesOnly in leaves
Leaf linkageNoneLinked list
HeightTypically lowerSlightly higher
Search (point query)May find key before leafAlways goes to leaf
Range querySlow (must traverse up/down)Fast (follow leaf chain)
Insert/DeleteMore complexSimpler
Used in DBMSRarelyAlmost always

24.7 Worked Example

Given: B+ tree with order n=4n=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?

Answer
Min children = 5/2=3\lceil 5/2 \rceil = 3. Min keys = min children - 1 = 2.
For a leaf: min keys = (51)/2=4/2=2\lceil (5-1)/2 \rceil = \lceil 4/2 \rceil = 2.

Q2. Why do DBMSs prefer B+ trees over B-trees?

Answer
B+ 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

Document outline

Keep your place and jump directly to a heading.

Table of Contents
System Normal // Awaiting Context

Intelligence Hub

Navigate the knowledge graph to generate context. The Hub adapts dynamically to surface backlinks, related notes, and metadata insights.