heap 实现 code

heap 类实现,


装入heap的结构要实现>= 和 <= 运算符


/*
 * 堆的实现
 */
#ifndef __MHEAP__ 
#define __MHEAP__ 

#include <stdio.h>
#include <stdlib.h>

template <class T>
class MHeap  
{
public:	
	bool Cmp(const T &a, const T &b)
	{
		return IsMaxHeap ? a <= b : a >= b;
	}
	
	// big 为真则创建大堆, 否则小堆
	MHeap(int n, int big);
	~MHeap();
	
	void BuildHeap();
	
	// 删除给定下标的元素
	bool Remove(int pos, T& val);
	
	// 从position向下开始调整
	void ShiftDown(int pos);
	
	// 从position向上开始调整
	void ShiftUp(int pos);
	
	// 压入数据
	bool Push(const T& val);
	inline bool Top(T &val);
	void Pop();
	inline bool Empty();

private:
	T *Data;
	int CurrentSize;
	int MaxSize;
	int IsMaxHeap;
};

template<class T>
MHeap<T>::MHeap(int n, int big)
{
	if(n <= 0)
		return;
	CurrentSize = 0;
	MaxSize = n;
	IsMaxHeap = big;
	Data = (T *)malloc(sizeof(T) * MaxSize);
	if (!Data)
		return;
}

template<class T>
MHeap<T>::~MHeap()
{
	free(Data);
}

template<class T>
void MHeap<T>::BuildHeap()
{
	for (int i = (CurrentSize >> 1) - 1; i >= 0; i--) 
		ShiftDown(i); 
}

template<class T>
void MHeap<T>::ShiftDown(int pos)
{
	int index = pos;
	T temp = Data[index];
	
	for (int child = ((index << 1) + 1); child < CurrentSize; 
		index = child, child = ((index << 1) + 1)) {
		if((child < CurrentSize - 1)&& Cmp(Data[child], Data[child + 1]))
			child++;
		if(Cmp(Data[child], temp))
			break;

		Data[index] = Data[child];
	}
	
	Data[index]=temp;
}

template<class T>
void MHeap<T>::ShiftUp(int pos) 
{
	int index = pos;
	T temp = Data[index];
	while(index > 0) {
		int parent = ((index - 1) >> 1);
		if (Cmp(temp, Data[parent]))
			break;
			
		Data[index] = Data[parent];
		index = parent;
	}
	Data[index] = temp;
}

template<class T>
bool MHeap<T>::Push(const T& val)
{
	if(CurrentSize == MaxSize)
		return false;
	Data[CurrentSize] = val;
	ShiftUp(CurrentSize);
	CurrentSize++;
	return true;
}

template<class T>
bool MHeap<T>::Top(T &val)
{	
	if(CurrentSize==0)
		return false;

	val = Data[0];
	return true;
}

template<class T>
void MHeap<T>::Pop()
{	
	if(CurrentSize==0)
		return;

	Data[0] = Data[--CurrentSize];
	ShiftDown(0);
	
	return;
}

template<class T>
bool MHeap<T>::Remove(int pos, T& val)
{
	T temp = Data[pos];
	
	if(pos < 0 || pos >= CurrentSize)
		return false;

	Data[pos] = Data[--CurrentSize];
	ShiftUp(pos);
	ShiftDown(pos);
	val = temp;
	
	return true;
}

template<class T>
bool MHeap<T>::Empty()
{
	return CurrentSize <= 0;
}

#endif


int main(int argc, char **argv)
{
	MHeap<int> heap(256, 1);
	
	for (int i = 1; i < argc; i++) {
		int tmp = atoi(argv[i]);
		heap.Push(tmp);
	}
		
	while(!heap.Empty()) {
		int res;
		heap.Top(res);
		printf("%d\n", res);
		heap.Pop();
	}
	
	return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值