Explain why we want the loop index i in line 2 of buildMaxHe

Explain why we want the loop index i in line 2 of build_MaxHeap to decrease from floor(length(a)) to 1 rather than increase from 1 to floor(length(a))? function a = build MaxHeap(a) for i= length(a):-l: l %i=n downto l a = max_heapify(a, i); end

Solution

This is because we need to make sure that before calling Max-Heapify the   necessary condition of Max-Heapify should be meet .

As,If we start from Starting (or 1),as its subtree is not a maximum heap, we can\'t follow this order.

The statement , provides sureity that for each i iteration,both left and right subtrees of node i are Max-heaps already.

 Explain why we want the loop index i in line 2 of build_MaxHeap to decrease from floor(length(a)) to 1 rather than increase from 1 to floor(length(a))? functio

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site