Solution
Solution
Solution
Note: In this problem the 35 was inserted to the heap that resulted from extract-max. In your homework you are suppose to do the insertion on the original heap.
Solution
Solution
Delete(i){
temp = A[i] \\ holds value we are deleting
A[i] = A[n] \\ put A[n] in place of A[i]
n=n-1
if (A[i] > temp){ \\ if the value of A[i] is increased
value = A[i] \\ move A[i] up tree (as needed) like in insert
while ((i > 1) and A[parent(i)] < value){
A[i] = A[parent(i)]
i = parent(i)
}
A[i] = value
}
else \\ if we decreased the value of A[i]
heapify(A,i) \\ use heapify from text
}
If A[i] > temp then we have replaced A[i] with something larger.
In this case we use a procedure like that used for insertion, to
"insert" the new value in the right place in the chain of its
ancestors. This takes O(log n) time (where n is the number of
items in the heap) since the number of iterations is at most the
height of the heap.
Note that given when A[i] <= temp (i.e. we have replaced A[i] with
something smaller) then we have exactly the conditions required for
heapify to restore the heap. Thus the time complexity is
that of heapify which is O(log n).
Thus the overall time complexity is O(log n).