Summary
This article introduces the priority queue from two aspects, 1. The common functions of the priority queue; 2. Priority Queue Code implementation: Heap sequencing and Huffman tree.
Body
I. List of priority queue functions
- Empty () returns true if the priority queue is null
- Pop () Delete the first element
- P Ush () add an element
- Size () returns the number of elements owned in the priority queue
- Top () returns the element with the highest priority in the priority queue
two. Priority Queue code Use cases
Example 1 #include <iostream> #include <queue> #include <deque> #include <vector> #include <funct Ional> using namespace std; #if _msc_ver > 1020//If VC + + version is > 4.2 using namespace std; STD C + + LIBS implemented in STD #endif//Using priority_queue with deque//use of function greater sorts the IT EMS in ascending order typedef deque<int, allocator<int> > Intdqu; typedef priority_queue<int,intdqu, greater<int> > Intprque; Using priority_queue with vectors//use of function less sorts the items in descending order typedef Vector<char, allocator<char> > Chvector; typedef priority_queue<char,chvector,less<char> > Chprque; void Main (void) {int size_q; Intprque Q; Chprque p; Insert items in the Priority_queue (uses deque) Q.push (42); Q.push (100); Q.push (49); Q.push (201); Output the item at the top using top () cout << q.top () << ENdl Output the size of priority_queue size_q = Q.size (); cout << "Size of Q is:" << size_q << Endl; Output items in Priority_queue using Top ()//and use POPs () to get to next item until//Priority_queue is empty while (!q.empty ()) {cout << q.top () << Endl; Q.pop (); }//Insert items in the priority_queue (uses vector) P.push (' C '); P.push (' a '); P.push (' d '); P.push (' m '); P.push (' h '); Output the item at the top using top () cout << p.top () << Endl; Output the size of priority_queue size_q = P.size (); cout << "Size of P is:" << size_q << Endl; Output items in Priority_queue using Top ()//and use POPs () to get to next item until//Priority_queue is empty while (!p.empty ()) {cout << p.top () << Endl; P.pop (); } }
Output: Size of Q is:4 42 49 100 201
m size of p is:5 m h D C a
Example 2#include<iostream> #include <queue> using namespace std; struct CMP { bool operator () (const int &a,const int &b) { return a<b;//sorted in ascending order } }; typedef priority_queue< int, VECTOR<INT>, cmp > qu; void Main () { Qu P; P.push (a); P.push (+); P.push (); P.push (201); while (!p.empty ()) { cout << p.top () << Endl; P.pop (); }
Output Result: 201 the
Example 3 (Implementation of Huffman tree with Priority_queue): #include <iostream> #include <queue> using namespace std; Class Node {public:int weight; Node* left; node* right; Node (int w, node* L, node* R): Weight (W), left (L), right (R) {} Node (int w): Weight (W), Left (null), right (null) {}}; Class CMP//functor for Priority_queue class {Public:bool operator () (node* a,node* b) {return a->weight>=b-> ; weight; } }; The pointer is passed in, if the object is implemented, you do not know the direction of the left and right pointers//in the sequence traversal void Inorder (node* p) {if (P! = NULL) {inorder (p->left); cout<<p->weight<< '/t '; Inorder (P->right); }} void Freetree (node* p)//Destroy binary tree {if (p->left!=null) Freetree (p->left); if (p->right!=null) Freetree (p->right); Delete (p); } int main () {node* m1,*m2; Priority_queue<node*,vector<node*>,cmp> Q; for (int i=0;i<6;++i)//6 nodes {int N=rand ()%100; Q.push (new Node (n));//The weighted value randomly generates cout<<n<< '/t '; } cout<<endl; for (iNT J=1;J<6;++J)//merge 5 times {m1=q.top (); Q.pop (); M2=q.top (); Q.pop (); int w=m1->weight+m2->weight; Q.push (New Node (W,M1,M2)); } node* root=q.top (); Inorder (root); cout<<endl; Freetree (root); System ("pause"); return 0; }
Output Result: 0 0 235 136
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Analysis of application Method of C + + priority queue