代码如下
#include
using namespace std;
template
class MinHeap
{
private :
T *heapArray;
int CurrentSize;
int MaxSize;
public :
MinHeap();
MinHeap(int n);
void Build();
void SiftDown(int i);
T RemoveMin();
bool insert(T newNode);
T top(){return heapArray[0];}
void print();
};
template
MinHeap
::MinHeap()
{
CurrentSize=0;
MaxSize=10;
heapArray=new T[MaxSize];
}
template
MinHeap
::MinHeap(int n) { CurrentSize=0; MaxSize=n; heapArray=new T[MaxSize]; } template
void MinHeap
::Build() { for(int i=CurrentSize/2-1;i>=0;i--) SiftDown(i); } template
void MinHeap
::SiftDown(int left) { int i=left; int j=2*i+1; T temp=heapArray[i]; while(j
heapArray[j+1])) j++; if(temp>heapArray[j]) { heapArray[i]=heapArray[j]; i=j; j=2*j+1; } else break; } heapArray[i]=temp; } template
T MinHeap
::RemoveMin() { if(CurrentSize==0) { cout<<"Can not delete"<
1) Build(); return temp; } } template
bool MinHeap
::insert(T newNode) { if(CurrentSize==MaxSize+1) { cout<<"full"<
void MinHeap
::print() { for(int i=0;i