Comparison of several heap structures

Source: Internet
Author: User

Everyone is familiar with the heap. It is nothing more than the maximum heap and the minimum heap. The heap is widely used. The top k Number of the priority queue and Uncle group can all be implemented using the heap, the time complexity is low. However, there are several different methods for implementing the heap. They have their own advantages.

Based on the underlying implementation of the heap, it can be divided into sequential storage heap and chain storage heap. Chain storage is divided into left-type heap, oblique heap, and two heap.


1. The sequential storage heap is the heap we often come into contact with. It is implemented using arrays and logically equivalent to a Complete Binary Tree. We are already familiar with its implementation and heap sorting. We will not repeat it here. The disadvantage of the sequential heap is that it takes a relatively high time complexity to merge two heaps, normally, elements are taken from one heap and inserted to another. The time complexity is O (nlogm). Note: assume that the two heap elements are n, m, respectively. You can also use an m + n Array to store all elements in the array and perform an initial heap operation. This reduces the time complexity, but requires additional memory overhead.


2. The chain heap solves the preceding defects, and all heap operations can be converted to merge operations.

2.1 left-side heap: Before introducing left-side heap, we will introduce a concept-zero path length: the shortest path length between a node and a node without two sons, the zero path of the null node is-1. The left heap requires that the path length of the Left subtree be no less than that of the right subtree. When the two left heap types are merged, the elements on the top of the heap are compared first ), merge the right subtree of the heap with a small top value with another heap and perform recursion until one heap is null and returns the result. after merging, the left and right subtree has a zero path length, if the right subtree is longer than the left subtree, the left and right subtree is exchanged, and the node zero PATH value is changed to the right subtree + 1. Under the core code:

Struct heapnode {int data; int zeropath; heapnode * left; heapnode * right; heapnode () {} heapnode (INT key, int zero = 0, heapnode * l = NULL, heapnode * r = NULL): Data (key), zeropath (zero), left (L), right (r) {}}; int getzeropath (heapnode * root) {// get the zero path length return root = NULL? -1: Root-> zeropath;} void setzeropath (heapnode * root) {// set the zero path length // for left-side heap, you can directly root-> zeropath = getzeropath (root-> right) + 1, however, the left and right Subtrees must be compared for the long time of the Oblique heap // zero path. For code reuse, the root-> zeropath = min (getzeropath (root-> left) is not distinguished ), getzeropath (root-> right) + 1;} void swap (heapnode * & T1, heapnode * & T2) {// switch left and right subtree heapnode * t = T1; t1 = t2; t2 = T;} heapnode * mergeheap (heapnode * R1, heapnode * R2) {// heap merge algorithm if (r1 = NULL) return R2; if (R2 = NULL) return R1; If (R1-> DATA> R2-> data) {R2-> right = mergeheap (R1, R2-> right ); if (getzeropath (R2-> left) <getzeropath (R2-> right) {swap (R2-> left, R2-> right);} setzeropath (R2 ); return R2;} else {R1-> right = mergeheap (R1-> right, R2); If (getzeropath (R1-> left) <getzeropath (R1-> right )) swap (R1-> left, R1-> right); setzeropath (R1); Return R1 ;}}

2.2 oblique heap: The oblique heap is similar to the left heap. The only difference is that the left and right subtree must be exchanged regardless of the left and right subtree's zero path size after merging. Different codes are as follows:


Heapnode * mergeheap (heapnode * R1, heapnode * R2) {If (r1 = NULL) return R2; If (R2 = NULL) return R1; if (R1-> DATA> R2-> data) {R2-> right = mergeheap (R1, R2-> right); swap (R2-> left, r2-> right); // setzeropath (R2); Return R2;} else {R1-> right = mergeheap (R1-> right, R2 ); swap (R1-> left, R1-> right); // setzeropath (R1); Return R1 ;}}

2.3. Two-way heap: the two-way heap is actually a forest. Using an array or linked list to store the root nodes of all binary trees can be ordered. The merging method is a little different from that of the left heap, if it is the smallest heap (opposite to the largest heap), the binary tree with the same degree will be merged during the merge, and the heap with a large top value will be used as the Child tree of the heap with a small top value, A tree of the same degree is not allowed in the two heap.

  • The second tree with a degree of 0 contains only one node.

  • A k-Level Two-term tree has a root node with 650 under the root node.) This. width = 650; "class =" MWe-math-Fallback-PNG-inline Tex "alt =" K "src =" http://upload.wikimedia.org/math/8/c/e/8ce4b16b22b58894aa86c421e8759df3.png "style =" border: none; Vertical-align: middle; "/> children, each of which has a degree of 650) This. width = 650; "class =" MWe-math-Fallback-PNG-inline Tex "alt =" K-1, K-2 ,..., 2, 1, 0 "src =" http://upload.wikimedia.org/math/5/6/3/5634b9392658c284f7536bd65c2b0537.png "style =" border: none; Vertical-align: middle; "/> the root of the Two-item tree

  • The number of two trees with a degree of K is 650) This. width = 650; "class =" MWe-math-Fallback-PNG-inline Tex "alt =" 2 ^ K "src =" http://upload.wikimedia.org/math/ B /3/4/b340d3e11a01b97cc9a572c939977fa5.png "style =" border: none; vertical-align: middle; margin: 0px; "/> nodes with a height of 650) This. width = 650; "class =" MWe-math-Fallback-PNG-inline Tex "alt =" K "src =" http://upload.wikimedia.org/math/8/c/e/8ce4b16b22b58894aa86c421e8759df3.png "style =" border: none; V Ertical-align: middle; margin: 0px; "/>. At depth D, There are 650) This. width = 650; "class =" MWe-math-Fallback-PNG-inline Tex "alt =" \ tbinom n d "src =" http://upload.wikimedia.org/math/ B /6/e/b6ea99a5a2f2e5ac8e231eb5d533e71c.png "style =" border: none; vertical-align: middle; margin: 0px; "/> (binary coefficient) nodes.

  • The level of K of the two trees can easily be obtained from the two degrees of K-1 of the two trees: take the second tree with a degree of K-1 as the leftmost subtree of the second tree with an original degree of K-1. This nature is the basis of two heaps for heap merge.

This article is from the "algorithm" blog, please be sure to keep this source http://363883844.blog.51cto.com/2977542/1544700

Comparison of several heap structures

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.