Algorithm learning-binary heap implementation of priority queue

Source: Internet
Author: User

Priorityquenue

The priority queue is the ADT of the job scheduling class, which is implemented using the binary heap.
The priority queue has at least two operations: insert and deletemin ).

Insert operation diagram:
    • Image Source: www.educity.cn
Delete operation diagram:
    • Source: www.cfanz.cn
Code implementation:
//// Main. CPP // binaryheap /// created by Alps on 14-8-17. // copyright (c) 2014 Chen. all rights reserved. // # include <iostream> # include "binaryheap. H "// # define elementtype intusing namespace STD; priorityquenue initialize (INT maxnum) {priorityquenue P = (Inline) 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-> elements [I/2]> X; I/= 2) {P-> elements [I/2] = p-> elements [I] + P-> elements [I/2]; p-> elements [I] = p-> elements [I/2]-p-> elements [I]; p-> elements [I/2] = p-> elements [I/2] = p-> elements [I];} p-> elements [I] = x ;} else {// error ("The priorityquenue is already full"); 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 ;}} void min (elementtype &, elementtype & B, elementtype & C) {If (B <c) {// RET Urn A <C? A: C; If (B <A) {B = a + B; A = B-A; B = B-a ;}} else {If (C <) {c = A + C; A = C-A; C = C-A;} // return B <C? B: C ;}} void buildheap (priorityquenue p) {int I = 0; for (I = p-> size/2; I> 0; I --) {if (I * 2! = P-> size) {min (P-> elements [I], p-> elements [I * 2], p-> elements [I * 2 + 1]);} else {// P-> elements [I] = p-> elements [I] <p-> elements [I * 2]? P-> elements [I]: p-> elements [I * 2]; If (p-> elements [I]> P-> elements [I * 2]) {int TMP = p-> elements [I * 2]; P-> elements [I * 2] = p-> elements [I]; p-> elements [I] = TMP ;}}} 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, 14); int A [] =, 68, 65, 26, 32, 14}; 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 define # define elementtype intstruct heapstruct; typedef heapstruct * struct; incluinitialize (INT maxnum); void destory (inclup); void makeempty (priorityquenue P ); void insert (priorityquenue P); elementtype deletemin (priorityquenue P); elementtype findmin (partition P); int isempty (priorityquenue P); int isfull (priorityquenue P ); struct heapstruct {int capacity; int size; elementtype * elements ;};# endif



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.