I have got this Binary Heap implementation code in my text book using class. But i don't understand the necessity of key in building a heap.
#define MAXREAL 999999.0
class HeapItem
{
public:
int data; //actual data that is stored
float key; //key value of the data, heap is constructed based on key
};
class MinHeap
{
public:
HeapItem * A; //stores heap items, e.g., nodes
int heapLength;
int * map;
MinHeap() //constructor
{
A = new HeapItem[MAX_HEAP_SIZE];
map = new int[MAX_HEAP_SIZE];
heapLength = 0;
}
//Fills the heap with an array of integers
//key values do not maintain heap property
//May be used in some algorithms such as dijkstra's shortest path
void initialize(int v[], int n)
{
heapLength = n;
for (int i = 0; i<n; i++) //nodes are stored from index 1 instead of 0 in the heap
{
A[i + 1].data = v[i];
A[i + 1].key = MAXREAL;
map[v[i]] = i + 1; //map tracks which vertex is stored at which heap node
}
}
}
What is the function of key and map in the MinHeap?