Main content:
1. What is a heap?
2. How to build a heap
3. Heap Sequencing
4. Reference Code
One, what is a heap?
"Heap" is a very interesting data structure, is a complete binary tree.
"Heap" attribute: the key value of each node must always be greater than (or less than) its parent node (greater than: called "maximum heap", less than: "Minimum heap"), or each node is always greater than or less than its child nodes.
For the maximum heap, the root node is the maximum, and for the smallest heap, the root node is the minimum value.
Usually the "heap" is implemented by an array, so that the elements of the specified index can be quickly positioned using the array's characteristics.
Therefore, when we know that a node is indexed to I, it can be easily located to its child nodes and parent nodes through the following definition of "heap".
In the heap with a starting index of 0:
1) The root node of the heap will be stored in position 0
2) node i's left child node in position 2 * i + 1
3) node I right child node in position 2 * i + 2
4) Node I parent node in position floor ((i-1)/2): Note floor means "rounding" action
In the heap with a starting index of 1:
1) The root node of the heap will be stored in position 1
2) node i's left child node at position 2 * I
3) node I right child node in position 2 * i + 1
4) Node I parent node in position floor (I/2): note floor means "rounding" action
Second, how to build a heap?
According to the above definition of the heap, build a heap, first need to build a complete binary tree, the tree node needs to satisfy: Each node value is not less than or not smaller than the value of its parent node, that is A[parent[i]] >=/<= a[i].
Given an unordered array, how do you build a maximum heap?
First, the unordered array is constructed as a complete binary tree, and then the binary tree is adjusted according to the conditions that each node needs to meet.
Build a maximum heap, and the core of the heap is to adjust the heap so that the two fork tree satisfies the definition of the heap (the value of each node is not greater than the value of its parent node).
The process of the heap transfer should start with the last non-leaf node (the parent node of the last node) , assuming that there is an array a = {1, 3, 4, 5, 7, 2, 6, 8, 0}.
So the process of the heap, such as array subscript starting from 0, a[3] = 5, respectively, with the left child and the right child compared to the size, if the a[3] maximum, then do not adjust, or the value of the child is the largest exchange position, in Figure 1 is a[7] > A[3] > A[8], so a[3] and a[ 7] Swap, go from Figure 1.1 to figure 1.2.
?
So the process of building the heap is
for (i = headlen/2-1; I >= 0; i++)? Do Adjustheap (A, Heaplen, i)
Heap Transfer:
If the initial array is in a non-descending order, then the heap is not needed, and the definition of the heap is satisfied directly, which is the best case, with a run time of θ (1);
If the initial array is 1.5, only a[0] = 1 does not satisfy the definition of the heap, the comparison with the child nodes is adjusted to figure 1.6, but figure 1.6 still does not meet the definition of the heap, so it is recursive, until the definition of the heap is satisfied or until the bottom of the heap.
If the heap is recursive to the end of the heap, then it is the worst case, the run time is O (h) (H is the height of the node to be adjusted, the base height is 0, and the heap top height is floor (logn)).
Time complexity of the heap adjustment: O (LOGN)
Time complexity of building a heap: O (n)
Analysis of time complexity: reference http://www.cnblogs.com/zabery/archive/2011/07/26/2117103.html
Third, how to sort the heap?
Depending on the nature of the heap, we can "move Down" from the parent node of the last node of the unordered sequence until the first node of the sequence. This ensures that each node is in the right place and a "heap" is established.
but "heap" is not a fully ordered sequence, because "heap" only guarantees the location of the parent node and child nodes, but does not guarantee the location of the left and right child nodes. So how do we do "heap sorting"?
Since the root node of a "heap" is necessarily the largest element in the entire sequence, we can only get one valid element per "heap" for a sorted sequence. If we take the root node and rearrange the rest of the sequence into a "heap", again and again, we can sequentially get a complete sequencing sequence.
Of course, in order to simplify the operation, each time we just need to exchange the root node with the last node, and then exclude the last location, the root node "Move Down" operation.
Time complexity of heap sequencing: O (NLOGN)
Analysis of time complexity: reference http://www.cnblogs.com/zabery/archive/2011/07/26/2117103.html
After the build heap is complete, heap 1.7 is a large heap.
Swap a[0] = 8 with a[heaplen-1], then Heaplen minus one, 2.1, then Adjustheap (A, heapLen-1, 0), 2.2. So swap the first element of the heap and the last element of the heap, then the heap size Heaplen minus one, the heap is Heaplen heap size for the heap, so loop until Heaplen = = 1 o'clock stop, and finally the result 3.
Four, the code
#include <iostream>using namespace Std;int leftchild (int i) {return 2*i+1;} int rightchild (int i) {return 2*i+2;} Adjust heapvoid makemaxheap (int *a,int len,int i) {int left=leftchild (i); int Right=rightchild (i); int largest=i; int tmp; while (Left<len | | right<len) {if (Left<len && a[largest]<a[left]) largest=left; if (Right<len && a[largest]<a[right]) largest=right; if (largest!=i) {tmp=a[largest]; A[largest]=a[i]; a[i]=tmp; I=largest; Left=leftchild (i); Right=rightchild (i); } else break; }}void makeminheap (int *a,int len,int i) {int left=leftchild (i); int Right=rightchild (i); int smallest=i; int tmp; while (Left<len | | right<len) {if (Left<len && a[smallest]>a[left]) smallest=left; if (Right<len && a[smallest]>a[right]) Smallest=right; if (smallest!=i) {tmp=a[smallest]; A[smallest]=a[i]; a[i]=tmp; I=smallest; Left=leftchild (i); Right=rightchild (i); } else break; }}//build heapvoid buildmaxheap (int* a,int len) {int begin=len/2-1; for (int i=begin;i>=0;i--) makemaxheap (a,len,i);} void Buildminheap (int* a,int len) {int begin=len/2-1; for (int i=begin;i>=0;i--) makeminheap (a,len,i);} Heap sortvoid heapmaxsort (int* a,int len) {int hlen=len; int tmp; Buildmaxheap (A,hlen); while (hlen>0) {tmp=a[hlen-1]; A[HLEN-1]=A[0]; a[0]=tmp; hlen--; Makemaxheap (a,hlen,0); }}void Heapminsort (int* a,int len) {int hlen=len; int tmp; Buildminheap (A,hlen); while (hlen>0) {tmp=a[hlen-1]; A[HLEN-1]=A[0]; a[0]=tmp; hlen--; Makeminheap (a,hlen,0); }}int Main () {int a[]={1,3,4,5,7,2,6,8,0}; int len=sizeof (A)/sizeof (a[0]); Buildmaxheap (A,len); for (int i=0;i<len;i++) cout<<a[i]<< ""; cout<<endl; int b[]={1,3,4,5,7,2,6,8,0}; Buildminheap (B,len); for (int i=0;i<len;i++) cout<<b[i]<< ""; cout<<endl; Heapmaxsort (A,len); for (int i=0;i<len;i++) cout<<a[i]<< ""; cout<<endl; Heapminsort (B,len); for (int i=0;i<len;i++) cout<<b[i]<< ""; cout<<endl; return 0;}V. Articles of reference
Http://www.cnblogs.com/zabery/archive/2011/07/26/2117103.html
http://wxg6203.iteye.com/blog/668968
(algorithm) heap and heap sequencing