CNG351 Lecture 12 B
CNG351 Lecture 12 B
• Insertion:
– An insertion into a node that is not full is quite efficient
– If a node is full the insertion causes a split into two
nodes
– Splitting may propagate to other tree levels
• Deletion:
– A deletion is quite efficient if a node does not become
less than half full
– If a deletion causes a node to become less than half
full, it must be merged with neighboring nodes
You overflow
13 17 24 30
2* 3* 5* 7* 8*
2* 3* 7* 8*
5 13 17 24 30 You overflow!
Insertion into B tree (cont.)
• Understand
difference between
copy-up and push- 5 13 17 24 30
up
• Observe how
minimum We split this node, redistribute entries evenly,
occupancy is and push up middle key.
guaranteed in both
leaf and index pg
splits.
Entry to be inserted in parent node.
17 (Note that 17 is pushed up and only
appears once in the index. Contrast
this with a leaf split.)
5 13 24 30
Example B Tree After Inserting 8*
Root
17
5 13 24 30
2* 3* 7* 8* 14* 15* 19* 20* 22* 27* 29* 33* 34* 38* 39*
5 13 24 30
2* 3* 7* 8* 14* 16* 19* 20* 22* 27* 29* 33* 34* 38* 39*
ow
nde r fl
u 22*
You
22* 24*
17
5 13 27 30
New root 5 13 17 30
25 50 75
5 10 15 20 25 30 50 55 60 65 75 80 85 90
B+ tree is a B tree that have its Leaf nodes form linked lists
22
Inserting a Data Entry into a B+ Tree: Summary
• Find correct leaf L.
• Put data entry onto L.
– If L has enough space, done!
– Else, must split L (into L and a new node L2)
• Redistribute entries evenly, put middle key in L2
• copy up middle key.
• Insert index entry pointing to L2 into parent of L.
• This can happen recursively
– To split index node, redistribute entries evenly, but push up
middle key. (Contrast with leaf splits.)
• Splits “grow” tree; root split increases height.
– Tree growth: gets wider or one level taller at top.
24
Insertion Example (cont.)
• New B+ Tree:
25
Rotation
• After
26
Deleting a Data Entry from a B+ Tree: Summary
• Start at root, find leaf L where entry belongs.
• Remove the entry.
– If L is at least half-full, done!
– If L has only d-1 entries,
• Try to re-distribute, borrowing from sibling (adjacent node with
same parent as L).
• If re-distribution fails, merge L and sibling.
• If merge occurred, must delete entry (pointing to L or sibling) from
parent of L.
• Merge could propagate to root, decreasing height.
28
Leaf Below Index Below Delete Actions
Fill Factor Fill Factor
NO NO Delete the record from the leaf. If the key
appears in the index, use the next key to
replace it.
29
Deletion Example
• Original B+ tree (After inserting Key 95):
30
Deletion Example (cont.)
• Delete Record with Key 70:
31
Deletion Example (cont.)
• Delete
Record
with
Key
25:
32
Deletion Example (cont.)
• Delete
Record
with Key
60:
• The leaf containing 60 (60 65) will be below the fill factor
after the deletion. Thus, we must combine leaf nodes.
• With recombined leaves, the index will be reduced by one key.
Hence, it will also fall below the fill factor. Thus, we must
combine index nodes.
• Sixty appears as the only key in the root index node.
Obviously, it will be removed with the deletion.
33
Summary
• Multilevel Indexes
• Dynamic Multilevel Indexes
– Using B-Trees, and
– Using B+-Trees
• Indexes on Multiple Keys