"Algorithm" heap row, C + + implementation

Source: Internet
Author: User

Constructs a binary tree in the order of elements in an array, forming a (binary) heap. (binary tree is virtual, not really build two-fork tree)

The array a representing the heap has two important properties: A.heapsize, which indicates how many elements are in the heap, and how many elements are in the heap.

A.length, representing the length of the array

For example, array a= {1,2,3,4,5,6,7,8,9,10}, at which point a.heapsize = A.length. This binary tree is perfect except for the last layer.

Maximum heap: The value of the parent node is always not less than the value of the child node, the reaction in the array is a[i]>=a[2i+1] && a[i]>=a[2i+2], if 2i+2<a.size

Minimum heap: The value of the parent node is always not greater than the child node value, the reaction in the array is a[i]<=a[2i+1] && a[i]<=a[2i+2], if 2i+2<a.size, is a minimum heap

Heap rows are generally used for the maximum heap, output ascending array, and minimum heap for priority queues.

The time complexity of the heap is O (NLGN)

I'm writing the minimum heap sort, output descending array

//=================== Minimum heap maintenance ==================//Adjust the subtree order of the root node to arr[i], placing the smallest node at the root node location//recursive adjustment of displaced child nodesvoidMinheapfy (vector<int> &arr,intIintheapsize) {    intleft =2* i +1;//for Arr[i], it's left child is arr[2i+1] right child is arr[2i+2]    intright =2* i +2; intleast =i; if(Left < heapsize && Arr[left] <Arr[least]) least=Left ; if(Right < HeapSize && Arr[right] <Arr[least]) {Least=Right ; }    if(Least! =i) {        inttemp =Arr[i]; Arr[i]=Arr[least]; Arr[least]=temp;  Minheapfy (arr, least, heapsize); //If the subtree changes, recursively adjusts the changed child nodes    }}//=============== establishing minimum heap ================//using Minheapfy to adjust each subtrees tree, the elements of arr ARR[ARRSIZE/2:] are leaf nodesvoidBuildminheap (vector<int> &arr) {    intHeapSize =arr.size ();  for(inti = heapsize/2-1; I >=0; i--) {minheapfy (arr, I, heapsize); }}//================ output Heap row results ====================//Since the root node is always the smallest of the whole heap, each displacement of the root node and the last node//minimum heap creation for the array before being replacedvoidHeapsort (vector<int> &arr) {    intHeapSize =arr.size ();    Buildminheap (arr);  for(inti = heapsize-1; I >=0; i--){        inttemp = arr[0];//displace the root node element and the last element of the current arrayarr[0] =Arr[i]; Arr[i]=temp; HeapSize-=1; Minheapfy (arr,0, heapsize);//The minimum heap is established for the node before the ordered element    }}int_tmain (intARGC, _tchar*argv[]) {Vector<int> primes = {4,1,3,2, -,9,Ten, -,8,7 };    Heapsort (primes);  for(inti =0; I < primes.size (); i++) {cout<< Primes[i] <<Endl; } System ("Pause"); return 0;}

"Algorithm" heap row, C + + implementation

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.