7 Operations for Priority queue (heap)

Source: Internet
Author: User
Tags comparable

1. The priority queue has two basic operations: INSERT and delete minimum items (deletemin), which work to find, return, and delete the smallest element in the priority queue. The insert operation is equivalent to Enqueue (queue), and deletemin is equivalent to dequeue (out of the queue). Supplement: C + + provides 2 versions of Deletemin, one to delete the smallest item, and the other to store the deleted value in the object passed by reference while deleting the smallest item.

2. Class interface for priority queues

Template<TypeNameComparable>classbinaryheap{ Public:ExplicitBinaryheap (intcapacity= -);ExplicitBinaryheap (Const  vector<Comparable>& items);BOOLIsEmpty ()Const;ConstComparable & Findmin ()Const;voidInsertConstComparable & X);voidDelelemin ();voidDeletemin (comparable & Minitem);voidMakeempty ();Private:intCurrentSize; vector<Comparable> Array;voidBuildheap ();voidPercolatedown (inthole);};

3. Insert into a binary heap

void insert(const Comparable & x){    if(currentSize==array.size()-1)        array.resize(array.size()*2);    int hole=++currentSize;    for(;hole>1&&x<array[hole/2];hole/=2)        array[hole] =array[hole/2];    array[hole]=x;}

4. Executing deletemin in a binary heap

voidDeletemin () {if(IsEmpty ())ThrowUnderflowexception ();Array[1]=Array[currentsize--]; Percolatedown (1);}voidDeletemin (Comparable & Minitem) {if(IsEmpty ())ThrowUnderflowexception (); minitem=Array[1]; arrary[1]=Array[currentsize--]; Percolatedown (1);}voidPercolatedown (intHole) {intChild Comparable tmp=Array[Hole]; for(; hole*2<=currentsize;hole=child) {child=hole*2;if(child!=currentsize&&Array[child+1]<Array[Child]) child++;if(Array[Child]<tmp]Array[hole]=Array[Child];Else              Break; }Array[Hole]=tmp;}

5. The left-hand heap (leftist heap), like a binary pile, has both structural and sequencing properties. Zoo is also a binary tree, and the only difference between it and a two-fork tree is that the left-hand heap is not ideal-balanced (perfectly balanced), and in fact tends to be very unbalanced. The nature of Zuo: For each node in the heap x, the 0 path of the left son is as large as the 0 path of the son at least less right operand. Since the left-hand heap tends to deepen the left path, the right path should be short, and the right path along the right side of the left-hand heap is indeed the shortest path in the heap. Otherwise there will be a path through a node x and get the left son, at which point the X destroys the nature of the left-hand heap.

6. The basic operation of the left-hand heap is merging (inserts are only special cases of merging). The left-hand heap type declaration:

Template<typenamecomparable>class Leftistheap{public:Leftistheap();Leftistheap(const leftistheap & RHS); ~Leftistheap(); BOOL IsEmpty()Const Constcomparable& Findmin()Const void Insert(const comparable & x); void Deletemin(); void Deletemin(comparable & Minitem); void Makeempty(); void merge(leftistheap & RHS); ConstLeftistheap& operator=(const leftistheap & RHS);p rivate:structLeftistnode{comparableElementLeftistnode*left;Leftistnode*right; int NPL;Leftistnode(const comparable & theelement, leftistnode * lt=NULL, Leftistnode * rt=NULL,int NP=0): element(theelement), left(lt), right(RT), NPL(np){}    };Leftistnode*root;Leftistnode*merge(leftistnode *H1,leftistnode *h2);Leftistnode*merge1(leftistnode *H1,leftistnode *h2); void Swapchildren(leftistnode *t); void Reclainmemory(leftistnode *t);Leftistnode*clone(leftistnode *t)const;};

7. Merging the driver and the actual program of the left-hand heap

voidMerge (Leftistheap&RHS) {if(This==&Rhsreturn; Root=Merge (ROOT,RHS.root); Rhs.Root=NULL;} Leftistnode*Merge (Leftistnode*H1,leftistnode*H2) {if(H1==NULL)returnH2;if(H2==NULL)returnH1;if(H1 -Element<H2 -ElementreturnMerge1 (H1,H2);Else         returnMerge1 (H2,H1);} Leftistnode*Merge1 (Leftistnode*H1,leftistnode*H2) {if(H1 -Left==NULL) H1 -Left=H2;Else{H1 -Right=Merge (H1 -RIGHT,H2);if(H1 -Left -Npl<H1 -Right-NP1) Swapchildren (H1); H1 -Npl=H1 -Right -Npl+1; }returnH1;}

8. Insert procedure and Deletemin program for the left-hand heap

insert(const Comparable & x){    root=merge(new LeftistNode(x).root);}void deleteMin(){    if(isEmpty())        throw UnderflowException();    LeftistNode *oldRoot=root;    root=merge(root->left,root->right);    delete oldRoot;}void delteMin(Comparable & minItem){    minItem=findMin();    deleteMin();}

9. The two-item queue is not a sequential tree, but a collection of stacks, called forests (forest).

10. Two Queue class architecture and node definition:

Template<TypeNameComparable>classbinomialqueue{ Public: Binomialqueue (); Binomialqueue (ConstComparable & Item); Binomialqueue (ConstBinomialqueue & RHS); ~binomialqueue ();BOOLIsEmpty ()Const;ConstComparable & Findmin ()Const;voidInsertConstComparable & X);voidDeletemin ();voidDeletemin (comparable & Minitem);voidMakeempty ();voidMerge (Binomialqueue & RHS);ConstBinomialqueue &operator= (ConstBinomialqueue & RHS);Private:structBinomialnode {comparable element;        Binomialnode *leftchild;        Binomialnode *nextsibling; Binomialnode (ConstComparable & Theelement, Binomialnode *lt,binomialnode *rt): Element (theelement). Leftch ILD (LT). Nextsiblig (RT) {}};enum{default_trees=1};intCurrentSize; vector<BinomialNode*>Thetrees;intFindminindex ()Const;intCapacity ()Const; Binomialnode * Combinetrees (Binomialnode *t1,binomialnode *t2);voidMakeempty (Binomialnode * & T); Binomialnode * Clone (Binomialnode * t)Const;};

11. In STL, the binary heap is implemented by a class template called Priority_queue, which can be found in the standard header file queue. The STL implements a maximum heap instead of the smallest heap, so the item being accessed is the largest item, not the smallest item. The key member functions are as follows:

void push(constObject & x);constObjectconst;void pop();bool empty();void clear();

To make this article get treatise and ask questions, reprint please indicate the source:
Http://blog.csdn.net/nomasp

7 Operations for Priority queue (heap)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.