堆的基本操作

来源:互联网 发布:qq有linux版本吗 编辑:程序博客网 时间:2024/04/29 04:38
void shiftdown(int i, int n){int k, t;t=heap[i]; k=2*i;while(k<=n){if((k<n)&&(heap[k]>heap[k+1]))k++;if(t>heap[k]){heap[i]=heap[k];i=k;k=2*i;}else break;}heap[i]=t;}void del(){heap[1]=heap[n--];if(n>0)shiftdown(1, n);}void shiftup(int x){int t=heap[x], k=x/2;while(k){if(t<heap[k]){heap[x]=heap[k]; x=k; k=k/2;}else break;}heap[x]=t;}void insert(int x){heap[++n]=x;shiftup(n);}

0 0
原创粉丝点击