b+-tree deletion

24
1 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

Upload: mac

Post on 05-Jan-2016

50 views

Category:

Documents


0 download

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: B+-Tree Deletion

1

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

Page 2: B+-Tree Deletion

2

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

Page 3: B+-Tree Deletion

3

B+ Tree Deletion AlgorithmTo 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.

Page 4: B+-Tree Deletion

4

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

Page 5: B+-Tree Deletion

5

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

Page 6: B+-Tree Deletion

6

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

Page 7: B+-Tree Deletion

7

Deletion Summary (cont’d)• Right leaf merging:

Page 8: B+-Tree Deletion

8

Deletion Summary (cont’d)• Left leaf merging:

Page 9: B+-Tree Deletion

9

Deletion in B+ Tree - Case1: No underflow

Example: Delete 20 from the following B+ tree of order M = 3 and L = 3

No leaf underflow

Delete 20

Page 10: B+-Tree Deletion

10

Deletion in B+ Tree- Case 2a: Leaf key borrowing from Right siblingExample: Delete 25 from the following B+ tree of order M = 3 and L = 3

Delete 25

Leaf underflow

Borrow min key 30 from right siblingReplace 30 in parent by a copy

of new minimum in right sibling

Page 11: B+-Tree Deletion

11

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

Borrow max key 13 from left siblingReplace 15 in parent by a copy

of new minimum in right sibling

Page 12: B+-Tree Deletion

12

Deletion in B+ Tree – Case 3a: Right Leaf MergingExample: 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

Page 13: B+-Tree Deletion

13

Deletion in B+ Tree – Case 3b: Left Leaf MergingExample: 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

Page 14: B+-Tree Deletion

14

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

Page 15: B+-Tree Deletion

15

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

Page 16: B+-Tree Deletion

16

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 underflowCannot borrow. Merge overflow node with adjacent right sibling

Internal node underflow

Borrow min key 45 from adjacent right sibling

Page 17: B+-Tree Deletion

17

Deletion in B+ Tree – Example of Borrowing internal key from right sibling (cont’d)

Page 18: B+-Tree Deletion

18

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

Internal node underflow

Borrow max key 17 from adjacent left sibling

Page 19: B+-Tree Deletion

19

Deletion in B+ Tree – Example of Borrowing internal key from left sibling (cont’d)

Page 20: B+-Tree Deletion

20

Deletion in B+ Tree – Case 5: Merging internal nodesAn 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.

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.

merge node, adjacent right sibling and the separating key x

Page 21: B+-Tree Deletion

21

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

Page 22: B+-Tree Deletion

22

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

Internal node underflow

Cannot borrow. Merge node with adjacent right sibling

Page 23: B+-Tree Deletion

23

Deletion in B+ Tree – Case5: Merging internal nodes (cont’d)

Page 24: B+-Tree Deletion

24

Deletion in B+ Tree – Case 5: Merging the rootExample: 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