Download presentation
Presentation is loading. Please wait.
Published byArline Malone Modified over 9 years ago
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 Heapify is called on a node. It checks that the heap property is being maintained. If not, it fixes it. 42 2736 i 2i 2i+1 27 4236 i 2i 2i+1 If a fix was made heapify calls itself recursively on the swapped child 27 4236 i 2i 2i+1 Details such as checking to make sure elements 2i and 2i+1 are in the heap are omitted Details such as checking to make sure elements 2i and 2i+1 are in the heap are omitted
3
removeMin 2 10 25 18212627 57 2047376929 58 9585 903194559871 7991 86 size = 27 69
4
removeMin 2 10 25 18212627 57 2047376929 58 9585 903194559871 7991 86 size = 27 69 ReturnValue: 2
5
Consider ….. 10 30 20 5060 Remove 10…. Which element becomes a root now? Try 20 to preserve the order property (r<=c)…. 20 30 5060 Is this acceptable? Not complete tree. Therefore …… Let’s try to preserve the structure property….
6
removeMin 69 10 25 18212627 57 2047376929 58 9585 903194559871 7991 86 size = 26 69 ReturnValue: 2 Decrement the size
7
removeMin 69 10 25 18212627 57 2047376929 58 9585 903194559871 7991 86 size = 26 ReturnValue: 2
8
removeMin 69 10 25 18212627 57 2047376929 58 9585 903194559871 7991 86 size = 26 ReturnValue: 2 Heapify
9
removeMin 10 69 25 18212627 57 2047376929 58 9585 903194559871 7991 86 size = 26 ReturnValue: 2 Heapify
10
removeMin 10 69 25 18212627 57 2047376929 58 9585 903194559871 7991 86 size = 26 ReturnValue: 2 Heapify
11
removeMin 10 18 25 69212627 57 2047376929 58 9585 903194559871 7991 86 size = 26 ReturnValue: 2 Heapify
12
removeMin 10 18 25 69212627 57 2047376929 58 9585 903194559871 7991 86 size = 26 ReturnValue: 2 Heapify
13
removeMin 10 18 25 20212627 57 6947376929 58 9585 903194559871 7991 86 size = 26 ReturnValue: 2 Heapify
14
removeMin 10 18 25 20212627 57 6947376929 58 9585 903194559871 7991 86 size = 26 ReturnValue: 2 Heapify
15
removeMin 10 18 25 20212627 57 3147376929 58 9585 906994559871 7991 86 size = 26 ReturnValue: 2 Heapify
16
removeMin 10 18 25 20212627 57 3147376929 58 9585 906994559871 7991 86 size = 26 ReturnValue: 2
17
removeMin 10 18 25 20212627 57 3147376929 58 9585 906994559871 7991 86 size = 26 Questions?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.