Ich habe diesen Binary Heap-Implementierungscode in meinem Lehrbuch mit Klasse. Aber ich verstehe nicht die Notwendigkeit, einen Haufen zu bauen.Funktion des Schlüssels in binärem 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
}
}
}
Was ist die Funktion von Schlüssel und Karte im MinHeap?
Außerhalb des Kontexts ist es unmöglich zu sagen, was der Zweck des Schlüsselfelds ist. Können Sie uns ein bisschen mehr Code geben, damit wir sehen können, wie es verwendet wird? –