C++ Programming
07-06-2009, 03:50 PM
Hi Frnds,
I am trying to do the heap implementation.
I was following the algorithm explained in cormen....
#include "MyHeap.h"
void MyHeap::Build_Max_Heap(int * array, int length)
{
for (int i= (length/2); i>=0 ; i--)
{
Max_heapify(array, i, length);
}
}
void MyHeap::Max_heapify(int * array, int index, int length)
{
int left = ((2 * index) + 1);
int right = ((2 * index) + 2);
int largest;
if ((left array[index]))
{
largest = left;
}
else
{
largest = index;
}
if ((right array[largest]))
{
largest = right;
}
if (largest != index)
{
swap(array[largest], array[index]);
Max_heapify(array, largest, length);
}
}
void MyHeap::swap(int & a, int & b)
{
int c = a;
a = b;
b = c;
}
void MyHeap::print(int * array, int length)
{
for (int i = 0; i< length; i++)
{
cout
I am trying to do the heap implementation.
I was following the algorithm explained in cormen....
#include "MyHeap.h"
void MyHeap::Build_Max_Heap(int * array, int length)
{
for (int i= (length/2); i>=0 ; i--)
{
Max_heapify(array, i, length);
}
}
void MyHeap::Max_heapify(int * array, int index, int length)
{
int left = ((2 * index) + 1);
int right = ((2 * index) + 2);
int largest;
if ((left array[index]))
{
largest = left;
}
else
{
largest = index;
}
if ((right array[largest]))
{
largest = right;
}
if (largest != index)
{
swap(array[largest], array[index]);
Max_heapify(array, largest, length);
}
}
void MyHeap::swap(int & a, int & b)
{
int c = a;
a = b;
b = c;
}
void MyHeap::print(int * array, int length)
{
for (int i = 0; i< length; i++)
{
cout