void maxheap(int i) { int left=i*2,right=i*2+1; if(a[left]>a[i]&&left<len) big=left; else big=i; if(a[right]>a[i]&&right<len) big=right; swap(big,i); maxheap(big); } int max_extract() { if(len<1) {cout<<"error"<<endl; return; } int max=a[1]; a [1] = a [len]; maxheap(1); len--; return max; } void increase_heap(int key,int i) { if(key<a[i]) { cout<<"error"<<endl; return; } a[i]=key; while(i>1&&a[i/2]<a[i]) { swap(a[i],a[i/2]); i=i/2; } } void insert_heap(int key) { a [++ len] = INF; increase_heap(key,len); }
Introduction to Algorithms Heap Priority Queues
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=325738868&siteId=291194637
Recommended
Ranking