Like a heap, there are two types of Queues: the maximum priority queue and the minimum priority queue. An Application of the maximum priority queue schedules jobs on a time-sharing computer (which is useful at last). For the maximum priority queue, the following operations are supported:
1) insert (S, x) inserts element x into queue s
2) maximum (s): returns the maximum element in S.
3) EXTRACT-MAX (s): Remove and return the maximum element in S
4) The INCRESE-KEY (S, X, key) inserts the value key at the X position.
The priority queue is implemented by the heap. This is what we can see.
Take page 82 as an example.
DemonstrationProgramAs follows:
// ================================================ ==================================================================== // Name: prioity. CPP // Author: Xia // version: 1.0 // copyright: NUAA // description: simulation Implementation of priority queue ========================================================== ==## include <iostream> # include <climits> // int_min # include <vector> // Vector # include <algorithm> // for_eachusing namespace STD; int parent (int I) {// return to section I If (I> 0) return I/2; Return-1;} void maxheapify (vector <int> & V, int I, int heap_size) {// Let a [I] Drop in the maximum heap, and make the subtree with I as the root become the big top heap int left = 2 * I; // L <-left (I) int right = 2 * I + 1; // R <-Right (I) int largest = I; If (left <= heap_size) & (V [left]> V [I]) largest = left; else largest = I; If (right <= heap_size) & (V [right]> V [Largest]) largest = right; // find the largest if (largest! = I) {swap (V [I], V [Largest]); // exchange enables the I and sub-heap to satisfy the heap nature, but the largest is the root of the vulnerability, recursively adjust maxheapify (v, largest, heap_size) ;}} void buildheap (vector <int> & V) {// create heap for (INT I = v. size ()/2; I> 0; I --) maxheapify (V, I, V. size ()-1);} int heapmaximum (vector <int> A) {// returns the largest element on the stack, return a [1];} int heapextractmax (vector <int> & A) {// remove and return the maximum int heap_size =. size ()-1; if (heap_size <1) {cout <"heap underflow" <Endl; exit (exit_failure);} int max = A [1]; A [1] = A [heap_size]; heap_size --;. pop_back (); maxheapify (A, 1, heap_size); Return Max;} void heapincreasekey (vector <int> & A, int I, int key) {// Insert the number of keys at I and adjust the heap if (Key <A [I]) {cout <"new key is smaller than current key" <Endl; exit (exit_failure);} A [I] = key; while (I> 1) & (A [Parent (I)] <A [I]) {swap (A [I], a [Parent (I)]); I = parent (I) ;}} void maxheapinsert (vector <int> & A, int key) {. push_back (int_min); // pseudo Code Heapsize + 1, and insert negative infinity heapincreasekey (A,. size ()-1, key); // The second parameter is actually heap size} template <class T> class print {public: void operator () (T & T) {cout <t <";}}; const int heap_size = 10; // assume that the element is 10 int main () {int temp [heap_size] = {16, 14, 10, 8, 7, 9, 3, 2, 4, 1}; vector <int> V (temp + 0, temp + heap_size); print <int> Print; v. insert (v. begin (), 0); // insert 0 to conform to the pseudo-code buildheap (V); // although our array order already conforms to the heap, however, it is common sense to create a cout <"Initial heap v" <Endl; for_each (v. begin () + 1, V. end (), print); // traverses the output cout <Endl; cout <"insert a try" <Endl; heapincreasekey (v, 9, 15 ); // p82 fig for_each (v. begin () + 1, V. end (), print); cout <Endl; cout <"extract count" <Endl; cout <"max ="
The running result is as follows:
Description of each part of the programSource code:
1. To be closer to pseudo code, we do not need to use the zero position of the vector, so left [I] = 2 * I, right [I] = 2 * I + 1;
2. Although our initial array already complies with the heap concept, this is just a coincidence. For any array, we should first create a heap for it, buildheap
It seems that the second step is inserted, just like the figure on the 82 pages of the book, which changes the priority. The number of inserts in the fifth step is more like a new job. Is it like Job Scheduling? Haha, heap, unexpected is also a high level of goods...
A heap on a copybook can support any priority queue operation on a set of N in O (lgn) time.
See the MAX-HEAP-INSERT process, is there a very familiar? According to question 6.1, when we build a heap, we can use the maxheapinsert process. This is not enough to create a heap. It feels like sorting inserts.
The process is as follows:
Void buildmaxheap (vector <int> & V) {v. clear (); For (INT I = 0; I <v. size (); I ++) maxheapinsert (A, V [I]);}
Some ideas in Chapter 6:
As for heap sorting, I personally think it is similar to selecting sorting. The largest one is selected each time, but the comparison is done by each sub-node with three or three nodes. The sorting scale will decrease, although this is the same as the choice, the heap introduces recursion (there are also recursion in insertion, but it is not much faster, so the factor of heap sorting is not recursion), and there is a heap idea of division, separate each sub-heap. As with the division of the fast sorting, the upper and lower parts do not need to be compared, thus greatly reducing the number of comparisons compared with the selected sorting.
For priority queues, I used to know queue scheduling. Although queues can be used for scheduling, the structure like heap can be used to organize the queues. After a clear picture is drawn, I find that the understanding will be more profound, also more image.
Cainiao goes on ~~~