In a max heap (assuming it’s represented by an array), the top of the heap (ie. the largest value in the heap) swaps with the last element in the array (ie. one of the smallest values in the heap), the last element is removed, and then the new top-of-the-heap element swaps with other values to settle back into its proper place.
Instead, why isn’t the top element just removed and then other elements can “fill in” for the the heap?
One of the key properties of a heap is that the underlying binary tree is a complete binary tree (i.e. every level except the last one has to be completely “filled”). This is so that the heap has
O(lg N)operations because we only have to modify one element at each of theO(lg N)levels. Let’s take a look at an exampleIf we follow your method and “fill in” the heap we get
The tree is no longer a complete binary tree as there is a “hole” at the
?. Since we don’t know that the tree is complete, we don’t know anything about the height of the tree and so we can’t guaranteeO(lg N)operations.This is why we take the last element in the heap, put it on top and then shuffle it down – to maintain the complete binary tree property.