Priority Queue (heap)

Source: Internet
Author: User

In the case of printer jobs, the queue is generally used in the form of FIFO (fisrt in first out), but when encountering a 1-part and a 100-part job, it is relatively reasonable to print 1 copies, in addition, the priority of different jobs is different, priority should be processed first.

Insert = = Enqueue

Deletemin = = Dequeue

Binary heap (complete binary tree): In addition to the bottom layer, a fully filled two-tree, the underlying elements from left to right fill in.

A fully binary tree is a regular, usable array representation: for any node element x, its parent node element H->ELEMENT[I/2], its left son H->element[2i], right son h->element[2i+1].

A B C D E F G H I J

0 1 2 3 4 5 6 7 8 9 10 11 12 13

A heap of data structures: an array, a capacity, and the current heap size.

Precautions:

Node declaration:

struct heap{       int           capacity;        int            size;       ElementType   *elements;}

Heap order: The keyword of node x is greater than or equal to the keyword of node x Father node.

Priority Queue creat:

Priorityqueue *initialize (intmaxelement) {Priotityqueue*H; if(Maxelement <minpqsize) {cout<<"Priorityqueue is too small"<<Endl; returnNULL; } H->elements = New Element[maxelement +1]; if(H->elements = =NULL) {cout<<"Out of space"<<Endl; returnNULL; } H->capacity =maxelement; H->size =0; H->elements[0] =Mindata;//mindata is a small enough number to ensure that the element is inserted up to i=1 at the root node. returnH;}

Insert: (end insert)

voidInsert (ElementType X, Priotityqueue *H) {       if(Isfull (H)) {cout<<"Out of space"<<Endl; return; }        for(inti = ++h->size; X < h->elements[i/2]; I/=2)//h->elements[0] is a small enough number mindata to ensure that the x<mindata is not established. H->elements[i] = h->elements[i/2]; H->elements[i] =X;}

Deletemin (deleted at root)

ElementType Deletemin (Priorityqueue *i) {       intChild ;       ElementType minelement, lastelement; if(IsEmpty (H)) {cout<<"Priotityqueue is empty"<<Endl; returnh->elements[0]; } minelement= h->elements[1]; Lastelement= h->elements[h->size--];//Save the last element, Size-1                  for(inti =1;2*i <= h->size; i =Child ) { Child=2*i; if(Child! = h->size && H->element[child +1] < h->Element[child]) child++; if(h->element[child]<lastelement) H->element[i] = h->Element[child]; Else                  Break; } H->elements[i] =lastelement; returnminelement;}

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.