I have read the algorithm to delete the root element of heap.
1. Swap the root element with last element of the heap.
2. Then heapify (shift down) from the root element downwards.
At few other places, I find that they heapify upwards from the last element’s parent towards root.(i.e., check deleteTop() function here http://www.geeksforgeeks.org/archives/14873) Hence confused with the right approach 🙁 Does this vary based on the situation or the article itself is wrong?
The code of
deleteTop()is wrong.When given this max-heap and running
deleteTop():The resulting heap is wrong since 2<(3 and 10)