Algorithm-Two fork heap

Source: Internet
Author: User

The biggest difference between implementing a stack or queue and implementing a priority queue is the performance requirements. For stacks and queues, our implementations can do all of the work in constant time, while for priority queues, inserting elements and deleting the largest elements in the worst case requires linear time to complete. The next step in the implementation of the data structure-based heap is to ensure that both operations are performed faster.

1. Definition of a heap

The binary heap of data structure can implement the basic operation of priority queue well. In an array of binary heaps, each element is guaranteed to be greater than or equal to an element of another two specific position. Accordingly, the elements of these positions are at least greater than or equal to another two elements in the array, and so on. If we draw all the elements into a binary tree, it is easy to see the structure by connecting each of the larger elements with the two smaller elements.

definition: When each node of a binary tree is greater than or equal to its two sub-nodes, it is called a heap order.

Accordingly, in a heap-ordered two-fork tree, each node is less than or equal to its parent node, if any. We can get a non-descending element from any node upward, and we can get a list of non-incrementing elements from any node.

proposition: The root node is the largest node in a heap-ordered two-fork tree

Proof: According to the nature of the tree to induce

Binary stacking notation

If we use pointers to represent an ordered two-fork tree, then each element needs three pointers to find its upper and lower nodes (the parent node and two child nodes each need one). As shown in. A fully binary tree can be represented by an array without a pointer. The specific method is to put the nodes of the two-fork tree into the array in order of hierarchy, the root node in position 1, its sub-nodes in positions 2 and 3, and the child nodes of the node are in position 4, 5, 6 and 7, and so on.

definition: a two-fork heap is a set of elements that can be sorted by a full binary tree in a heap order and stored hierarchically in the array (the first position of the array is not used)

For simplicity's sake, we'll refer to the two-fork heap as a heap below. In a heap, the parent node of the node of position K is └k/2┘, while its two sub-nodes are positioned at 2k and 2k+1 respectively. This allows us to move up and down the tree by calculating the index of the array without the use of pointers: from a[k] up one layer to K equals K/2, and the next layer makes K equal to 2k or 2k+1.

The structure of a fully binary tree implemented with an array (heap) is very strict, but its flexibility is enough to enable us to implement the priority queue efficiently. With them we will be able to implement the operation of inserting elements on several levels and deleting the largest elements. With the convenience of moving up and down the tree without pointers in the array and the following properties, the algorithm guarantees the performance of the logarithmic complexity.

proposition: The height of a complete binary tree of size n is └lgn┘

Proof: It is easy to prove this by induction, and when N reaches a power of 2, the height of the tree increases by 1.

2. Heap algorithm

The heap operation will first make some simple changes, break the heap state, and then traverse the heap and restore the heap state as required. We call this process the order of the heap. In the process of ordering, we will encounter two kinds of situations. When a node's priority rises (or a new element is added to the bottom of the heap), we need to restore the heap from bottom to top. When a node's priority drops (such as replacing the root node with a smaller element), we need to restore the heap from top to bottom. First, we'll learn how to implement both of these auxiliary operations, and then use them to implement the actions of inserting elements and deleting the largest elements.

Ordered by the bottom-highest heap (floating)

private void swim (int k) {while        (K > 1 && less (K/2, K)) {            exch (k, K/2);            K = K/2;        }    }

If the ordered state of a heap is broken because a node becomes larger than its parent node, then we need to fix the heap by swapping it and its parent node. After swapping, this node is larger than its two sub-nodes (one is the parent node, the other is smaller than it is the child node of the parent node), but the node may still be larger than its current parent node. We can restore order over and over again in the same way, moving the node up and down until we meet a larger parent node. This process is simple to remember as long as the location of the parent node of the location K node is └k/2┘. The loop in the swim () method guarantees that the ordered state of the heap will be broken only if the node on position K is larger than its parent node. So as long as the node is no longer larger than its parent node, the heap's ordered state is restored. Such as

Ordered from top to bottom heap (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 ordered state of a heap is broken because a node becomes smaller than its two sub-nodes or one of them, then we can recover the heap by swapping it with the larger of its two sub-nodes. The interchange may continue to break the ordered state of the heap at the child nodes, so we need to fix it in the same way, moving the node down until its sub-nodes are smaller or reaching the bottom of the heap. The sub-nodes of nodes with location K at 2k and 2k+1 can get the corresponding code directly.

The sink () and swim () methods are the basis for efficient implementation of the priority queuing API for the following reasons.

inserts an element. we add the new element to the end of the array, increase the size of the heap and let the new element float to the appropriate position (as shown in the left half).

deletes the largest element. we delete the largest element from the top of the array and put the last element of the array to the top, reducing the size of the heap and letting the element sink to the appropriate position (as shown in the right half).

The heap-based priority queue algorithm solves a fundamental problem we put forward at the outset: its implementation of the priority queuing API guarantees that both the insertion element and the deletion of the maximum element are used and the size of the queue is only a logarithmic relationship.

" Source Download "

Algorithm-Two fork heap

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.