Two-item Queue lookup Insert Merge operation

Source: Internet
Author: User

The source code is as follows:


/*<span style= "color: #ff0000;" > A two power heap </span> is a left ordered heap, consisting of the right subtree as an empty left subtree composed of a complete binary tree root composition <span style= "color: #ff0000;" > Two queue </span>: is a collection of two power heaps. There is no heap of equal size. Its structure is determined by the number of queue nodes to determine the binary representation of the corresponding integer. */#include <stdlib.h> #include <stdio.h> #define maxbqsize 40typedef struct pqnode* pqlink;typedef struct pq* Pq;struct item{int Data;char c;}; struct Pqnode{item key; Pqlink L,r;}; struct pq{pqlink *bq;}; Pqlink z = null;//connects two equal-sized two power heaps Pqlink pair (pqlink p,pqlink q) {if (p->key.data<q->key.data) {p->r = q->l; Q->l = p; return q; } else{q->r = p->l; p->l = q; return p;}} Insert Operation Pqlink Pqinsert (PQ PQ, Item v) {int i; Pqlink C, t = (pqlink) malloc (sizeof *t); c = t, c->l = Z; C->r = Z; C->key = V;for (i=0;i<maxbqsize;i++) {if (c==z) break;if (pq->bq[i] = = Z) {pq->bq[i] = C; break;} c = Pair (C,pq->bq[i]);p q->bq[i] = z;} return t;} Two merge operations in two Queues #define test (C,b,a) (c) + D (b) + 1* (a) void Bqjoin (Pqlink *a, Pqlink *b) {int i; Pqlink C = z;for (i=0;i<maxbqsize;i++) switch (test (c!=z,b[i]!=z,a[i]!=z)) {case 2:a[i] = B[i];break;case 3:c=pair (a[i],b[i]); A[i]=c;break;case 4:a[i]=c ; c=z;break;case 5:c =pair (C,a[i]); a[i] = Z; Break;case 6:case 7:c = pair (C,b[i]);}} Two the operation of removing the largest element in the queue item Pqdelmax (PQ PQ) {int i, max; Pqlink x; Item v; Pqlink temp[maxbqsize];for (i=0,max=-1;i<maxbqsize;i++) if (pq->bq[i]!=z) if (max==-1 | | v.data<pq->bq[i]- >key.data) {max = i; v = pq->bq[max]->key;} x = Pq->bq[max]->l;for (i=max;i<maxbqsize;i++) temp[i]=z;for (i=max;i>0;i--) {temp[i-1]=x; x = x->r; Temp [I-1]->r=z;} Free (Pq->bq[max]);p Q->bq[max] = Z; Bqjoin (pq->bq,temp); return v;} Main () {}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Two-item Queue lookup Insert Merge operation

Related Article

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.