Need Help with MinHeap Implementation

I am trying to implement a MinHeap data structure in C++. In my code I have tried to implement the following functions:

1.initialize(int v[],int n) – Initialize the heap to store vertices in array v with all their keys set to ∞.
2.Heapltem removeMin() − This operation will return the heap node that has the minimum key value. Must restore heap property by calling ℎeapify after removal
3.ℎeapify(int i) − This operation is required to re-store heap property for implementing certain operations, such as removeMin operation, and decreaseKey operation
4.decreaseKey(int v, float k) − This operation decrease the key value of the vertex v to k. After decreasing the key value, it may happen that the new key violates min-heap property (parent’s key value must be less than or equal to childs’ key values). So, after decrease, a node may be required to bubble-up (swapping of nodes) the tree (done by buHeapify operation).
5.buHeapify(int i) − This operation is used as part of the decreaseKey operation. When the key value of a vertex at node index i is decreased, the node may be required to move up the tree so that heap property (parent’s key less than or equal to childs’ key) remains correct. This may be implemented as a recursive function or in iterative manner that starts swapping of nodes at node number i, then may call recursively at parent of i. In the worse case, the function will end at the root of the tree (all nodes up-to the root will be swapped).
6.getKey(int v) − This operation returns the key value of a vertex v in the heap.
7.emtpy() − Returns true if the heap is empty, otherwise returns false.
I have made the following code to implement the MInHeap. Though it's not complete, I need your help to complete the MinHeap Class. Here is the code I have made so far

#define MAX_HEAP_SIZE 100000

#define MAXREAL 999999.0



class HeapItem
{
public:

int vertex;
float key;
};


class MinHeap
{
public:
HeapItem * A; //stores heap items, e.g., nodes
int heapLength;
int * map;

MinHeap()
{
A = new HeapItem[MAX_HEAP_SIZE];
map = new int[MAX_HEAP_SIZE];
heapLength=0;
}

~MinHeap()
{
if(map) delete [] map;
if(A)
delete [] A;
}

void initialize(int v[], int n)
{
heapLength = n;
for(int i=0;i<n;i++) //nodes are stored from index 1
{ //in the heap
A[i+1].vertex = v[i];
A[i+1].key = MAXREAL;
map[v[i]] = i+1;
}
}


void decreaseKey(int v, float key) //I have to implement this
{

}

HeapItem removeMin() //I have to implement this
{

}

float getKey(int v)
{
int i = map[v]; //get the index of vertex


return A[i].key;
}

void heapify(int i)
{
int l,r,smallest;
while(1)

{

l=2*i; //left child index
r=2*i+1; //right child index
smallest=i;


if(l>heapLength && r>heapLength)
break; //nothing to do, we are at bottom
else if(r>heapLength)
smallest = l;
else if(l>heapLength)
smallest = r;
else if( A[l].key < A[r].key )
smallest = l;
else
smallest = r;


if(A[i].key <= A[smallest].key)
break; //we are done heapifying

else
{

//swap nodes with smallest child
HeapItem t;
t=A[i];
A[i]=A[smallest];
map[A[i].vertex]=i;
A[smallest]=t;
map[A[smallest].vertex]=smallest;
i=smallest;


}




} //End while
}
// End Heapify


void buHeapify(int i) //I have to implement this to support decrease key operation
{

}

bool Empty()
{
if(heapLength==0)
return true;
else
return false;
}
};


I am confused to write codes in the following 3 functions

void decreaseKey(int v, float key),

HeapItem removeMin(),

void buHeapify(int i).

what will be the codes in these function to fulfill the MinHeap data structure? Please help
Topic archived. No new replies allowed.