Heap-to-heap sequencing-priority queue

Source: Internet
Author: User

In the previous section we wrote about the tree and the two-tree knowledge.

http://blog.csdn.net/wtyvhreal/article/details/43487095


The heap is a special complete binary tree.


All parent nodes are smaller than child nodes, so a complete binary tree is called the minimum heap, and the inverse is called the maximum heap.


A complete binary tree, tuned for the minimum heap step:


The following code is adjusted downwards:


From the above you can get: the time complexity of adjusting the heap is O (logn).


If the heap size is n, then the time required to insert a new element is O (logn).

Insert the value 3 in the smallest heap just now.

The code for upward adjustment is as follows:


Build a heap: Start with an empty heap and insert each element into the heap in turn. The overall time complexity is O (NLOGN).


Build the heap with O (N) complexity below.

。。。


n elements to build a heap:

You can first put the N nodes to the top-down, from left to right in the way from 1 to n encoding, so that the N node can be converted to a complete binary tree, and then from (the last non-leaf node) the first N/2 node (n is the total number of nodes) to the root node to process the complete binary tree (the last non-leaf node is the first n/ 2 nodes). Adjust the current node downward as needed until the characteristics of the heap are met. The time complexity is O (N).

Proving time complexity is O (N):


First, the loop is from i = N/2-1, which means that this is a bottom-up build heap. Thus, 1/2 of the elements are compared down one at a time, 1/4 of the downward comparison two times, 1/8 of the downward comparison 3 times, ..., 1/2^k of the downward comparison of K times, where 1/2^k <= 1, K is approximately equal to LG (N). So there is the total comparison:

T = () * n

Make S =

S =
S-1/2s = 1/2s =
It's obvious to be here, S <= 2
So t <= 2n = t = O (n).



The time complexity of heap sequencing is also O (NLOGN).

For example, from small to large, first build the smallest heap. Then each time the top element is removed and then adjusted until the heap is empty.



There is also a stacking method that builds the largest heap, rather than the smallest heap built above.

When the maximum heap is established, the largest element is h[1], Exchange h[1] and H[n], at this time H[n] is the largest element. Then the swap h[1] is adjusted downward to preserve the properties of the heap. Swap once, n--, repeatedly until the size of the heap becomes 1. The h array is already sorted.


Summarize:

Support for inserting elements and finding the most value of the data structure is called the priority queue. A heap is a priority queue .


The heap is also often used to find the number of K in a series, only to establish a minimum size of K, heap top is the number of k large, time complexity O (NLOGK).

(10 number to find the 3rd largest number, the first arbitrary 3 number, built the smallest heap, and then starting from the 4th number, compared to the heap top, small ignore, bigger than the heap top, then discard the current heap top and will this number as a new heap top, and then to maintain ... )



Heap-to-heap sequencing-priority queue

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.