17 B+ trees

B+-Tree Deletion
•
Underflow conditions
•
B+ tree Deletion Algorithm
•
Leaf key rotations
•
Deletion Summary
•
Deletion Case 1: No underflow
•
Deletion Case 2: Key borrowing (key rotation) from adjacent leaf sibling
•
Deletion Case 3: Leaf merging
•
Deletion Case 4: Internal key borrowing (internal key rotation)
•
Case 5: Merging internal nodes
1
Deletion in B+ Tree: Underflow conditions
•
Like insertion, deletion must be on a leaf node.
•
A B+ tree has two UNDEFLOW conditions:
– Leaf underflow Condition:
• A leaf underflows if after deleting a key from it, it contains
L/2 - 1 keys
– Internal node underflow Condition:
• An internal node (excluding the root node) underflows if in the
key deletion process it contains M/2 - 2 keys
2
B+ Tree Deletion Algorithm
To delete a key targetKey (and its associated record), we search for it. If it is not
found in a leaf we report an error. If it is found at a leaf, say x, we remove it and
its data reference. There are two issues to handle:
First issue:
• targetKey appears as a separating key in some internal node
– targetKey can appear in at most one ancestor y of x as a separating key.
Moreover, we must have visited y and seen targetKey in it when we
searched down the tree. So after deleting targetKey from x, we access y
and replace targetKey by a copy of the new smallest key in node x.
After handling the first issue, we handle the second issue:
Second issue:
• If after deleting targetKey, there is no leaf underflow, the deletion is complete;
otherwise, if there is a leaf underflow (After deleting targetKey, node x contains
L/2 - 1 keys) then:
If there is an adjacent leaf sibling with at least  L/2  + 1 keys we borrow from the
sibling the minimum key (if right sibling) or the maximum key (if left sibling). If no
adjacent sibling leaf with at least  L/2  + 1 keys exists, then we have to merge two
leaves.
3
Deletion in B+ Tree: Leaf key rotation
•
•
Let u be the node with leaf underflow.
Leaf left key rotation (borrowing from adjacent right sibling v):
– Move the minimum key of v to u
– Replace the separating key between u and v with a copy of the new minimum in v
4
Deletion in B+ Tree: Leaf key rotation (cont’d)
•
•
Let u be the node with leaf underflow.
Leaf right key rotation (borrowing from adjacent left sibling v)
– Move the maximum key of v to u
– Replace the separating key between u and v with a copy of the new minimum in u
5
Deletion Summary
•
•
We first fix the separator issue.
After that, we check if the leaf underflows. If it is the case, we always try to borrow from an
adjacent sibling (first looking at adjacent right sibling, then, if not possible, adjacent left
sibling).
•
If an adjacent sibling x has some extra keys, we ‘borrow’ an extra key from the sibling (min
if x is a right sibling and max if it is a left one), COPY the new minimum key of the right
sibling to the separating key in the parent, and fix the references DONE
•
Else, if ALL the siblings have the minimum number of keys L/2, we need to merge the
underflow leaf with an adjacent sibling (right sibling, then, if not possible, left sibling).
–
Let u be the leaf node with underflow. Let v be the adjacent sibling:
•
•
•
–
•
Move the keys in u to v.
Remove the reference to u at parent [i.e., delete u]
Delete the separating key between u and v from the parent.
The merge process deletes one key from the parent, so the parent may
underflow  need to continue the process  in the worst case, we may go up to
the root:
If an internal node underflows use the borrowing or merge algorithms given in
slide 15, 16 and 19
6
Deletion Summary (cont’d)
•
Right leaf merging:
7
Deletion Summary (cont’d)
•
Left leaf merging:
8
Deletion in B+ Tree - Case1: No underflow
Example: Delete 20 from the following B+ tree of order M = 3 and L = 3
Delete 20
No leaf underflow
9
Deletion in B+ Tree- Case 2a: Leaf key borrowing from Right sibling
Example: Delete 25 from the following B+ tree of order M = 3 and L = 3
Delete 25
Leaf underflow
Replace 30 in parent by a copy
of new minimum in right sibling
Borrow min key 30
from right sibling
10
Deletion in B+ Tree – Case 2b: Leaf key borrowing from left sibling
Example: Delete 17 from the following B+ tree of order M = 3 and L = 3
Delete 17
Leaf underflow
Replace 15 in parent by a copy
of new minimum in right sibling
Borrow max key
13 from left sibling
11
Deletion in B+ Tree – Case 3a: Right Leaf Merging
Example: Delete 15 from the following B+ tree of order M = 4 and L = 3
Delete 15
Leaf underflow
Cannot borrow.
Merge overflow
node with
adjacent right
sibling
12
Deletion in B+ Tree – Case 3b: Left Leaf Merging
Example: Delete 19 from the following B+ tree of order M = 4 and L = 3
Delete 19
Leaf underflow
Cannot borrow. Merge
overflow node with
adjacent left sibling
13
Deletion in B+ Tree – Case 4: Internal Key Borrowing
An internal node u underflows:
Case 4a [Internal Left key rotation] : the adjacent right sibling v of u has at least  M/2 keys.
• Move the separating key between u and v in the parent of u and v down to u.
• Make the leftmost child of v the rightmost child of u.
• MOVE the leftmost key in v to become the separating key between u and v in the parent
of u and v.
Note: Contrast
with Leaf key
borrowing
where the
leftmost key in
the right
sibling is
COPIED up to
the parent to
replace the
separating key
14
Deletion in B+ Tree: Internal Key Borrowing (cont’d)
An internal node u underflows:
Case 4b [Internal Right key rotation] : the adjacent left sibling v of u has at least  M/2 keys.
• Move the separating key between u and v in the parent of u and v down to u.
• Make the rightmost child of v the leftmost child of u.
• MOVE the leftmost key in u to become the separating key between u and v in the
parent of u and v.
Note: Contrast
with Leaf key
borrowing
where the
leftmost key in
the right
sibling is
COPIED up to
the parent to
replace the
separating key
15
Deletion in B+ Tree - Example of Borrowing internal key from right sibling
An internal node u underflows:
Case 4a : the adjacent right sibling of v of u has at least  M/2 keys.
Example: Delete 26 from the following B+ tree of order M = 5 and L = 4
Delete 26
Leaf underflow
Cannot borrow. Merge overflow
node with adjacent right sibling
Borrow min key 45 from adjacent
right sibling
Internal node underflow
16
Deletion in B+ Tree – Example of Borrowing internal key from right sibling (cont’d)
17
Deletion in B+ Tree – Example of Borrowing internal key from left sibling
An internal node u underflows:
Case 4b : the left sibling of v of u has at least  M/2 keys
Example: Delete 20 from the following B+ tree of order M = 5 and L = 4
Delete 20
Leaf underflow
Cannot borrow. Merge overflow
node with adjacent left sibling
Borrow max key 17 from adjacent
left sibling
Internal node underflow
18
Deletion in B+ Tree – Example of Borrowing internal key from left sibling (cont’d)
19
Deletion in B+ Tree – Case 5: Merging internal nodes
An internal node w underflows:
Case 5 [Merging] : each of the adjacent right and left sibling of w has  M/2 - 1 keys.
Let v be one of the siblings
• Merge w and v.
– Move the separating key between w and v in the parent of w and v down to
w. Note that this corresponds to deleting separating key from the parent of
w and v.
– Move the keys and child references in w to v.
– Remove the reference to w in the parent.
merge node, adjacent right sibling and the
separating key x
If the parent of the merged node underflows, the merging process propagates upward. In the
limit, a root with one key is deleted and the height decreases by one.
20
Deletion in B+ Tree – Case 5: Merging internal nodes (cont’d)
Note: The merging could also be done by using the adjacent left sibling instead
of the adjacent right sibling.
merge node, adjacent left sibling and the
separating key v
21
Deletion in B+ Tree – Case5: Merging internal nodes (cont’d)
An internal node u underflows:
Case 5 : each of the right and left sibling of u has  M/2 - 1 keys.
Example: Delete 34 from the following B+ tree of order M = 5 and L = 4
Delete 34
Leaf underflow
Cannot borrow. Merge overflow
node with adjacent left sibling
Cannot borrow. Merge node with
adjacent right sibling
Internal node underflow
22
Deletion in B+ Tree – Case5: Merging internal nodes (cont’d)
23
Deletion in B+ Tree – Case 5: Merging the root
Example: Delete 20 from the following B+ tree of order M = 3 and L = 3
Delete 20
underflow
Cannot borrow. Merge
overflow node with
adjacent left sibling
24