The priority queue is implemented by the heap, and the implementation of the heap is mentioned earlier, and one important operation of the priority queue is:
1. Heap_max O (1)
2. Heap_extract_max O (LGN)
3. Heap_increase_key O (LGN)
4, Heap_insert O (LGN)
The following is the C language implementation
#define Min-100000int Heap_max (struct heap *h) { return h->arr[1];} int Heap_extract_max (struct heap *h) { int max = h->arr[1]; H->ARR[1] = h->arr[h->size]; h->size--; Max_heapify (H, 1); return Max;} void Heap_increase_key (struct heap *h, int pos, int key) { if (H->arr[pos] < key) { H->arr[pos] = key;
while (pos > 1 && h->arr[pos] > H->ARR[POS/2]) { swap (& (H->arr[pos]), & (H->arr[p OS/2])); POS/= 2;}}} void Heap_insert (struct heap * h, int key) { //sets the element after the last element to infinity and then increases its key value h->arr[++ (h->size)] = MIN; Heap_increase_key (H, h->size, key);}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Priority Queue C language implementation