Priority Queue Adt_prioqueue

Source: Internet
Author: User

If the minimum value is defined as the highest priority, use the minimum heap as an example.

each new element must be adjusted upward, the same as the top priority elements to be adjusted downwards, make it a heap.

The adjustment work after inserting a new element into P[j] is done by the Adjustup () function, which compares the path in the opposite direction to the function Adjustdown (), from the bottom up to the parent node.

Line comparison. If the element value of the parent node is greater than the child's node element value, adjust it until either the parent is not larger than the element to be inserted, or the top of the heap is reached.


In the program, the new element is first inserted in q[j], so that the TMP element equals the new element Q[j], starting with i = J, the TMP is compared to its parent q[(I-1)/2]. If TMP is less than q[(i-1)/2], the

q[(I-1)/2] Move down to q[i] until either TMP >= q[(I-1)/2], or the top of the heap has been reached.


If the priority queue is not full, the function append () First inserts a new element at the end of the priority queue and then calls Adjustup () to adjust the queue back to the heap.

If the priority queue is non-empty, the function sever () first assigns the top element of the heap to X, and then replaces the original heap-bottom element with the heap-top element, reducing the heap size by one, and finally using the calling letter

number Adjustdown () to adjust downwards.


Implementation code:

#include "iostream" #include "Cstdio" #include "CString" #include "algorithm" #include "Cassert" using namespace std; Template <class t>class prioqueue{public:prioqueue (int msize = 0); ~prioqueue () {delete []q;} BOOL IsEmpty () const {return n = = 0;}//Priority queue is empty return Truebool Isfull () const {return n = = maxSize;}/Priority ranks full return truevoid Ap Pend (const t& x); The priority queue adds a value of x to the element void Serve (t& x); The highest priority element in the popup queue in the priority queue and assigns the value to Xprivate:void adjustdown (int r, Int j); Adjust the void adjustup (int j) downwards; adjust void Print upward ();  t* q;int N, maxsize;/* data */};template <class t>void prioqueue<t>::P rint () {for (int i = 0; i < n; ++i) cout << q[i] << "\ t"; cout << Endl;} Template <class t>prioqueue<t>::P rioqueue (int msize) {maxSize = Msize;n = 0;q = new T[maxsize];} Template <class t>void prioqueue<t>::adjustup (int j) {int i = j; T tmp = q[i];while (i > 0 && tmp < q[(i-1)/2]) {Q[i] = q[(i-1)/2];i = (i-1)/2;} Q[i] = tmp; Print ();} Template <Class T>void Prioqueue<t>::append (const t& x) {ASSERT (! Isfull ()); q[n++] = x; Adjustup (n-1);} Template <class t>void Prioqueue<t>::serve (t& x) {ASSERT (! Isfull ()); x = q[0];q[0] = Q[--n]; Adjustdown (0, n-1);} Template <class t>void prioqueue<t>::adjustdown (int r, int j) {int child = 2 * R + 1; T tmp = Q[r];while (Child <= j) {if (Child < J && Q[child] > q[child + 1]) child++;if (tmp <= q[child]) b reak;q[(CHILD-1)/2] = Q[child];child = 2 * child + 1;} q[(CHILD-1)/2] = tmp; Print ();} int main (int argc, char const *argv[]) {prioqueue<int> PQ (8);//implementation Process PQ. Append (71); Pq. Append (74); Pq. Append (2);p Q. Append (54); Pq. Append (93); Pq. Append (;p) Q. Append, int i;cout << ENDL;PQ. Serve (i);p Q. Serve (i); return 0;}


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

Priority Queue Adt_prioqueue

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.