堆的定义:

最大树(最小树):每个结点的值都大于(小于)或等于其子结点(如果有的话)值的树。
最大堆(最小堆):最大(最小)的完全二叉树。

最大堆的抽象数据结构:

 class MaxHeap
{
private:
T* heapArray; //存放堆数据的数组
int CurrentSize;//当前堆中元素数目
int MaxSize; //堆中能容纳的最大元素数目
public:
MaxHeap(T* array,int num,int max);
virtual ~MaxHeap()
{
delete []heapArray;
}
void BuildHeap();
void Swap(int pos_x,int pos_y); //交换位置x与y的元素
bool IsLeaf(int pos) const; //如果是叶子结点,返回true
int LeftChild(int pos) const; //返回左孩子位置
int RightChild(int pos) const; //返回右孩子位置
int Parent(int pos) const; //返回父结点位置
bool Remove(int pos,T& node); //删除给定下标元素
void SiftDown(int left); //筛选法函数,参数left表示开始处理的数组下标
void SiftUp(int position); //从position向上开始调整,使序列成为堆
bool Insert(const T& newNode); //向堆中插入新元素newNode
T& RemoveMax(); //从堆顶删除最大值
void print(); //输出函数
};

下面是一些简单函数的实现:

 template<class T>
MaxHeap<T>::MaxHeap(T* array,int num,int max)
{
heapArray = array;
CurrentSize = num;
MaxSize = max;
}
template<class T>
void MaxHeap<T>::Swap(int pos_x,int pos_y)
{
T temp = heapArray[pos_x];
heapArray[pos_x] = heapArray[pos_y];
heapArray[pos_y] = temp;
}
template<class T>
bool MaxHeap<T>::IsLeaf(int pos) const
{
return (pos>=CurrentSize/)&&(pos<CurrentSize);
}
template<class T>
int MaxHeap<T>::LeftChild(int pos) const
{
return pos*+;
}
template<class T>
int MaxHeap<T>::RightChild(int pos) const
{
return pos*+;
}
template<class T>
int MaxHeap<T>::Parent(int pos) const
{
if(pos == )
return -;
return (pos-)/;
}

下面来看几个重要的操作的实现:

·堆的插入操作

(1)新元素添加到末尾(保持完全二叉树的性质);
(2)为了保持堆的性质,沿着其祖先的路径,自下而上依次比较和交换该结点与父结点的位置,直到重新满足堆的性质位置;
(3)在插入过程中,总是自下而上逐渐上升,最后停留在某个满足堆的性质的位置,故此过程又称为 “筛选”。

 template<class T>
bool MaxHeap<T>::Insert(const T& newNode)
{
if(CurrentSize == MaxSize)
return false;
heapArray[CurrentSize] = newNode;
SiftUp(CurrentSize);
CurrentSize++;
return true;
}

·建堆过程

(1)首先将所有关键码放到一维数组中,这时形成的完全二叉树并不具备堆的特性,但是仅包含叶子结点的子树已经是堆 (即在有n个结点的完全二叉树中,当i > [n/2]-1时,以关键码Ki为根的子树已经是堆。
(2)这时从含有内部结点数最少的子树(这种子树在完全二叉树的倒数第二层,此时i = [n/2]-1开始,从右至左依次调整。
(3)对这一层调整完成之后,继续对上一层进行同样的工作,直到整个过程到达树根时,整棵完全二叉树就成为一个堆了

 template<class T>
void MaxHeap<T>::BuildHeap()
{
for(int i = CurrentSize/-; i >= ; i--)
{
SiftDown(i);
}
}

·堆的删除操作

(1)把最末端结点填入删除产生的空位(保持完全二叉树的性质)
(2)为了保持堆的性质,比较当前结点和其父节点的大小来决定向上还是向下“筛选”,直到重新满足堆的性质位置

 template<class T>
bool MaxHeap<T>::Remove(int pos,T& node)
{
if(pos < || pos >= CurrentSize)
return false;
node = heapArray[pos];
heapArray[pos] = heapArray[--CurrentSize];
if(heapArray[Parent(pos)] < heapArray[pos])
{
SiftUp(pos);
}
else SiftDown(pos);
return true;
}

下面是删除堆顶元素的代码:

 template<class T>
T& MaxHeap<T>::RemoveMax()
{
if(CurrentSize == )
{
cout<<"Can't delete"<<endl;
exit();
}
else
{
Swap(,--CurrentSize);
if(CurrentSize>)
{
SiftDown();
}
return heapArray[CurrentSize];
}
}

下面是该类的核心代码:

向下筛选:

 template<class T>
void MaxHeap<T>::SiftDown(int left)
{
int i = left; //标识父结点
int j = LeftChild(i); //标识关键码较小的子结点
T temp = heapArray[i]; //保存父结点
while(j < CurrentSize) //筛选
{
if((j < CurrentSize-)&&(heapArray[j] < heapArray[j+]))
{//若有右结点,且大于左结点
j++; //则j指向右结点
}
if(temp < heapArray[j])
{//若父结点小于子结点的值则交换位置
heapArray[i] = heapArray[j];
i = j;
j = LeftChild(j);
}
else break;//找到恰当的位置,跳出循环
}
heapArray[i] = temp;
}

向上筛选:

 template<class T>
void MaxHeap<T>::SiftUp(int position)
{ //从position开始向上调整
int tempos = position;
T temp = heapArray[tempos];
while((tempos > )&&(temp > heapArray[Parent(tempos)]))
{
heapArray[tempos] = heapArray[Parent(tempos)];
tempos = Parent(tempos);
}
heapArray[tempos] = temp;
}

测试函数:

 int main()
{
int a[] = {,,,,,,,,,};
MaxHeap<int> S(a,,);
cout<<"构建最大堆:"<<endl;
S.BuildHeap();
S.print();
cout<<"插入元素10:"<<endl;
int newNode = ;
S.Insert(newNode);
S.print();
cout<<"删除堆顶元素:"<<endl;
S.RemoveMax();
S.print();
cout<<"删除pos = 1的元素:"<<endl;
int x;
S.Remove(,x);
cout<<"x = "<<x<<endl;
S.print();
return ;
}

测试结果:

[BinaryTree] 最大堆的类实现-LMLPHP

05-13 13:13