We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
When you delete the root, you replace it with the last element, then perform heapifyDown, if the new Root is greater than either children.
But when you have to delete any arbitrary element, after replacing it with the last element, dont you need to take care of both the conditions below?
1) Heapify up - If the element is less than the parent
2) Heapify Down - If the element is greater than either children.
But the editorial only does a Heapify Down operation during deletion?
For instance, consider the tree :- 10, 20, 16, 40, 50, 18, 17, 80, 85, 55, 56, 19. If you want to delete node 40, you replace it with the last element i.e. 19. Now a heapify down operation wont
restore the heap property. You will need to do a heapify up operation and this is not covered in the Editorial code. Wanted to check if my understanding is correct.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
QHEAP1
You are viewing a single comment's thread. Return to all comments →
When you delete the root, you replace it with the last element, then perform heapifyDown, if the new Root is greater than either children.
But when you have to delete any arbitrary element, after replacing it with the last element, dont you need to take care of both the conditions below? 1) Heapify up - If the element is less than the parent 2) Heapify Down - If the element is greater than either children.
But the editorial only does a Heapify Down operation during deletion?
For instance, consider the tree :- 10, 20, 16, 40, 50, 18, 17, 80, 85, 55, 56, 19. If you want to delete node 40, you replace it with the last element i.e. 19. Now a heapify down operation wont restore the heap property. You will need to do a heapify up operation and this is not covered in the Editorial code. Wanted to check if my understanding is correct.