A typical application of this data structure in a heap-priority queue
Normal queue: FIFO;
Priority queue: The outbound order is not related to the queued order;
The most typical application of a priority queue is to perform tasks in the computer's operating system, when the operating system performs multiple tasks, the operating system is the CPU's execution cycle is divided into multiple time slices, in each time slice can only perform a task, then what is the first task to do? The answer is that each task has a priority, and the operating system will dynamically select one of the highest priority tasks to start executing, which will require the use of a priority queue. That is, all tasks go into the priority queue, and each time the priority queue is scheduled to determine which task the CPU performs.
Why is it "dynamic"?
Because each time the CPU is processing the task, there are other new tasks to enter.
Now that the priority queue has a good effect on Dynamic Data processing, what about the results of static processing?
The answer is that the priority queue is also advantageous in dealing with static data.
For example: Select the top 100 in 100 million elements?
Abstract: Select the first m element in n elements?
The easy-to-think solution is to sort the n elements and then pick the first m elements. The time complexity is O (NLOGN).
But if we use the priority queue, then our time complexity will be up to O (NLOGM).
If the data to be processed is large, then the use of the priority queue will make the whole process more than 10 times times faster and even more noticeable.
Primary operations for priority queues:
Team
Out of the queue (remove the highest priority element)
Implementation of the priority queue (normal array, sequential array, heap):
Although it seems that the time complexity generated by using this data structure in a heap is less than an ordinary array when it is queued, it is not as sequential as the queue. On average, however, the complexity of using a heap to implement a priority queue to handle a system task is much lower than using an array.
In the most extreme case, for a total of n requests:
Use normal or sequential arrays, worst case: O (n2)
Using heaps, time complexity can be stabilized at: O (NLGN)
What is a heap?
The heap is a tree-like structure, the most classic of which is the binary stack (binary heap). It corresponds to a binary tree.
But this binary tree has several characteristics (for Dagen (or Maximum heap)):
1. Any node on the two fork tree is always not larger than its Father node;
2. The two-fork tree corresponding to the heap is a fully binary tree.
Cases:
Note that in the feature 1 listed above, it does not mean that the larger the number of layers (in the largest heap), such as the size comparison between 19 and 13.
Implementation of the maximum heap:
Implementation of the heap can be implemented using a node with two pointers (the way the tree is implemented).
The classic implementation is to use an array to store the heap. We can use arrays to implement a heap because the heap is a complete binary tree.
If we label each element of a heap a number. Like this:
As we can see, the left child node of each node is twice times the parent node, such as 2==1*2;4==2*2;6==3*2.
The right child node of each node is twice times the parent node plus one, such as 3==1*2+1;5==2*2+1;7==3*2+1.
Note that index number NO. 0 is not used.
The formula is obtained:
Note that division is Computer division, with the exception of endless rounding.
Code (build the maximum heap frame):
Packagecom.heap; Public classMaxheap {Private int[] arr; Private intCount//number of elements in the heap PublicMaxheap (intcapacity) {arr=New int[Capacity];//the capacity of the maximum heap is specified by the userCount=0; } //returns the number of elements of the largest heap Public intsize () {returncount; } Public Static voidMain (string[] args) {maxheap heap=NewMaxheap (100); System.out.println (Heap.size ()); }}
How do I add an element to the maximum heap?
For example, add an element (52) to the previous maximum heap:
At this point, the newly inserted element is at the end of the array where we store the largest heap, which is where it is.
As we can see, the maximum heap definition is not met in the tree where the newly inserted element is located (that is, 16, 15, 52 elements).
So what we're going to do is compare the new insertion element with its parent node, Jovin the insertion node is larger than its parent node, swap the position between the two, then compare the position of the new insertion node swap and its current parent node compare size, and continue swapping if it is larger than its parent node. Until the newly inserted node is no more than its current parent node at its current position.
Code:
Packagecom.heap; Public classMaxheap {Private int[] arr; Private intCount//number of elements in the heap Private intcapacity; PublicMaxheap (intcapacity) {arr=New int[Capacity];//the capacity of the maximum heap is specified by the user This. capacity=capacity; Count=0;//Initialize to 0}//determines whether the current heap is empty PublicBoolean IsEmpty () {returnCount==0; } //returns the number of elements of the largest heap Public intsize () {returncount; } //insert element to maximum heap Public voidInsertintnum) { if(count+1>capacity) System.out.println ("Capacity is full, cannot be inserted"); ElseShifup (Count++); } //adjust the elements in the heap to a suitable location Private voidShifup (intk) {//if the current element is larger than its parent node, swap the position of both if(k>1&&arr[k/2]<Arr[k]) { intTemp=arr[k/2]; Arr[k/2]=Arr[k]; ARR[K]=temp; } Else return;//Cyclic Summary conditionsShifup (k/=2); } Public Static voidMain (string[] args) {maxheap heap=NewMaxheap (100); Heap.insert (3); System.out.println (Heap.size ()); }}
Heap sort (heap sort)