In general:
Removing the smallest element from MinHeap
Store the old root r
of the tree into a temporary variable, and replace the root node with the
last element in the heap (that is removed from the end of the heap and the size of the heap is decreased). After this,
revise the heap order property by 'sifting' the new root node p
to its correct position
by shifting it with its smaller child. This is repeated until the heap order property
is satisfied, i.e., there is no child that has a lower priority than p
.
Finally, return the preserved old root r
.
Removing the largest element from MaxHeap
In MaxHeap, the algorithm works just like above, but the new root going down is swapped with its largest child. We continue to study this algorithm in more details in the next Chapter, see MAX-HEAPIFY algorithm.
This document was last updated 30.5.2011. Please send your comments to Mikko Laakso, Ari Korhonen, and Ville Karavirta.