in the explanation before Priorityqueue, you need to familiarize yourself with an ordered data structure: the minimum heap .
The minimum heap is a sort of complete binary tree in which none of the non-endpoint values are greater than the value of their left child and right child node.
It can be concluded that if a binary tree satisfies the minimum heap requirements, then the heap top (root node) is the smallest element of the entire sequence.
Examples of the minimum heap are as follows:
It can be noted that the two sub-nodes of the three ,and their tertiary nodes, There is no strict size requirements. Traversing from the root node in a breadth-first manner can form a sequence:
[10,20,30,31,21,32,70]
In turn, the formula that makes up a binary tree can be pushed: for the element labeled i in the sequence , the left child is Ieft (i) = i*2 +1, the right child is "R " (i) = "L" (i) +1 .
Now you can think of a question, how do you make it satisfy the nature of the minimum heap for a given sequence?
For example [20, 10, 12, 1, 7, 32, 9] , which can form a binary tree:
Here's a way to do this:
1, the reverse traversal sequence;
2, the settlement process is: and the minimum value of the sub-node, if more than the minimum value, then the sub-node Exchange data, and vice versa do not do processing, continue 1 process;
3, the settlement after the node, again sedimentation, until the leaf node.
at the same time, since the node after the SIZE/2 is a leaf node, no comparison is required, so it is possible to step backwards from the size/2-1 position and save the execution times.
This method is used to parse the previous sequence:
1, the series [20,10,12,1,7,32,9 ] Length is 7, so size/2-1 =2, the reverse traversal process is ->20;
2, the left child for the 9, theright child for the12>9, the settlement, the results as shown:
3 , the left child is 1, the right child is 7,ten > 1 , and the result is as shown in the settlement:
4 , the left child is 1, the right child is 9,> 1, the settlement, The results are as follows:
5 , the left child is ten, the right child is 7,> 7, the settlement, Get the final result:
satisfies the minimum heap requirements, at which point the resulting sequence is [1,7,9,10,20,32,12] .
the process of this implementation is also The process of Priorityqueue the heapify method,theheapify method is responsible for converting the sequence to the smallest heap, the so-called build heap . Its source code is as follows:
Private void heapify () { for (int i = (size >>> 1)-1; I >= 0; i--) SIFTD Own (I, (E) queue[i]); }
the Siftdown method is the settlement previously mentioned .
Siftdown (k,x) method parsing
Siftdown This method, depending on whether the comparator member variable is null, has a slightly different way of doing it :
if comparator is not null, call comparator to compare;
Conversely, the element is considered Comparable for comparison ;
if the element is not Comparable is implemented, it throws classcastexception.
Either way, the algorithm executed is the same. Comparator Source parsing:
Private voidSiftdownusingcomparator (intK, E x) { //find only non-leaf nodes inthalf = size >>> 1; while(K <half) { //left child intChild = (k << 1) + 1; Object C=Queue[child]; //Right Child intright = child + 1; //take the smallest of the left and right children if(Right < Size && Comparator.compare ((e) C, (e) queue[right]) > 0) C= Queue[child =Right ]; //parent node Smaller than minimum child description satisfies minimum heap, end loop if(Comparator.compare (x, (E) c) <= 0) Break; //Exchange parent node and minimum child position, continue settlementQUEUE[K] =C; K=Child ; } Queue[k]=x;}
The comment has explained the code's execution logic to sink the parent node that does not meet the minimum heap condition to the bottom. as can be seen from the above code, The time complexity of siftdown will not exceed O (logn).
Siftup (k,x) method Analytical
The Siftup method is used to elevate nodes. The newly added node must be at the bottom of the sequence, in order for the sequence to satisfy the minimum heap nature, the node needs to be promoted.
like Siftdown , it also has two equivalent implementations of paths, where only shifupusingcomparator parsing is done:
Private voidSiftupusingcomparator (intK, E x) { while(k > 0) { //Find parent Node intParent = (k-1) >>> 1; Object e=Queue[parent]; //when the parent node is small, it satisfies the minimum heap nature and terminates the loop if(Comparator.compare (x, (e) e) >= 0) Break; //Exchange the newly added node and parent node locations to continue the elevation operationQUEUE[K] =e; K=parent; } Queue[k]=x; }
The insertion of a node is at the end of the sequence, which is likely to be smaller than the parent node and does not satisfy the definition of the minimum heap, so a floating operation is required.
Here is an example to help understand that there are minimal heap sequences [Ten , +, +, +, , [] , make up the minimum heap as follows:
1, the implementation of add , changed to:
2,19<40, and the swap location:
3,19<20, and Exchange Location:
4,19>10, the endof the floating operation, the final number of the series are:
[Ten , A., , - , + ]
Satisfies the nature of the minimum heap.
"Xu Xu Daolai java": Priorityqueue and minimal heap