Heap and heap sequencing

Source: Internet
Author: User

Heap sort and quick sort, merge sort are the common sort methods of time complexity O (N*LOGN).

The heap sort is an in-place sort and the secondary space is O (1).

It is an unstable sort method. (The stability of a sort is that if there are two elements of the same order in the sorted sequence, their relative positions are not changed before and after sorting)

First, what is a heap, a heap is usually an array object that can be seen as a tree. The following properties are met:

1. The value of a node in the heap is always not greater than or less than the value of its parent node;

2. The heap is always a complete tree (the complete tree is the tree in which the leaf nodes appear only on the two largest layers of the hierarchy).

The largest heap of the root node is called the maximum heap or large heap, and the smallest heap of the root node is called the minimum heap or small Gan. The common heap has two forks, Fibonacci heaps, and so on. Since several other heaps (two-item heap, Fibonacci Ponachi, etc.) are used less often, the two-fork heap is referred to as a heap.

Storage of Heaps

The heap is usually represented by an array, and the parent node of the I node is labeled (i–1)/2. The index of the left and right sub-nodes is 2 * i + 1 and 2 * i + 2 respectively. such as the No. 0 node of the left and right sub-nodes subscript 1 and 2 respectively.

?

Operation of the Heap

Build heap:

In general, the tree does not meet the conditions of the heap, by rearranging the elements, you can create a "heap" of trees. such as the initial table: 55 12 16, after the heap is: 12 55 16.

Insert of Heap:

Each insertion is a new data placed at the end of the array. The tree is then updated to restore the heap order. such as the initial table: 12 22 7, after inserting new data, the array is 12 22 7 16, then the order of the tree is re-ordered, the array is 12 16 7 22.

It can be found that the parent node from this new data to the root node is necessarily an ordered sequence.

// 新加入i结点 其父结点为(i - 1) / 2void MinHeapFixup(int a[], int i){ int j, temp;temp = a[i];j = (i - 1) / 2; //父结点while (j >= 0 && i != 0){if (a[j] <= temp)break;a[i] = a[j]; //把较大的子结点往下移动,替换它的子结点i = j;j = (i - 1) / 2;}a[i] = temp;}
//在最小堆中加入新的数据nNumvoid MinHeapAddNumber(int a[], int n, int nNum){<span style="white-space:pre"></span>a[n] = nNum;<span style="white-space:pre"></span>MinHeapFixup(a, n);}


Removal of heaps

Only the No. 0 data can be deleted at a time in the heap. To facilitate rebuilding the heap, the actual operation is to assign the value of the last data to the root node, and then start from the root node with a top-down adjustment. Adjust the first in the left and right son node to find the smallest, if the parent node than the smallest sub-node is also small to indicate that no adjustment is needed, the parent node and it will be exchanged and then consider the subsequent nodes. Equivalent to the "sinking" process of a data from the root node.

such as the initial table: 12 16 50 22, after the deletion of the No. 0 data, the array is 22 16 50 _, and then rearrange the order of the tree, the array is 16 22 50.

Starting from the I-node adjustment, n for the total number of nodes starting from 0 compute the child node of the I node is 2*i+1, 2*i+2voidMinheapfixdown(int a[],int I,int n) {Int J, temp;temp = A[i];j =2 * i +1;while (J < N) {if (j + 1 < N & & A[j + 1] < A[j]) //find the smallest J++;if (a[j] >= temp) break;a[i] = a[j]; //move the smaller sub-node up, replacing its parent node i = j;j = 2 * i +  1;} A[i] = temp;} //Remove the number void minheapdeletenumberint a[], int N) {Swap (A[0], A[n-1]); Minheapfixdown (A, 0, N-1);}      

Stacking arrays

about how to make a heap of data. Maybe a lot of people would like to have one of the data from the array to build the heap? No.

For example, int a[0] = {8,11,16,29,49,19,59,64,3,18};

If you think of this array as a tree, then its leaf node 19,59,64,3,18 is a legitimate heap. Just start the 49 downward adjustment. Then take the 29,16,11,9 node to make a downward adjustment operation.

 //establish minimum heap  void makeminheap (int a[], int N) {for (int i = n/2-1; I >= 0; i--) Minheapfixdown (A, I, n);}  


In this way, the heap operations are all done.

Said so much, finally to the protagonist debut.

Depending on the nature of the heap, the heap is built well. The No. 0 data in a heap is the smallest data in the heap. Remove this data and perform the next heap delete operation. So the No. 0 data in the heap is the smallest data in the heap, repeating the above steps until there is only one data in the heap to take out the data directly.

Since the heap is also modeled with an array, the first time the array is stacked, a[0] and a[n-1] are exchanged, and then the heap is restored to a[0...n-2. Swap a[0] with A[n–2] for the second time, then redo the heap for a[0...n-3], repeating such operations until a[0] and a[1] are exchanged. Because each time the smallest data is incorporated into the subsequent ordered interval, the entire array is ordered after the operation is completed.

void MinheapsortTodescendarray(int a[], int n){for (int i = n - 1; i >= 1; i--){Swap(a[i], a[0]);MinHeapFixdown(a, 0, i);}}

Note that using the minimum heap sort is a descending array, and you can use the maximum heap to get an incremented array.

Application:

Heap is a classic data structure, the time complexity of inserting and deleting elements into a heap is O (LgN), N is the number of elements in the heap, and the complexity of obtaining the minimum key value (Keng Gen) is O (1).

Libevent Timing Event Management is to use a time as key to the small Gan structure to do, give up the original red and black tree, probably is heap than red and black tree simple bar.

Reference:

http://blog.csdn.net/morewindows/article/details/6709644

Heap and heap sequencing

Heap and heap sequencing

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.