C++语言maxheap.h头文件
模板& ltT类,E类& gt
结构元素{
t键;
字段其他数据;
元素& ltt,E & gt& ampoperator =(Element & lt;t,E & gt& ampx)
{ key = x.keyoperator = x.otherdata}
布尔运算符& gt=(Element & lt;t,E & gt& ampx){return键& gt= x.key}
布尔运算符& lt(Element & ltt,E & gt& ampx){return键& ltx.key}
};
模板& ltT类,E类& gt
类别最大堆{
公共:
max heap(int SZ = DefaultSize);
max heap(Element & lt;t,E & gtarr[],int n);
~ max heap(){ delete[]heap;}
int Insert(const元素& ltt,E & gt& ampx);
int Remove(Element & lt;t,E & gt& ampx);
bool IsEmpty()const { return currentSize = = 0;}
bool is full()const { return currentSIze = = MaxHeapSize;}
私人:
元素& ltt,E & gt*堆;
int currentSize
int MaxHeapSize
void siftDown(int start,int m);
void sift up(int start);
Swap(const int i,const int j)
{ Element & ltt,E & gttmp = heap[I];heap[I]= heap[j];heap[j]= tmp;}
};
模板& ltT类,E类& gt
void MaxHeap & ltt,E & gt* sift down(const int start,const int m{
int i = startint j = 2 * I+1;
元素& ltt,E & gttemp = heap[I];
而(我& lt=m){
if(j & lt;m & amp& ampheap[j]& lt;heap[j+1])j++;
if(temp & gt;= heap[j])break;
否则{
heap[I]= heap[j];
I = j;j = 2 * j+1;
}
}
heap[I]= temp;
};