Priority queue implemented by heap

Source: Internet
Author: User



I recently learned a new data structure and found that I didn't take this course seriously before.


The maximum (small) Heap obtained through the full binary tree to get a fast priority queue.


Max (small) Heap: A Complete Binary Tree, except that all root nodes are greater than (smaller than) Its subnodes.


When adding an element to the heap, you need to re-arrange the maximum heap, but the time only needs log2 (n + 1). When deleting the root element, you also need to re-arrange it, but the time is also log2 (n + 1)


Insert element:

Because the heap is a complete binary tree, all the new elements are added to the last position,


Add element 13 --


<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + pushed/LXEuLjH17HIvc8go6wgyOe5 + pushed + zc3qs8m808jroaM8L3A + pushed = "brush: java;"> void max_heap: push (int x) // input the element {int k; k = ++ length to the maximum heap. while (k> 1) {if (x> heap [k/2]) {heap [k] = heap [k/2]; k = k/2;} else break;} heap [k] = x ;}

Delete element:

Deleting an element is to delete the root node. After the root node is deleted, the last node is mentioned to the root node,

After the root element is deleted


Implementation: after the final element is mentioned to the node location, you need to use this to compare it with the node and its son, and then repeat the same steps of inserting the element.


Code:

Void max_heap: pop () {int x = 1, y; int z = heap [length --]; y = 2; while (y <= length) // determine whether the son exists. {// The encoding method is if (y <length & heap [y] 

The complete code is provided below:


# Include
 
  
Using namespace std; class max_heap {public: max_heap () {length = 0;} max_heap (int x) {length = 0 ;}//~ Max_heap () {delete [] heap;} void push (int x); int front (); void pop (); int empty (); private: int max_size; int length; int heap [100] ;}; void max_heap: push (int x) // medium pressure element {int k; k = ++ length to the maximum heap; while (k> 1) {if (x> heap [k/2]) {heap [k] = heap [k/2]; k = k/2 ;} else break;} heap [k] = x;} int max_heap: front () {return heap [1];} int max_heap: empty () {if (length! = 0) return 0; else return 1;} void max_heap: pop () {int x = 1, y; int z = heap [length --]; y = 2; while (y <= length) // determines whether the son exists. {// The encoding method if (y <length & heap [y] 
   

<

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.