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;
}