1. First of all, in this test I use an array to represent a heap, and the starting point of each heap is from the index 1 of the array
Large bank
#include <iostream> #include <algorithm> using namespace std; int b[100]; int c[100]; void shiftUp(int a[],int k) //Insert n elements into an empty heap one by one, time complexity nlgN { b[k+1]=a[k]; k=k+1;//The b array here starts from 1, which can exactly correspond to the left child is twice the root node, and the right child is 2*k+1 of the root node while(k>1&&b[k]>b[k/2]){ swap(b[k],b[k/2]); k=k/2; } } void shiftDown(int k,int sum) { b[k]=b[sum];//This method is to continuously put the value of the leaf on the root node sum--; while(2*k<=sum) //First judge whether there is a child, if there is a left child, it definitely means that it is not a single seedling { int j=2*k; // if(j+1<=sum&&b[j]<b[j+1]) j=j+1; //Determine whether there is a right child, determine which child has a larger value, and exchange it with that if(b[k]>b[j]) break; //If the value of the parent node is large, then end swap(b[k],b[j]); k=j; } } void shiftDownArray(int k,int sum) //The time complexity is O(n) starting from a non-leaf node { while(2*k<=sum) //First judge whether there is a child, if there is a left child, it definitely means that it is not a single seedling { int j=2*k; // if(j+1<=sum&&c[j]<c[j+1]) j=j+1; //Determine whether there is a right child, determine which child has a larger value, and exchange it with that if(c[k]>c[j]) break; //If the value of the parent node is large, then end swap(c[k],c[j]); k=j; } } intmain() { int a[]={2,3,1,5,8};//A simple array of test cases for(int i=0;i<5;i++) { shiftUp(a,i); // Insert elements into the heap one by one here to select the largest value cout<<a[i]<<" "; } cout<<b[1]<<endl; for(int i=0;i<5;i++) { cout<<b[i+1]<<" ";//Output result test } cout<<endl; int sum = 5; while(sum>0){ cout<<b[1]<<"->"<<endl; shiftDown(1,sum); //Here is to continuously take out the maximum value of the heap, and then rebuild the heap sum--; } int m=5/2; //The first non-leaf node of the constructed heap, for(int i=0;i<5;i++) { c[i+1]=a[i]; } //The first non-leaf node (including the root node) from bottom to top, perform the shiftDown operation for(int i=m;i>=1;i--){ shiftDownArray(i,5); //Check whether the node is larger than the child node, if not, then debug and convert } cout<<c[1]<<endl; for(int i=1;i<6;i++) { cout<<c[i]<<" "; } }
2. An array represents a heap, the array starts from subscript 0, and by the way, it is sorted from small to large
Then the left child is 2*i+1
The right child is 2*i+2
#include <iostream> #include <algorithm> using namespace std; void shiftDownArray(int a[],int k,int sum) //The time complexity is O(n) starting from non-leaf nodes { while(2*k+1<sum) //First judge whether there is a child, if there is a left child, it definitely means that it is not a single seedling { int j=2*k+1; // if(j+1<sum&&a[j]<a[j+1]) j=j+1; //Determine whether there is a right child, determine which child has a larger value, and exchange it with that if(a[k]>a[j]) break; //If the value of the parent node is large, then end swap(a[k],a[j]); k=j; } } intmain() { int a[]={2,3,1,5,8}; int n=5; for(int i=(n-1)/2;i>=0;i--) { shiftDownArray(a,i,n); //Build a large top heap first, this is built with the first non-leaf node } for(int i=0;i<n;i++){ cout<<a[i]<<" "; } cout<<endl; for(int i=n-1;i>0;i--){ //Then stack the top to the end of the array swap(a[0],a[i]); for(int j=0;j<n;j++){ cout<<a[j]<<" ###"; } cout<<endl; shiftDownArray(a,0,i); // Rebuild the big top heap again, but the length of the array is reduced by 1, this is to replace the top of the heap with the last one, and rebuild the heap for(int j=0;j<n;j++){ cout<<a[j]<<" ***"; } cout<<endl; } }