最小堆问题

来源:百度知道 编辑:UC知道 时间:2024/05/09 07:40:16
最小堆的类声明!!

template <class T>

class MinHeap
{
private:
T *heapArray;
int CurrentSize;
int MaxSize;
void swap(int pos_x, int pos_y);
void BuildHeap();

public:
MinHeap(const int n);
virtual ~MinHeap() {delete [] heapArray;};

bool isLeaf(int pos) const {return (pos >= CurrentSize/2) && (pos < CurrentSize);};
int leftchild(int pos) const {return pos*2 + 1;};
int rightchild(int pos) const {return pos*2 + 2;};
int parent(int pos) const {return (pos - 1) / 2;};

bool Remove(int pos, T&node);
bool Insert(const T&newNode);
T &RemoveMin();

void SiftUp(int position);
void SiftDown(int left);

void DisplayHeap();
};

template <class T>
MinHeap<T>::MinHeap(const int n)
{
if (n <= 0)
return;

CurrentSize = 0;
MaxSize = n;
heapArray = new T[MaxSize];
BuildHeap();
}

te