Several applications of (binary) heap

Source: Internet
Author: User
Reprint Address: http://www.cnblogs.com/chihits/archive/2011/05/02/1987637.html

1. As a priority Queue, this is a classic. Do not say.

2. Merge elements from multiple ordered containers into a large, orderly container.
Assuming there are N ordered containers, we want to put the elements inside these containers in an orderly way into a large container. Let's call n an ordered container as a small container. Maintain a cursor,cursor for each small container just start pointing to the first element in the small container, the smallest element. In each loop, a minimum heap is established for n elements pointed to by n cursor, from which the smallest element of extract is added to the large container, and the cursor of the small container extract the smallest element is moved backward; If the cursor of a container reaches end, the minimum heap size is reduced by one.
Because each time we join a large container is the smallest element in all of the current small containers, so the large containers are ordered.


3. Find the maximum number of M in N number (a friend was asked this question during an interview at Google HQ)
First, a minimum heap of size m is established for the first m number (note is the smallest heap, if the smallest number of M is found then the largest heap is established), for each of the following numbers, compare it with the minimum heap of the root (that is, the smallest element in the smallest heap, which we call the current minimum value) size. If the new value is smaller than the current minimum value, skip it; If the new value is larger than the current minimum value, delete the minimum value, insert the new value into the minimum heap, and then make the heap adjustment. The complexity of the algorithm is O (n lg m).
In this way, we can guarantee that after each loop, the minimum number of M (which can be seen as a cyclic invariant) remains the largest in the smallest heap. After processing the n number, the smallest heap gets the largest number of M.
The most straightforward method of this topic is, of course, to sort the n numbers, and the complexity is O (n lg N). Another way is to get random selection based on the idea of quick sorting. Random-select (array, n, K) can find the largest (small) number of K in an array of length n, while dividing the array into two parts, the first part is smaller (larger) than Array[k], and the latter part is larger (small) than the array[k. At the desired level, the algorithm is linear.


4. A set of continuously generated random numbers found and maintained in the median value. (in English: numbers is randomly generated and passed to a method.) Write a program to find and the maintain the median value as new values is generated.)
The idea is to maintain a maximum heap and a minimum heap. The maximum heap maintains elements less than median, and the smallest heap maintains elements greater than median. If the new incoming element is smaller than median, insert the largest heap, and the smallest heap is inserted larger than the median. At the same time the size of the two heaps is always equal or the maximum heap is one more than the smallest heap (of course the smallest heap is more than one of the largest heaps); This nature is very easy to maintain, we just have to move one element from one heap to another.

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.