c++实现的最小堆类

今天复习最小堆,用c++写了一个最小堆的类,贴出来分享给大家。

#include <iostream>

using namespace std;

template <class T>
class MinHeap{
     public:
         void HeapInitialize(T a[],int size,int ArraySize);
        MinHeap<T> &Insert(const T &x);
        MinHeap<T> &DeleteMin(T &x);
        void Print();
        bool IsEmpty();
        int Size();
    private:
          T *Heap;
          int CurrentSize;
          int MaxSize;
};

template <class T>
void MinHeap<T>::HeapInitialize(T a[],int size,int ArraySize)
{
     Heap = (T*)malloc(ArraySize*sizeof(T));

     for(int m=0;m<size;m++)
         Heap[m] = a[m];

     CurrentSize = size;
     MaxSize = ArraySize;
     for(int i = (CurrentSize-1)/2;i>=0;i--)
     {
         T y = Heap[i];
         int c = 2*i;
         while(c<CurrentSize)
         {
             if(c<CurrentSize-1&&Heap[c]>Heap[c+1])
                 c++;
             if(y<=Heap[c])
                 break;
             else
             {
                 Heap[c/2] = Heap[c];
                 c*=2;
              }
         }
         Heap[c/2] = y;
     }
}
template <class T>
MinHeap<T> &MinHeap<T>::Insert(const T &x)
{
     if(CurrentSize==MaxSize)
         exit(1);
     int i = CurrentSize++;
     while(i!=0 && x<Heap[i/2])
     {
         Heap[i] = Heap[i/2];
         i /= 2;
     }
     Heap[i] = x;
     return *this;
}
template <class T>
MinHeap<T> &MinHeap<T>::DeleteMin(T &x)
{
     if(CurrentSize==0)
         exit(1);
     x = Heap[0];
     T y = Heap[--CurrentSize];
     int i = 0,ci = 1;
     while(ci<CurrentSize)
     {
         if(ci<CurrentSize-1 && Heap[ci]>Heap[ci+1])
             ci++;
         if(Heap[ci]<y)
         {
             Heap[i] = Heap[ci];
             i = ci;
             ci *= 2;
           }
           else
             break;
     }
     Heap[i] = y;
     return *this;
}

template <class T>
void MinHeap<T>::Print()
{
     for(int i=0;i<CurrentSize;i++)
         cout<<Heap[i]<<" ";
     cout<<endl;
}


template <class T>
bool MinHeap<T>::IsEmpty()
{
     if(CurrentSize>0)
         return false;
     else
         return true;
}


template <class T>
int MinHeap<T>::Size()
{
     return CurrentSize;
}


void main()
{
     int arr[] = {2,1,5,3,2,4,6,34,3};
     MinHeap<int> *mh = new MinHeap<int>();
     mh->HeapInitialize(arr,9,20);
     cout<<"Original heap:";
     mh->Print();
     int x;
     mh->Insert(7);
     mh->Insert(11);
     mh->Insert(23);
     mh->Insert(10);
     mh->Insert(11);
     cout<<"After inserting:";
     mh->Print();

     cout<<"Extract from heap:";
     while(!mh->IsEmpty())
     {
         mh->DeleteMin(x);
         cout<<x<<" ";
     }
     cout<<endl;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值