Why cant we just directly swap the given element with the last element and call down heapify?
Why cant we just directly swap the given element with the last element and call down heapify?
in case of min heap, the condition is parent must be smaller than all of its children, whereas left and right subtree of the node has no relation between them.
lets assume that the element to be deleted is in left subtree while the last element is in right subtree. so its possible that the last element might be smaller than the element to be deleted or it might be greater than the element to be deleted.
you can relate this to increasekey and decresekey operation of the heap.
so yes you can directly swap them. but in this case, based on the condition whether the value decreases or increases, you might need to upheapify or downheapify.
please rate and resolve if satisfied.
thanks