Use the heap to implement the priorityqueue class template (vector)

Source: Internet
Author: User

Important Concept: heap is a Complete Binary Tree with keys and satisfying the following "heap nature": Keys in any path from the root node to the leaf node are not incremented. Heap is used to implement priority queue, because it allows O (lg n) insertion and deletion, which is due to push () and POP () A function is implemented by traversing the path from the root node to the leaf node through the heap. This path is not longer than the height of the tree. It is at most (lg n ).

# Ifndef mypriorityqueue_h # define mypriorityqueue_h # include <algorithm> # include <vector> using namespace STD; Template <class T> class priorityqueue {public: priorityqueue () {} priorityqueue (const priorityqueue & Q): Val (Q. val ){}~ Priorityqueue () {} priorityqueue & operator = (const priorityqueue & Q) {Val. clear (); val = Q. val;} int size () const {return Val. size ();} bool empty () const {return Val. empty ();} const T & Top () const {return Val. front ();} void push (const T &); void POP (); protected: Private: vector <t> val; void heapifydown (); // store elements from the top of the heap. If the top of the heap is smaller than the child element, the elements are exchanged. If both the child elements meet the requirements, void heapifyup () is exchanged with the large ones (); // re-store elements from the bottom of the heap. If the elements at the bottom of the heap are greater than the parent element, they are exchanged until the top of the heap}; Template <class T> void priorityqueue <t >:: heapifydown () {int Len = Val. size (); For (INT I = 0; I <Len;) {Int J = I * 2 + 1; if (j <Len & Val [I] <Val [J]) {If (J + 1 <Len & Val [J] <Val [J + 1]) {swap (Val [I], Val [J + 1]);} elseswap (Val [I], Val [J]); I = J ;} else {If (++ j <Len & Val [I] <Val [J]) {swap (Val [I], Val [J]); I = J ;} elsebreak ;}}template <class T> void priorityqueue <t >:: heapifyup () {int Len = Val. size (); For (INT I = len-1; I> 0;) {Int J = (I-1)/2; If (Val [I]> Val [J]) {swap (Val [I], Val [J]); I = J;} elsebreak;} template <class T> void priorityqueue <t> :: push (const T & T) {Val. push_back (t); heapifyup ();} template <class T> void priorityqueue <t >:: POP () {Val. front () = Val. back (); Val. pop_back (); // Delete the heap top element. Replace heapifydown () with the heap bottom element.} # endif
Test function: int _ tmain (INT argc, _ tchar * argv []) {typedef priorityqueue <int> PQ; PQ Q1; q1.push (44); q1.push (66 ); q1.push (33); q1.push (88); q1.push (77); q1.push (55); q1.push (22); While (! Q1.empty () {cout <q1.top () <""; q1.pop () ;}return 0 ;}

Storage array: 88 77 55 44 66 33 22
Output result: 88 77 66 55 44 33 22

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.