1 / 17

removeMin

removeMin. We must take the value from the root node and return it to the user. Then we must remove the node. Easy array implementation: Take the last element in the heap and put it in root Then call heapify!. heapify. i. i. 42. 27. 2i+1. 2i. 2i+1. 2i. 27. 36. 42. 36.

margo
Télécharger la présentation

removeMin

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. removeMin • We must take the value from the root node and return it to the user. • Then we must remove the node. • Easy array implementation: • Take the last element in the heap and put it in root • Then call heapify!

  2. heapify i i 42 27 2i+1 2i 2i+1 2i 27 36 42 36 • Heapify is called on a node. It checks that the heap property is being maintained. If not, it fixes it. • If a fix was made heapify calls itself recursively on the swapped child i 27 Details such as checking to make sure elements 2i and 2i+1 are in the heap are omitted 2i+1 2i 42 36

  3. removeMin size = 27 2 25 10 18 21 26 27 57 20 47 37 69 29 29 58 69 86 90 31 94 55 98 71 95 85 79 91

  4. removeMin ReturnValue: 2 size = 27 2 25 10 18 21 26 27 57 20 47 37 69 29 29 58 69 86 90 31 94 55 98 71 95 85 79 91

  5. Consider ….. 20 30 50 60 10 20 30 Remove 10…. Which element becomes a root now? Try 20 to preserve the order property (r<=c)…. 50 60 Is this acceptable? Not complete tree. Therefore …… Let’s try to preserve the structure property….

  6. removeMin ReturnValue: 2 size = 26 69 Decrement the size 25 10 18 21 26 27 57 20 47 37 69 29 29 58 69 86 90 31 94 55 98 71 95 85 79 91

  7. removeMin ReturnValue: 2 size = 26 69 25 10 18 21 26 27 57 20 47 37 69 29 29 58 86 90 31 94 55 98 71 95 85 79 91

  8. removeMin ReturnValue: 2 size = 26 69 25 10 18 21 26 27 57 20 47 37 69 29 29 58 86 90 31 94 55 98 71 95 85 79 91 Heapify

  9. removeMin ReturnValue: 2 size = 26 10 25 69 18 21 26 27 57 20 47 37 69 29 29 58 86 90 31 94 55 98 71 95 85 79 91 Heapify

  10. removeMin ReturnValue: 2 size = 26 10 25 69 18 21 26 27 57 20 47 37 69 29 29 58 86 90 31 94 55 98 71 95 85 79 91 Heapify

  11. removeMin ReturnValue: 2 size = 26 10 25 18 69 21 26 27 57 20 47 37 69 29 29 58 86 90 31 94 55 98 71 95 85 79 91 Heapify

  12. removeMin ReturnValue: 2 size = 26 10 25 18 69 21 26 27 57 20 47 37 69 29 29 58 86 90 31 94 55 98 71 95 85 79 91 Heapify

  13. removeMin ReturnValue: 2 size = 26 10 25 18 20 21 26 27 57 69 47 37 69 29 29 58 86 90 31 94 55 98 71 95 85 79 91 Heapify

  14. removeMin ReturnValue: 2 size = 26 10 25 18 20 21 26 27 57 69 47 37 69 29 29 58 86 90 31 94 55 98 71 95 85 79 91 Heapify

  15. removeMin ReturnValue: 2 size = 26 10 25 18 20 21 26 27 57 31 47 37 69 29 29 58 86 90 69 94 55 98 71 95 85 79 91 Heapify

  16. removeMin ReturnValue: 2 size = 26 10 25 18 20 21 26 27 57 31 47 37 69 29 29 58 86 90 69 94 55 98 71 95 85 79 91

  17. removeMin size = 26 10 25 18 20 21 26 27 57 31 47 37 69 29 29 58 86 90 69 94 55 98 71 95 85 79 91 Questions?

More Related