您的位置:首页 > 理论基础 > 数据结构算法

数据结构之二叉堆

2014-04-26 17:37 267 查看
二叉堆是一种特殊的堆,二叉堆是完全二元树或者是近似完全二元树。二叉堆有两种:最大堆和最小堆。最大堆:父结点的键值总是大于或等于任何一个子节点的键值;最小堆:父结点的键值总是小于或等于任何一个子节点的键值。

二叉堆一般用数组来表示。例如,根节点在数组中的位置是0,第n个位置的子节点分别在2n+1和 2n+2。


二叉堆的基本操作:


1、插入节点 新插入的节点始终放在末端,然后向上调整二叉堆


2、删除堆顶端元素
把末端元素放到顶端,然后向下调整二叉堆


3、增加给定位置的值


3、降低给定位置的值

最大堆的代码实现:

#ifndef BINARYHEAP_H
#define BINARYHEAP_H
namespace binaryheap
{
template <typename Element>
class BinaryHeap
{
private:
Element* head;
int tail;
int size;
public:
BinaryHeap(int s){head = new Element[s];tail=-1;size=s;}
~BinaryHeap(){delete [] head;}
bool IsFailed(){return head?false:true;}//判断堆是否创建成功
void MaxHeapAdjustDown(Element* h,int start,int end);//向下调整最大堆
void MaxHeapAdjustUp(Element* h,int end);//向上调整最大堆
void AdjustMaxHeap(Element* h,int end);//把数组中的元素调整为最大堆
bool InsertElement(Element data);//在堆中插入元素
bool DecreaseKeyValue(int pos,int differ);//降低给定位置的值
bool IncreaseKeyValue(int pos,int differ);//增加给定位置的值
bool RemoveBigElement(Element* data);//移除堆顶端元素
void SmallToBigSort();//把最大堆从小到大排序
void AdjustMaxHeap();
void Show();
};
template <typename Element>
void BinaryHeap<Element>::MaxHeapAdjustDown(Element* h,int start,int end)
{
Element tem = h[start];
int i = 2*start+1;
while(i<=end)
{
if((i+1<=end)&&(h[i]<h[i+1]))
i++;
if(h[i]<=tem)
break;
h[start] = h[i];
start = i;
i = 2*start + 1;
}
h[start] = tem;
}
template <typename Element>
void BinaryHeap<Element>::MaxHeapAdjustUp(Element* h,int end)
{
Element tem = h[end];
int i = (end-1)/2;
while(end!=0)
{
if(h[i]>=tem)
break;
h[end] = h[i];
end = i;
i = (end-1)/2;
}
h[end] = tem;
}
template <typename Element>
void BinaryHeap<Element>::AdjustMaxHeap(Element* h,int end)
{
for(int i=(end-1)/2;i>=0;i--)
MaxHeapAdjustDown(h,i,end);
}
template <typename Element>
bool BinaryHeap<Element>::InsertElement(Element data)
{
if(tail+1==size)
return false;
tail++;
head[tail] = data;
MaxHeapAdjustUp(head,tail);
return true;
}
template <typename Element>
bool BinaryHeap<Element>::DecreaseKeyValue(int pos,int differ)
{
if(pos<0||pos>tail)
return false;
head[pos] -= differ;
MaxHeapAdjustDown(head,pos,tail);
return true;
}
template <typename Element>
bool BinaryHeap<Element>::IncreaseKeyValue(int pos,int differ)
{
if(pos<0||pos>tail)
return false;
head[pos] += differ;
MaxHeapAdjustUp(head,pos);
return true;
}
template <typename Element>
bool BinaryHeap<Element>::RemoveBigElement(Element* data)
{
if(tail==-1)
return false;
if(data != NULL)
*data = head[0];
head[0] = head[tail];
tail--;
MaxHeapAdjustDown(head,0,tail);
return true;
}
template <typename Element>
void BinaryHeap<Element>::SmallToBigSort()
{
Element tem;
for(int i=tail;i>0;i--)
{
tem = head[i];
head[i] = head[0];
head[0] = tem;
MaxHeapAdjustDown(head,0,i-1);
}
}
template <typename Element>
void BinaryHeap<Element>::AdjustMaxHeap()
{
AdjustMaxHeap(head,tail);
}
template <typename Element>
void BinaryHeap<Element>::Show()
{
for(int i=0;i<=tail;i++)
cout<<head[i]<<" ";
cout<<endl;
}
}
#endif
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: