Implementation of resolution from heap to priority queue

Source: Internet
Author: User

A priority queue, as its name implies, is a queue that stores and retrieves data based on a given priority. It can be said that it is a perfect combination of queues and sorting. It not only stores data, but also sorts the data according to the rules we set.

Let's talk about the implementation of priority queue first. One thing to clarify is that many people always think that Priority Queue is a Priority Heap, which is of course one-sided. Since the priority queue is only a combination of data storage and sorting, the following implementation methods can be listed based on the knowledge we have learned: unordered arrays, unordered linked lists, ordered arrays, ordered linked lists, and binary search trees. These methods also have priorities in implementation. The following table compares the time performance of some methods to achieve basic operations:


Figure 1 Comparison of time performance of some implementation schemes of priority queue

In this table, we can sort the priorities based on the time complexity. Of course, you will choose the last binary search tree to implement the priority queue solution, but we actually use heap. Heap is a binary tree. The heap and the binary search tree are similar, but they are also somewhat different: In terms of shape, the binary search tree can have different shapes, the heap can only be a full binary tree. In terms of time performance, there is no obvious difference between the two. The heap is also ordered. We generally divide it into a large heap and a small heap. The value of the child node of the heap is smaller than the value of the parent node. On the contrary, the value of the child node of the heap is greater than that of the parent node.

During implementation, we often use array-based heaps, that is, the elements in the heap are stored in an array, and the storage of this array element follows certain rules: if the parent node is n, the corresponding left and right child nodes are 2n + 1 and 2n + 2, respectively. That is to say, we visually (if we use a picture to visualize the heap) and essentially dress the heap as two things, which are both easy to understand and conducive to implementation, the essence of the implementation is to use arrays because of the performance of arrays.

The Heap has two basic operations: add and delete. Let's talk about adding elements first. Suppose there is a heap like this:

At this time, element 1 is to be added. What should I do at this time? Step 1: add the element to the last position of the heap:

Step 2: Compare the newly added element with the value of its parent node. If the value of the new node is smaller than the value of its parent node (as in this example, exchange the values of the two nodes and repeat the second step until a small top heap is formed:

In this way, a new small top heap is born!

Then, an element is deleted from the heap. Suppose that in the new small top heap, we intend to delete the node with the value of 1:

Step 1: delete this 1. Assume that there is no value on the node:

Step 2: compare the two subnodes of the delete node (currently the top node) to see who has a small value, exchange the smaller value and the value of this empty node (assuming null), and repeat this step until the null value reaches a row on the smallest side:

The third step is to remove the empty node from the line of sight. In this way, the deletion process has come to an end (well, this heap is back before Liberation )!

With these basic principles in mind, it is possible that the increase or deletion of a larger amount of data should be affected.

Some people will question the sequence of other elements except the top element in the heap:

For example, why 4 will be placed on the right brother node of 5 is obviously affected by the binary search tree, because the heap is generally only useful for the heap top element, therefore, you only need to ensure that the top element of the heap is the smallest (for the small top heap ).

Next, we will briefly introduce some basic methods of priorityqueue provided by Sun to better understand the implementation mechanism of priority queue:

1. the first statement of the priorityqueue statement is as follows:

This statement indicates that the priority queue provided by Sun is implemented based on the priority heap;

2. The following is a comment when declaring an object array:

It can be seen that the elements in the heap are saved in the array, and the element with the smallest priority in the queue is always saved in queue [0], provided that the priority queue is not empty.

The method for filtering to priorityqueue is as follows:

In this Code, K represents the position of the currently added element, that is, the last position + 1, and X represents the element to be added. First obtain the value of the parent node, and then compare the value of X with the value of the parent node. If the value of the child node is greater than the value of the parent node, do not change it. Otherwise, the values of the two are exchanged. This method is mainly used to add elements. The corresponding method is siftdowncomparable (), which is used in the implementation of element deletion;

Next we will discuss the implementation of adding elements:

The siftup method is used. In fact, siftupusingcomparator () and siftupcomparable () are called in siftup (). This has just introduced the implementation of filtering. Adding elements here is the process of filtering.

Of course, we generally use the following three methods in use: add, poll, and peek. Add is used to add elements, which are implemented by the offer method internally, peek is used to obtain the heap top element but is not deleted. Poll deletes the heap top element after returning the heap top element.

The above is just a brief introduction to the priority queue. As a widely used data structure, there are many wonderful places in the priority queue, which are waiting for you to discover.

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.