手写堆_C++


  一般主程序中拿堆顶元素

1     x=h[1];
2     h[1]=h[top--];
3     down(1);

  在堆尾加入元素

1     h[++top]=x;
2     up(top);

  上浮下沉操作

 1 inline void up(int x)
 2 {
 3     if (x==1) return;
 4     if (h[x>>1]>h[x])
 5     {
 6         swap(h[x>>1],h[x]);
 7         up(x>>1);
 8     }
 9 }
10 inline void down(int x)
11 {
12     x<<=1;
13     if (x>top) return;
14     if (x1];
15     if (h[x>>1]>h[x])
16     {
17         swap(h[x>>1],h[x]);
18         down(x);
19     }
20 }

版权所有,转载请联系作者,违者必究

联系方式:http://www.cnblogs.com/hadilo/p/5932395.html