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); }
算法导论 堆 优先队列
猜你喜欢
转载自blog.csdn.net/lirui7610/article/details/80086676
今日推荐
周排行