On top of the heap

Comments on the top of the heap

We know that the heap is a very useful data structure. It can maintain the data in a short time into a sequence of monotonically increasing / decreasing. But this "simple heap" for problem solving play effect is limited after all. So we pile on the basis of simple, in-depth thinking and appropriately modified, so that it can solve the problem with some other simple heap can not be resolved, and the idea becomes simple and effective.

A branch of this essay on the heap - on top of the heap to explain, before reading the reader should grasp the basic concepts of the heap, in order to facilitate a better understanding of the top pile.

If the heap large root stack like into a wider at the triangle, the rootlets stack like into a narrow width triangle, then the top of the heap may particularly be thought of as a "gyro" or a "hourglass "by combination of two vertical stacks, we can put a set of data were added to the top of the heap and stack large root rootlets stack, to maintain our different needs.

So on top of the heap is doing with it?

As an example:

Given \ (N \) digits, before it is seeking \ (I \) th element of the first \ (K \) that a small element.

It is easy to think of using the heap to maintain the monotonically increasing sequence, if implemented using an array, we direct input array subscript \ (K \) of the element can be. But we are using the heap, its implementation - priority queues are not supported by any of the access point, then we can not be a single point of inquiry. The idea of the concept of the top of the heap, we can solve the problem:

Although the priority queue does not support any point of access, but you can use \ (O (1) \) time to check out the top of the heap element, that is to say, we can only heap stack top two elements of the stack on top of the maintenance carried out by monotonic maintenance. How to do it?

The principle is simple: the mathematical inequalities passed in principle, if a set of \ (A \) is the smallest element of the set than the other \ (B \) is the largest element bigger, then it can be concluded that: \ (A \ ) All elements than \ (B \) elements large. So, we have a small root heap "on the" large root heap "above", if small root Mound top element is larger than the large root heap top of the heap element, so big all the elements of all the elements of a small root heap than large root heap.

Back to the question: We ask first (K \) \ small elements, then we have a large number of elements in the root of the heap limit to \ (K \) months, before \ (K \) after the element into the team, each element compared with the first element before the top of the stack into the team, if the element is larger than the top of the heap, joined a small pile root, if not, the top of the heap large root heap of pop, new elements will be added to the large root heap. This will maintain a heap on top. Its role is to find the first \ (K \) smaller elements .

Similarly, on top of the heap it can also be used to solve other "first \ (K \) small" deformation problems: for example, before seeking \ (i \) elements such as median.

Guess you like

Origin www.cnblogs.com/fusiwei/p/11432323.html