A collection of Java (vi) priorityqueue

Source: Internet
Author: User

Reprint please indicate source: http://www.cnblogs.com/lighten/p/7299233.html

1. Preface

This chapter describes the priorityqueue--priority queue in the queue, as the name implies, a queue that can specify a specific sort. Some violate the queue's first-in-first-out, but it is in accordance with the orderly out of the queue, but also a more practical class.

2.PriorityQueue

Priorityqueue inherits from the abstract parent class Abstractqueue, and its data structure remains simple:

An array queue, a size, a collation comparer. If the comparer is null, the comparison is sorted according to the CompareTo method of the element itself. This queue and the previous container in the initial size and expansion of the size of the inconsistency, its default size is 11, the size of less than 64 when the original 3 times times, greater than 64 when the expansion after the original twice times.

The Add (E) method of Priorityqueue is the same as the offer (e) approach.

Determine the size of the capacity first, then determine whether it is the first, and then sort.

This sort of looks past somewhat strange, how to have the parent, and subscript is (k-1) >>>1? In fact, the operation here reflects the priority queue of the real data structure, which is actually a binary tree, the two-fork tree stored in the array. The root node is the 0 bits of the array. Give its concrete structure:

It is a binary tree that is drawn at hand, and the following is the position in the array of the corresponding two-fork tree. Binary trees are stored in an array in a simple way, from top to bottom, from left to right. Priorityqueue is a full binary tree with characteristics, and does not allow NULL nodes, the parent node is smaller than the leaf node, this is the heap sort of small top heap. If we can get the following conclusion in the order of array:

Left leaf node = parent node Subscript *2+1

Right leaf node = parent node Subscript *2+2

Parent node = (leaf node-1)/2

The insertion node is also better processed, and the parent node, compared to the parent node is not moving, less than the parent node on the float, this is the role of the Siftup method. By the way, the minimum heap does not guarantee the size relationship of the left and right nodes, only the parent and child nodes are concerned.

From the above narrative, it is not difficult to see the priority queue if not adjusted, there is no guarantee of priority, child node size is unordered. So each time out of the queue is the root element, the root element is certainly the smallest. After removing an element, the structure of the tree is bound to be destroyed, so each removal operation will adjust the tree to ensure that it conforms to the definition.

Siftdown is used to make such adjustments, the specific implementation of the following:

Look at the code to know, after removing the node, it is to determine the left and right nodes of the small one to float, and then cycle up.

When you remove content from any location, it is removed from the last element to become null. Then call Siftdown, re-find the last element should go to the position, other elements to fill the removal bit. In general this should be sufficient, but in some cases the subsequent elements need to be exchanged with the previous. As shown in the code, the last element just fills in the removal of the vacancy, in which case it is not possible to explain that the structure is correct, the reason is that the priority queue does not guarantee the order of the size of the left and right, it is good to understand the problem, and this situation needs to be siftup (), When this exception occurs, the exception is returned, and all other times NULL is returned.

3.

Remove any position of the diagram is no longer given, 2 summary of the end has been described, first Siftdown, and then a siftup.

4. Other reference articles

Http://www.cnblogs.com/CarpenterLee/p/5488070.html

A collection of Java (vi) priorityqueue

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.