170 likes | 299 Views
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.
E N D
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 • 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
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
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
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….
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
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
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
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
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
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
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
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
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
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
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
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?