#include<iostream> #include<time.h> #include<queue> using namespace std; const int MaxSize=101; int Heap[MaxSize];//size:1..n int heap_size; int Length_heap; void Max_Heapify(int i){ int l=i<<1,r=l+1,largest; if(l<=heap_size&&Heap[l]>Heap[i]) largest=l; else largest=i; if(r<=heap_size&&Heap[r]>Heap[largest]) largest=r; if(largest!=i){ Heap[i]^=Heap[largest]^=Heap[i]^=Heap[largest]; Max_Heapify(largest); } } void Min_Heapify(int i){ int l=i<<1,r=l+1,smallest; if(l<=heap_size&&Heap[l]<Heap[i]) smallest=l; else smallest=i; if(r<=heap_size&&Heap[r]<Heap[smallest]) smallest=r; if(smallest!=i){ Heap[i]^=Heap[smallest]^=Heap[i]^=Heap[smallest]; Min_Heapify(smallest); } } void Build_Min_Heap(){ for(int i=Length_heap>>1;i>=1;i--){ Min_Heapify(i); } } void Build_Max_Heap(){ for(int i=Length_heap>>1;i>=1;i--){ Max_Heapify(i); } } void Heap_deSort(){ for(int i=Length_heap;i>=2;i--){ Heap[i]^=Heap[1]^=Heap[i]^=Heap[1]; heap_size--; Min_Heapify(1); } } void Heap_aSort(){ for(int i=Length_heap;i>=2;i--){ Heap[i]^=Heap[1]^=Heap[i]^=Heap[1]; heap_size--; Max_Heapify(1); } } int Heap_Max(){ return Heap[1]; } int Heap_Min(){ return Heap[1]; } int Heap_Extract_Max(){ if(heap_size<1){ printf("heap underflow/n"); return -1; } int Max=Heap[1]; Heap[1]=Heap[heap_size]; heap_size--; Max_Heapify(1); return Max; } int Heap_Extract_Min(){ if(heap_size<1){ printf("heap underflow/n"); return -1; } int Min=Heap[1]; Heap[1]=Heap[heap_size]; heap_size--; Min_Heapify(1); return Min; } void Heap_Increase_Key(int i, int key){ if(key<Heap[i]){ printf("new key is smaller than current key./n"); return ; } int p=i>>1; while(i>1&&Heap[p]<Heap[i]){ Heap[p]^=Heap[i]^=Heap[p]^=Heap[i]; i=i>>1; p=i>>1; } } void Heap_Decrease_Key(int i,int key){ if(key>Heap[i]){ printf("new key is larger than current key./n"); return ; } int p=i>>1; while(i>1&&Heap[p]>Heap[i]){ Heap[p]^=Heap[i]^=Heap[p]^=Heap[i]; i=i>>1; p=i>>1; } } void Max_Heap_Insert(int key){ heap_size++; Heap[heap_size]=-9999999; Heap_Increase_Key(heap_size,key); } void Min_Heap_Insert(int key){ heap_size++; Heap[heap_size]=9999999; Heap_Decrease_Key(heap_size,key); } int main(){ return 0; }