Algorithm-binary heap
The biggest difference between stack or queue implementation and priority queue implementation is the performance requirements. For stacks and queues, our implementation can complete all operations within a constant time. For priority queues, one of the two operations, insert element and delete maximum element, takes linear time in the worst case. The implementation based on the data structure heap that we will discuss next ensures that these two operations can be executed more quickly.
1. Definition of heap
The binary heap of the data structure can perform basic operations on the priority queue. In the binary heap array, each element must be greater than or equal to the other two elements in a specific position. Correspondingly, the elements at these locations must be at least equal to or greater than the other two elements in the array, and so on. If we plot all the elements into a binary tree, we can easily see this structure by connecting each large element and two small elements with edges.
Definition:When each node of a binary tree is greater than or equal to its two subknots, it is called heap order.
Correspondingly, in a heap Ordered Binary Tree, each node is smaller than or equal to its parent node (if any ). From any node up, we can get a column of non-decreasing elements; from any node down, we can get a column of non-increasing elements.
Proposition:The root node is the largest node in a heap ordered binary tree.
Proof:Summarized based on the nature of the tree
Binary heap Representation
If we use pointers to represent a heap Ordered Binary Tree, each element requires three pointers to locate its upper and lower nodes (each parent node and two child nodes need one ). As shown in. The full binary tree can be expressed only by arrays without pointers. The specific method is to put the node of a binary tree into an array in hierarchical order. The root node is at location 1, and its subnodes are at location 2 and 3, the sub-nodes are located at locations 4, 5, 6, and 7, and so on.
Definition:A binary heap is a group of elements that can be sorted by a fully ordered binary tree and stored in an array in a hierarchical manner (the first position of the array is not used)
For simplicity, we will refer to the binary heap as heap in the following article. In a heap, the parent node of the node k is located at └ k/2, and its two child nodes are located at 2 k and 2 k + 1 respectively. In this way, without using pointers, we can also move up and down the tree by calculating the index of the array: From a [k] to the top layer, k equals k/2, the next layer equals k to 2 k or 2 k + 1.
The structure of the full Binary Tree implemented by using arrays (heaps) is very strict, but its flexibility is enough to enable us to efficiently implement priority queues. With these features, We can insert elements at the logarithm level and delete the largest elements. The algorithm ensures the performance of log complexity by conveniently moving up and down the tree without pointers in the array.
Proposition:The height of a completely N-sized binary tree is └ lgN weight.
Proof:This can be easily proved through induction, and when N reaches the power of 2, the height of the tree will increase by 1
2. Heap Algorithm
The heap operation will first make some simple changes to break the heap state, then traverse the heap and restore the heap state as required. We call this process the ordering of stacks. In the process of ordering, we will encounter two situations. When the priority of a node increases (or a new element is added at the bottom of the heap), we need to restore the heap order from bottom to bottom. When the priority of a node decreases (for example, replacing the root node with a smaller element), we need to restore the heap sequence from top to bottom. First, we will learn how to implement these two types of auxiliary operations, and then use them to insert elements and delete the largest elements.
Ordering from bottom to top (floating)
private void swim(int k) { while (k > 1 && less(k/2, k)) { exch(k, k/2); k = k/2; } }
If the heap's ordered state is broken because a node becomes bigger than its parent node, we need to replace it with its parent node to fix the heap. After the switch, this node is larger than its two child nodes (one is the former parent node and the other is smaller than it because it is the child node of the former parent node ), however, this node may still be larger than its current parent node. We can use the same method to restore order over and over again and move the node up until we encounter a larger parent node. As long as you remember that the parent node of the node where the location is k/2 bytes, this process is easy to implement. The cycle in the swim () method ensures that the heap sequence state is broken only when the node on location k is greater than its parent node. Therefore, as long as the node no longer exceeds its parent node, the orderly state of the heap will be restored. For example
Sorting from top to bottom (sinking)
private void sink(int k) { while (2*k <= N) { int j = 2*k; if (j < N && less(j, j+1)) j++; if (!less(k, j)) break; exch(k, j); k = j; } }
If the heap's ordered state is broken because a node becomes smaller than its two subnodes or one of them, then we can recover the heap by exchanging it with the large ones in its two subnodes. Switching may continue to break the heap's orderly state at the subnode, so we need to constantly fix it in the same way, move the node down until its child node is smaller than it or reaches the bottom of the heap. The corresponding code can be obtained directly from the subnodes of k nodes located at 2 k and 2 k + 1.
The sink () and swim () methods are the basis for efficient implementation of the priority queue API, for the following reasons.
Insert element.Add the new element to the end of the array, increase the heap size, and move the new element to the appropriate position (as shown in the left half ).
Delete the largest element.We delete the largest element from the top of the array and place the last element of the array to the top, reducing the heap size and sinking the element to a proper position (as shown in the right half ).
The heap-based priority queue algorithm solves a basic problem we raised at the beginning: its implementation of the priority queue API ensures that the values of the inserted and deleted maximum elements are in pairs with the queue size.
【Source code download]