1 / 19

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

Solution for Section Worksheet 4, #7b & #7c Section Worksheet 4 #3a & #3b (same problem). M=3, L=3. Insert 12. 12. M=3, L=3. Insert 24. 12. 24. M=3, L=3. Insert 36. 12. 24. 36. M=3, L=3. Insert 17. 24. 12. 12. 24. 12. 24. Create root. split. 17. 17. 36. 17. 36. 24.

peyton
Download Presentation

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

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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. M=3, L=3 Insert 17 24 12 12 24 12 24 Create root split 17 17 36 17 36 24 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 17 24 24 5 17 24 5 24 split 12 18 36 12 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. M=3, L=3 Insert 20 (step 1) 17 20 17 24 Overflow! Split internal node 20 24 5 17 5 17 24 22 36 12 18 12 18 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. M=3, L=3 Insert 20 (step 2) 20 17 24 17 24 Create root 20 24 5 17 20 24 5 17 22 36 12 18 22 36 12 18 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. M=3, L=3 Delete 17 (step 1) 20 20 17 24 17 24 Delete 17 20 24 5 17 20 24 5 17 22 36 12 22 36 12 18 Underflow, because leaf now has ceiling(L/2) - 1 items in leaf node. Original Tree

  12. M=3, L=3 Delete 17 (step 2) 20 20 17 24 24 merge 20 20 24 24 5 17 5 22 22 36 36 12 12 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. M=3, L=3 Delete 17 (step 3) 20 24 20 24 merge 20 24 5 5 20 24 22 36 12 12 22 36 17 17 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 24 20 24 merge 5 24 5 20 24 17 36 17 36 20 After merging, fix the keys in the internal node. 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.

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

  18. M=3, L=3 Delete 36 (step 1) 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.

More Related