CLRS Exercise: 6.5-8
The operation HEAP-DELETE(A,i)
deletes the item in node i
from heap A
. Give an implementation of HEAP-DELETE
that runs in O(lg n)
time for an n-element max-heap.
I wonder if the algorithm is wrong for the input A[10]={84,22,19,21,3,10,6,5,20}
(Index starts with 1) and with A[6]=10
being deleted. Replacing the last node with A[6]
would result in violating heap property, overlooking the parent value.
I wrote an algorithm for this and wanted to know if it works right or where am I going wrong ?
HEAP-DELETE(A,i)
A[i]=A[A.heapsize]
A.heapsize-=1
while i>1 and A[parent(i)]<A[i]
swap A[i] with A[parent(i)]
i=parent(i);