代码:
#include "iostream" using namespace std; int heap_size; int left(int i){ return 2*i; } int right(int i){ return 2*i+1; } void max_Heapify(int A[],int i){ int l,r,largest; l=left(i); r=right(i); int temp; if(l<=heap_size&&A[i]<A[l]) largest=l; else largest=i; if(r<=heap_size&&A[largest]<A[r]) largest=r; if(largest!=i){ temp=A[i]; A[i]=A[largest]; A[largest]=temp; max_Heapify(A,largest); } } void build_MaxHeap(int A[]){ heap_size=5; for(int i=heap_size/2;i>=1;i--){ max_Heapify(A,i); } } void heap_Delete(int A[],int i){ A[i]=A[heap_size]; heap_size--; max_Heapify(A,i); } void main(){ int A[20]={-1,12,2,43,23,3}; cout<<A[2]<<endl; build_MaxHeap(A); cout<<A[2]<<endl; heap_Delete(A,2); cout<<A[2]<<endl; getchar(); getchar(); }