数据结构之二叉堆

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


        二叉堆一般用数组来表示。例如,根节点在数组中的位置是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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值