Learning algorithm-Priority queue binary heap implementation

Source: Internet
Author: User

Priorityquenue

The priority queue is the ADT of the job scheduling class, which is implemented by a binary heap.


Priority queues have a minimum of two operations: insert and delete minimum (deletemin).

Insert Operation Plot:
    • Image source: www.educity.cn
To delete an operation plot:

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvywxwcze5oti=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast "align=" Middle ">

    • Image source: www.cfanz.cn
Code implementation:
<pre name= "code" class= "CPP" >////main.cpp//binaryheap////Created by Alps on 14-8-17.//Copyright (c) 2014 CH En. All rights reserved.//#include <iostream> #include "binaryHeap.h"//#define ElementType intusing namespace std;    Priorityquenue Initialize (int maxnum) {priorityquenue P = (priorityquenue) malloc (sizeof (struct heapstruct));        if (P = = NULL) {//error ("can ' t malloc memory");    Exit (1);    } p->elements = (ElementType *) malloc (maxnum * sizeof (ElementType));        if (p->elements = = NULL) {//error ("can ' t malloc memory");    Exit (1);    } p->capacity = Maxnum;    p->size = 0;    P->elements[0] = 0; return P;}        void Destory (Priorityquenue p) {if (P! = null && p->elements! = null) {free (p->elements);    Free (P);        }}void makeempty (Priorityquenue p) {if (P! = null && p->elements! = null) {free (p->elements); P->elements = (ElementType *) malloc (p->capacity * sizeof (ElementType));    p->size = 0;    }}int IsEmpty (Priorityquenue p) {if (P! = NULL) {return p->size = = 0;    }else{return 1;    }}int isfull (Priorityquenue p) {if (P! = NULL) {return p->size = = p->capacity-1;        }else{//error ("P is not exist");    return 1;    }}void Insert (priorityquenue P, ElementType X) {int i = 0; if (!        Isfull (P)) {p->elements[p->size+1] = X;        p->size++; for (i = p->size; P-&GT;ELEMENTS[I/2] > X;            I/=2) {P-&GT;ELEMENTS[I/2] = P->elements[i] + p->elements[i/2];            P->elements[i] = P-&GT;ELEMENTS[I/2]-p->elements[i];        P-&GT;ELEMENTS[I/2] = P->elements[i/2] = p->elements[i];    } P->elements[i] = X;        }else{//error ("The priorityquenue is already");    Return }}elementtype findmin (priorityquenue P) {if (!    IsEmpty (P)) {return p->elements[1]; }else{return NULL;    }}elementtype deletemin (priorityquenue P) {ElementType minelement, lastelement;    int temp;    int i = 1; if (!        IsEmpty (P)) {minelement = p->elements[1];        Lastelement = p->elements[p->size];            for (i = 1; i*2 < p->size; i = temp) {temp = i*2; if (temp! = p->size) {if (P->elements[temp] < p->elements[temp+1]) {p->                Elements[i] = p->elements[temp];                    }else{P->elements[i] = p->elements[temp+1];                temp++;                }} if (Lastelement < p->elements[i]) {P->elements[i] = lastelement;//                p->size--;            Break        }} P->elements[i] = lastelement;        p->size--;    return minelement;    }else{return NULL;    }}int Min (ElementType &a, ElementType &b, ElementType &c) {if (b < c) {    if (b < a) {B = a+b;            A = b-a;            b = b-a;        return 1;        }else{return-1;            }}else{if (c < a) {c = a+c;            A = c-a;            c = c-a;        return 0;        }else{return-1;        }}}//void buildheap (priorityquenue P) {//int i = 0;//int j = 0;//for (i = p->size/2; i > 0; i--) {// for (j = i; j*2 <= p->size;) {//if (J * 2! = p->size) {//int temp = Min (P->elements[i],p->elements[i*2],p->elem  ENTS[I*2+1]),//if (temp = = 0) {//j*=2;//}//if (temp = =                1) {//J = j*2+1;//}//if (temp = =-1) {//J *=2;// }//}else{////p->elements[i] = p->elements[i]<p->element S[I*2]? P->elements[i]:P->elements[i*2];if (P->elements[j] > P->elements[j*2]) {//int tmp = p->elements[j*2];//                    P-&GT;ELEMENTS[J*2] = p->elements[j];//P->elements[j] = tmp;//    J *= 2;//}else{//break;//}//}//}////    }//}int Main (int argc, const char * argv[]) {priorityquenue P = Initialize (20);    Insert (P, 13);    Insert (P, 21);    Insert (P, 16);    Insert (P, 24);    Insert (P, 31);    Insert (P, 19);    Insert (P, 68);    Insert (P, 65);    Insert (P, 26);    Insert (P, 32); Insert (P, +);//int a[]={13,21,16,24,31,19,68,65,26,32,14};//int a[]={53,26,41,59,97,31,58};//for (int i = 0; I < 11;    i++) {//p->elements[i+1] = a[i];//}//p->size = sizeof (a)/sizeof (int);    for (int i = 1; I <= p->size; i++) {printf ("%d", p->elements[i]);    } printf ("\ n");//Buildheap (P); Deletemin (P);    for (int i = 1; I <= p->size; i++) {printf ("%d", p->elements[i]);    } printf ("\ n"); return 0;}

The above is the main.cpp file.
The following is the BinaryHeap.h file code:
  binaryheap.h//  binaryheap////  Created by Alps on 14-8-17.//  Copyright (c) 2014 Chen. All rights reserved.//#ifndef Binaryheap_binaryheap_h#define binaryheap_binaryheap_h#define ElementType intstruct Heapstruct;typedef heapstruct *priorityquenue; Priorityquenue Initialize (int maxnum); void Destory (Priorityquenue p); void Makeempty (Priorityquenue p); void Insert ( Priorityquenue P); ElementType deletemin (priorityquenue P); ElementType findmin (priorityquenue p); int IsEmpty (priorityquenue p); int isfull (priorityquenue p); struct heapstruct{    int capacity;    int Size;    ElementType *elements;}; #endif



Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

Learning algorithm-Priority queue binary heap implementation

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.