Minimum heap and its operation function

Source: Internet
Author: User

A few days ago in doing kth largest Element in an Array when used to the heap, through the problem also learned the heap of make_heap,push_heap,pop_heap operation, see the C + + reference explanation also understand HEAP_ Sort what's going on. So think of yourself to achieve the following four functions.
Definition of heap:

    • Any node is smaller than all descendants of it, and the minimum element is on the root of the heap (heap order).
    • Heap is always a complete tree.
#include <iostream>#include <string>using namespace Std;//To construct a minimal heap, the smallest heap is any one of the nodes in the left and right subtree of values that are smaller than the value of the node void Downhelper (int *arr,int POS,int length); void Uphelper (int *arr,int POS);//Construct heap://The node containing only one element must be a heap//then from the array (length-2)/2The nodes begin to traverse the array in reverse order, and each node is lowered by void make_heap (int *arr,int length){ for(intI= (length-2)/2; i>=0; i--) {Downhelper (arr,i,length); }}//inserts an element into the heap, meaning that itlength-1Elements have already formed the heap, and now at the end of adding an element//re-forming the heap, then only the last element will need to be raised to void Push_heap (int *arr,int length) {Uphelper (arr),length-1);} Popping elements from the heap, the heap can only eject the elements of the heap top. For the heap, its popup operation is carried out in two steps://1: Swaps the top element of the heap and the last element of the pair//2: Performs a downward operation on the element at the top of a new heap//The length of the heap is reduced after this pop-up operation1, and the popup element is placed in the last void Pop_heap (int *arr,int length) {Swap (arr[0],arr[length-1]); Downhelper (arr,0,length-1);} To increase the operation, thePOSThe position of the element is raised, this operation will be used in PUSH_HEAP//until the parent node of the node is larger than the value of the node to jump out of the loop void Uphelper (int *arr,int POS){ while(POS!=0&&arr[POS]>arr[(POS-1)/2]) {Swap (arr[POS],arr[(POS-1)/2]);POS=(POS-1)/2; }}//the operation, thePOSThe position of the element is lowered until the end of the heap, when the length of the heap islengthThe maximum subscript for a//heap islength-1void Downhelper (int *arr,int POS,int length){//If the left dial hand node exists and the value of the left child node is greater than the current node while(POS * *+1<length&&arr[POS * *+1]>arr[POS])    {//If the right child node exists and the value of the right child node is greater than the left child node, the value of the current node and the right child node are exchangedif(POS * *+2<length&&arr[POS * *+2]>arr[POS+1]) {Swap (arr[POS],arr[POS * *+2]);POS=POS * *+2; }ElseOtherwise, and left child node swap {swap (arr[POS],arr[POS * *+1]);POS=POS * *+1; The}}//heap sort is to constantly pop an element out of the heap, since each popup element is the largest in the heap,//And the heap size is reduced after each popup element.1, the element at the top of the heap is placed at the end of the array//So the array becomes orderly after the pop-up operation is performed. void Sort_heap (int *arr,int length) {make_heap (arr),length); for(intI=0;i<length-1; i++) {pop_heap (arr),length-I.); }}intMain () {inta[]={Ten, -, -,5, the}; Make_heap (A,5); cout<<a[0]<<endl; Pop_heap (A,5); cout<<a[0]<<endl; Push_heap (A,5); cout<<a[0]<<endl; Sort_heap (A,5); cout<<a[0]<<" "<<a[1]<<" "<<a[2]<<" "<<a[3]<<" "<<a[4]<<endl;return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Minimum heap and its operation function

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.