Building a Heap from a List

Let A be an n element list. We will turn A into a heap (in place).

void Build_Heap( vector<int> & data, unsigned int i )
{
  if( i >= data.size() )
    return;

  Build_Heap( data, left_child( i ) );
  Build_Heap( data, right_child( i ) );
  Down_Heapify( data, i );
}

Recall that leftchild(i) is 2i and rightchild(i) is 2i+1.

Also, Down_Heapify(A, i) performs the down-heapify action starting at node i.

Correctness argument? What does down-heapify do?


next up previous
Next: Build-Heap Analysis Up: HEAPS Previous: Up-Heapify