I've been trying to figure this out for a couple days now. I have a problem for school that says the following:
Let A be a min-heap. The operation HEAP-MODIFY(A, i, k) changes the key in the node i to a new value k, then rearranges the elements in a min-heap. Give an implementation of the HEAPMODIFY that runs in O(lgn) time for an n-element min-heap.
Since we have to design an algorithm that runs in O(lg(n)) time, I know that I can't just iterate through each element. I have the following solution but I am not sure if it is correct.
HEAP-MODIFY(A,i,k):
A[i] = K
if A[i] < A[i/2]
while A[i] < A[i/2] and i > 1
exchange A[i], A[i/2]
i=i/2
else if A[i] > A[2*i]
while A[i] > A[2*1] and i <k
exchange A[i], A[2*i]
i = 2*1
Any suggestions as to how this can be tackled?