0
0
Data Structures Theoryknowledge~10 mins

B+ trees in databases in Data Structures Theory - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - B+ trees in databases
Start at Root Node
Search Key in Node
Is Node Leaf?
NoFollow Child Pointer to Next Node
Repeat Search in Child Node
Find Key or Position
If Insert: Add Key
Is Node Full?
NoDone
Yes
Split Node
Push Up Middle Key to Parent
If Parent Full, Repeat Split
Done
This flow shows how a B+ tree searches for a key starting at the root, moves down internal nodes, and inserts keys by splitting nodes when full, pushing keys up to parents.
Execution Sample
Data Structures Theory
Insert keys: 10, 20, 5, 6, 12 into B+ tree of order 3

Steps:
1. Insert 10
2. Insert 20
3. Insert 5
4. Insert 6 (causes split)
5. Insert 12
This example shows inserting keys into a B+ tree with max 3 keys per node, demonstrating node splits and key promotion.
Analysis Table
StepActionNode State BeforeOperationNode State AfterNotes
1Insert 10Empty rootAdd 10 to rootRoot keys: [10]Root is leaf, no split
2Insert 20Root keys: [10]Add 20 to rootRoot keys: [10, 20]Still space, no split
3Insert 5Root keys: [10, 20]Add 5 and sortRoot keys: [5, 10, 20]Node full (3 keys)
4Insert 6Root keys: [5, 10, 20]Add 6 and sort: [5, 6, 10, 20]Split node into two: Left: [5, 6] Right: [10, 20]Push up 10 to new root; height increases to 2
5Insert 12Root keys: [10] Right child: [10, 20]Add 12 to right child and sortRight child keys: [10, 12, 20]Right child full
6Split right childRight child keys: [10, 12, 20]Split into [10] and [12, 20]Root keys: [10, 12] Children: Left [5, 6], Middle [10], Right [12, 20]Push up 12 to root; root has space, no further split
7DoneFinal treeNo operationRoot keys: [10, 12] Leaves: [5, 6], [10], [12, 20]Insertion complete
💡 All keys inserted; tree balanced with splits and key promotions.
State Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6Final
Root keys[][10][10, 20][5, 10, 20][10][10][10, 12][10, 12]
Left child keysN/AN/AN/AN/A[5, 6][5, 6][5, 6][5, 6]
Right child keysN/AN/AN/AN/A[10, 20][10, 12, 20][12, 20][12, 20]
Tree height11112222
Key Insights - 3 Insights
Why do we split the node when it becomes full?
When a node has more keys than allowed (step 4 in execution_table), it splits to keep the tree balanced and maintain search efficiency.
What happens to the middle key during a split?
The middle key is pushed up to the parent node (step 4), acting as a separator between the two new child nodes.
Why does the tree height increase after some splits?
When the root node is full and splits (step 4), a new root is created, increasing the tree height by one.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4. What keys are in the left child after the split?
A[5, 10]
B[5, 6]
C[10, 20]
D[6, 10]
💡 Hint
Check the 'Node State After' column in step 4 for the split details.
At which step does the tree height increase?
AStep 6
BStep 3
CStep 4
DStep 2
💡 Hint
Look at the 'Tree height' variable in variable_tracker after each step.
If we insert a key smaller than all existing keys at step 5, how would the execution_table change?
AThe left child would receive the new key instead
BThe right child would split earlier
CThe root would split immediately
DNo splits would occur
💡 Hint
Consider where keys smaller than existing ones go in a B+ tree (see step 5 node states).
Concept Snapshot
B+ trees store sorted keys in nodes with pointers to children.
Leaf nodes hold actual data keys.
When a node is full, it splits and pushes the middle key up.
This keeps the tree balanced and search fast.
Tree height grows only when root splits.
Used in databases for efficient range queries.
Full Transcript
This visual execution trace shows how B+ trees in databases manage keys and maintain balance. Starting from an empty root, keys are inserted one by one. When a node reaches its maximum capacity, it splits into two nodes, and the middle key is pushed up to the parent node. If the parent is also full, it splits too, possibly increasing the tree height. Leaf nodes contain the actual data keys, and internal nodes guide the search. This structure ensures fast search, insert, and range queries by keeping the tree balanced. The example inserts keys 10, 20, 5, 6, and 12 into a B+ tree of order 3, showing node splits and key promotions step-by-step.