二叉堆(最小堆)(数据结构与算法分析的代码实现)

“堆是一棵被完全填满的二叉树,可能的例外是在底层,底层上的元素从左到右填入。这样的树称为完全二叉树”

“因为完全二叉树很有规律,所以可以用一个数组表示而不需要使用链”

上面两句是摘自《数据结构与算法分析》

书中代码的上滤和下滤的实现比算法导论的好,算法导论通过递归,每一次都交换不合适的节点,书本的代码则是通过循环找到节点正在要移动到的位置。

参考算法导论的代码:http://www.cnblogs.com/alan-forever/archive/2012/09/26/2704860.html

算法导论的是堆排序的代码,不过堆的性质都有的。

下面的代码是最小堆的,而且是一个类模板。

View Code
  1 #include<iostream>
  2 #include<vector>
  3 using namespace std;
  4 
  5 template <typename Comparable>
  6 class BinaryHeap
  7 {
  8 public:
  9     explicit BinaryHeap( int capacity = 100 ) : array( capacity + 10 )
 10     {
 11         currentSize = 0;
 12     }
 13 
 14     explicit BinaryHeap( const vector<Comparable> & item ) : array( item.size() + 10 ), currentSize( item.size() )
 15     {
 16         for(int i = 0; i < item.size(); ++i)
 17             array[i+1] = item[i];
 18         buildHeap();
 19     }
 20 
 21     bool isEmpty() const
 22     {
 23         return currentSize == 0;
 24     }
 25 
 26     const Comparable & finMin( ) const
 27     {
 28         return array[1];
 29     }
 30 
 31     void insert( const Comparable & x)
 32     {
 33         if(currentSize == array.size() - 1)
 34             array.resize( array.size() * 2);
 35 
 36         int hole = ++currentSize;
 37         for(; hole > 1 && x < array[hole / 2]; hole /= 2)
 38             array[hole] = array[hole / 2];
 39         array[hole] = x;
 40     }
 41 
 42     void deleteMin()
 43     {
 44         array[1] = array[currentSize--];
 45         percolateDowm( 1 );
 46     }
 47 
 48     void deleteMin(Comparable & Min)
 49     {
 50         Min = array[1];
 51         deleteMin();
 52     }
 53 
 54     void makeEmpty()
 55     {
 56         delete array[currentSize];
 57         currentSize = 0;
 58     }
 59 
 60     void print( )
 61     {
 62         for(int i = 1; i <= currentSize; ++i)
 63             cout << array[i] << " ";
 64         cout << endl;
 65     }
 66 
 67 private:
 68     int currentSize;
 69     vector<Comparable> array;
 70 
 71     void buildHeap()
 72     {
 73         for(int i = currentSize / 2; i > 0; i--)
 74             percolateDowm( i );
 75     }
 76 
 77     void percolateDowm( int hole )
 78     {
 79         int child;
 80         Comparable temp = array[hole];
 81 
 82         for(; hole * 2 <= currentSize; hole = child)
 83         {
 84             child = hole * 2;
 85             if(child != currentSize && array[child + 1] < array[child])
 86                 child++;
 87             if(array[child] < temp)
 88                 array[hole] = array[child];
 89             else
 90                 break;
 91         }
 92         array[hole] = temp;
 93     }
 94 };
 95 
 96 int main()
 97 {
 98     vector<int> a;
 99     int m;
100     for(int i = 0; i < 10; ++i)
101     {
102         cin >> m;
103         a.push_back( m );
104     }
105     BinaryHeap<int> minHeap( a );
106     cin >> m;
107     minHeap.insert( m );
108     minHeap.deleteMin();
109     minHeap.print();
110     return 0;
111 }

 

转载于:https://www.cnblogs.com/alan-forever/archive/2012/12/11/2813417.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值