Exercise 6.2-2

Starting with the procedure MAX-HEAPIFY, write pseudocode for the procedure MIN-HEAPIFY(\(A, i\)), which performs the corresponding manipulation on a min-heap. How does the running time of MIN-HEAPIFY compare to that of MAX-HEAPIFY?

MIN-HEAPIFY(A, i)

1  L = LEFT(i)

2  R = RIGHT(i)

3  if L ≤ A.heap-size and A[L] < A[i]:

4       smallest = L

5  else smallest = 1

6  if R ≤ A.heap-size and A[R] < A[smallest]:

7       smallest = R

8  if smallest ≠ i:

9       exchange A[i] with A[smallest]

10      MIN-HEAPIFY(A, smallest)

There is no difference in running time between MAX-HEAPIFY and MIN-HEAPIFY.