Presentation is loading. Please wait.

Presentation is loading. Please wait.

Solution for Section Worksheet 4, #7b & #7c

Similar presentations


Presentation on theme: "Solution for Section Worksheet 4, #7b & #7c"— Presentation transcript:

1 Solution for Section Worksheet 4, #7b & #7c Section Worksheet 4 #3a & #3b (same problem)

2 M=3, L=3 Insert 12 12

3 M=3, L=3 Insert 24 12 24

4 M=3, L=3 Insert 36 12 24 36

5 Insert 17 M=3, L=3 24 12 12 24 12 24 Create root split 17 17 36 17 36
Old node needs to have ceiling( (L+1)/2 ) = 2 items, new node needs to have floor( (L+1)/2 ) = 2 items. 36 First key in new root is the smallest item of right child 36 overflows!

6 M=3, L=3 Insert 18 24 12 24 17 36 18

7 M=3, L=3 Insert 5 24 17 24 5 24 5 17 24 split 12 36 12 18 36 17 18 Old node needs to have ceiling( (L+1)/2 ) = 2 items, new node needs to have floor( (L+1)/2 ) = 2 items. Then fix pointer and internal node keys. 18 overflows!

8 M=3, L=3 Insert 22 17 24 5 17 24 12 18 36 22

9 Insert 20 (step 1) M=3, L=3 17 24 17 20 Overflow! Split internal node
5 17 24 5 17 20 24 12 18 36 12 18 22 36 20 22 After splitting (17,18,20,22) into (17,18) & (20,22) we have overflow in the internal (parent) node 22 overflows!

10 Insert 20 (step 2) M=3, L=3 20 17 24 17 24 Create root 5 17 20 24 12
18 22 36 5 17 20 24 12 18 22 36 Old internal node needs to have ceiling( (M+1)/2 ) = 2 pointers, new node needs to have floor( (M+1)/2 ) = 2 pointers. This means they each have 1 key, since each key has a left and a right pointer. Root’s 1st key should be the smallest item in its right child, so smallest item of (20,22,24,36) is 20.

11 Delete 17 (step 1) M=3, L=3 20 20 17 24 17 24 Delete 17 20 24 5 17 20
22 36 12 18 22 36 12 Underflow, because leaf now has ceiling(L/2) - 1 items in leaf node. Original Tree

12 Delete 17 (step 2) M=3, L=3 20 20 17 24 24 merge 5 17 20 24 5 20 24 12
22 36 12 22 36 17 If (17)’s neighbor, (5,12), has enough items such that it won’t underflow, we could adopt 12, but that would cause underflow, so need to merge Merging leaf nodes caused underflow in parent internal node, which now only ceiling(M/2)-1 pointers (i.e. only 1 pointer). So need to fix!

13 Delete 17 (step 3) M=3, L=3 20 24 20 24 merge 5 20 24 5 20 24 12 22 36
Internal node’s neighbor, (24), does not have enough pointers such that it will not underflow if we adopt one, so we must merge these two internal nodes. But now, the root is underflowed (only 1 pointer), so…

14 M=3, L=3 Delete 17 (step 4) 20 24 5 20 24 12 22 36 17 ….we can just delete the root.

15 M=3, L=3 Delete 12 20 24 5 20 24 17 22 36 No underflow, so we’re done.

16 M=3, L=3 Delete 22 20 24 24 merge 5 20 24 5 24 17 36 17 36 20 Deleting 22 causes underflow in middle child. We could adopt one of neighbor’s children to fix underflow, except then, neighbor would underflow, so we need to merge. After merging, fix the keys in the internal node.

17 M=3, L=3 Delete 5 24 17 24 20 36 Deleting 5 doesn’t cause underflow, so we’re fine.

18 Delete 36 (step 1) M=3, L=3 24 17 24 17 merge 20 20 24
Deleting 36 causes an underflow, i.e. leaf now has ceiling(L/2)-1 items. Adopting from neighbor (17,20) would cause underflow in the neighbor, so need to merge. Now we have an underflow of only 1 pointer in the parent node, so we need to fix it…

19 M=3, L=3 Delete 36 (step 2) 17 20 24 …so we delete it, and we’re done.


Download ppt "Solution for Section Worksheet 4, #7b & #7c"

Similar presentations


Ads by Google