Knowing that the heap starts with a heap sort of
The two fork heap is either a complete binary tree or an approximate complete binary tree, the heap is stored in the array:
When the root node is labeled 0 o'clock, the sub-node subscript of the element with the subscript n is 2*n+1,2*n+2, and its parent node is labeled (n-1)/2.
two properties of the fork heap:
1. The key value of the parent node is always >= (<=) The key value of any one of the sub-nodes
2. The left and right sub-trees of each node are two fork piles.
The
maximum heap when the parent node's key value is always greater than or equal to the key value of any one of the child nodes.
The minimum heap when the key value of the parent node is always less than or equal to the key value of any one of the child nodes
The operation method of the heap is defined as follows:
#ifndef _heap_ctl_h#define _heap_ctl_h#ifdef __cplusplusextern "C" {#endifvoid heappprint (const char *info, int a[], int n Len), void swap (int *a, int *b), void buildmaxheap (int *a, int hlen), void buildminheap (int *a, int hlen); void Minheapaddnumb ER (int *a, int *nlen, int newnum), void maxheapaddnumber (int *a, int *nlen, int newnum), int minheapdelnumber (int *a, int *n Len), int maxheapdelnumber (int *a, int *nlen), void ascheapsort (int *iarray,int nlen), void descheapsort (int *iarray,int Nlen); #ifdef __cplusplus} #endif #endif
The function is implemented as follows:
#include <stdio.h> #include "heapctl.h" static void __pprint (int a[], int i, int nlen) {printf ("); if (I < Nlen) {p rintf ("%d", A[i]); __pprint (A, i*2+1, Nlen); __pprint (A, i*2+2,nlen);} printf (")");} /** * @brief output heap for the tree tool to graphically display * * @params info written in the previous words * @params a The array to be printed * @params the length of the Nlen array */void heappprint (const char * info, int a[], int nlen) {if (info) printf ("%s", info);p rintf ("\n\\tree"), __pprint (A, 0, Nlen);p rintf ("\ n");} /** * @brief Exchange two data * * @params a The first data to be exchanged * @params B for the second data exchanged */void swap (int *a, int *b) {#if 0//takes advantage of the secondary space tmpint tmp = *a ; *a = *B;*B = tmp; the addition of #endif # if 0//may overflow *a + = *b;*b = *a-*b;*a-= *b; #endif # if 1//xor operation A^b^b=a*a ^= *b;*b ^= *a;*a ^= *b; #en dif}/** * @brief Big Top heap Adjustment * * @params a array A * @params Hlen * @params need to adjust the node I */void maxheapadjust (int *a,int i,int size)//Adjustment heap {int lchild=2*i+1; I's left child node ordinal int rchild=2*i+2; I right child node ordinal int max=i; Temporary variable if (i <= SIZE/2)//If I is a leaf node it is not necessary to adjust {if (lchild<size && A[lchild]>a[max]) {max=lchild; } if (Rchild<size && A[rchild]>a[max]) {max=rchild; } if (max! = i) {swap (&a[i], &a[max]); Maxheapadjust (a,max,size); Avoid resizing a subtree with Max as the parent node is not a heap}}}/** * @brief build a big Top heap * * @params a array A * @params the number of Hlen array elements */void buildmaxheap (int *a, int hlen) {int i;//heap resembles a complete binary tree, Nlen is even: the number of nodes with a depth of 2 N2 = nlen>>1-1, N1 = 1, N0 = Nlen>>1;//nlen is odd, N2 = nlen/2, n1 = 0, N0 = nlen/2+1; N0 = n2 + 1//is adjusted from the non-leaf node maximum ordinal position, the value is SIZE/2 for (i=hlen/2-1; i>=0; i--) {maxheapadjust (A,i,hlen); }}/** * @brief small top heap Adjustment * * @params a array A * @params Hlen * @params need to adjust the node I */void minheapadjust (int *a,int i,int size)//Tune Whole heap {int lchild=2*i+1; I's left child node ordinal int rchild=2*i+2; I right child node ordinal int max=i; Temporary variable if (lchild<size && A[lchild]<a[max]) {max=lchild;} if (rchild<size && A[rchild]<a[max]) {max=rchild;} if (max ! = i) {swap (&a[i], &a[max]); Minheapadjust (A, max, size); Avoid resizing a subtree with Max as the parent node is not a heap}}/** * @brief build a big Top heap * @params a array A * @params the number of Hlen array elements */void buildminheap (int *a, int hlen) {in The T i;//heap resembles a complete binary tree, Nlen is even: the number of nodes with a depth of 2 N2 = nlen>>1-1, N1 = 1, N0 = Nlen>>1;//nlen is odd, N2 = nlen/2, n1 = 0, N0 = nlen/2+1; N0 = n2 + 1//is adjusted from the non-leaf node maximum ordinal position, the value is SIZE/2 for (i=hlen/2-1; i>=0; i--) {minheapadjust (A,i,hlen); }}/** * @brief insert data into the small top heap * * @params an array of data to insert * @params nlen array element length pointer, inserted will increment * @params Newnum inserted element value * * 1, insert data into the end of the array Tail * 2, according to the size of its parent node to adjust the small top heap */void Minheapaddnumber (int *a, int *nlen, int newnum) {A[*nlen] = Newnum; Int J, I = *nlen;for (j = (i-1)/2; (J >= 0 && I! = 0) && A[i] < a[j]; i = j, j = (i-1)/2) swap (&a[i], &a[j]); ++*nlen; }/** * @brief Insert data into the large top heap * * @params an array of data to insert * @params nlen array element length pointer, inserted will increment * @params Newnum inserted element value * * 1, insert data into the end of the array * 2, Root Resize the Big Top heap */void Maxheapaddnumber (int *a, int *nlen, int newnum) {A[*nlen] = new in relation to the size of its parent nodeNum; Int J, I = *nlen;for (j = (i-1)/2; (J >= 0 && I! = 0) && a[i] > A[j]; i = j, j = (i-1)/2) swap (&a[i], &a[j]); ++*nlen; }/** * @brief The small top heap delete operation, the heap can only delete the No. 0 data, * * @params A to delete the data array * @params nlen array element length pointer, after insertion will be reduced by * * 1, the inserted data into the end of the array * 2, according to its parent node of the large Small relationship Adjustment Large top heap */int minheapdelnumber (int *a, int *nlen) {int newlen = *nlen-1;swap (&a[0], &a[newlen]); Minheapadjust (A , 0, Newlen); *nlen = Newlen;return A[newlen];} int maxheapdelnumber (int *a, int *nlen) {int newlen = *nlen-1;swap (&a[0], &a[newlen]); Maxheapadjust (A, 0, NewLen) ; *nlen = Newlen;return A[newlen];} /** * @brief use a large top heap for ascending order * * @params the array name to sort * @params the number of elements in the Nlen array */void ascheapsort (int *a,int nlen) {int I;buildmaxhe AP (A,nlen); for (i=nlen-1; i>=1; i--) {swap (&a[0], &a[i]); Swaps the top of the heap and the last element, that is, each time the largest of the remaining elements is placed to the last face Maxheapadjust (A, 0, i); Resize the heap top node to become a big Top heap}}/** * @brief use a small top heap for descending order * * @params an array name to sort * @params the number of elements in the Nlen array */void descheapsort (int *iarray,in T nlen) {int i;Buildminheap (IArray, Nlen); for (i=nlen-1; i>=1; i--) {swap (&iarray[0], &iarray[i]); Minheapadjust (IArray, 0 , i);}}
The implementation of function and its application please refer to the comments
or the following documents
Http://www.cnblogs.com/dolphin0520/archive/2011/10/06/2199741.html
http://blog.csdn.net/morewindows/article/details/6709644/
Binary Tree Learning heap sequencing