优先级队列

20 篇文章 0 订阅
5 篇文章 0 订阅

题目:优先级队列(封装堆)

注:由于大堆与小堆的实现大同小异,只需要修改大于或小于号;所以为了代码复用,本人使用了模板参数及模板的模板参数
模板的模板参数:为了引入仿函数即函数对象;此处不多介绍仿函数,它只是一个函数对象;一个类型;如下:
类型GreaterCompare与类型LessCompare:他们的内部都只是重载了();

template<class T>
struct GreaterCompare
{
bool operator()(const T& leftChild, const T& rightChild)
{
return leftChild > rightChild;
}
};

template<class T>
struct LessCompare
{
bool operator()(const T& leftChild, const T& rightChild)
{
return leftChild < rightChild;
}
};


堆的实现代码如下:

#pragma once
#include<iostream>
using namespace std;
#include<vector>
//仿函数(函数对象)
template<class T>
struct GreaterCompare
{
	bool operator()(const T& leftChild, const T& rightChild)
	{
		return leftChild > rightChild;
	}
};

template<class T>
struct LessCompare
{
	bool operator()(const T& leftChild, const T& rightChild)
	{
		return leftChild < rightChild;
	}
};

//模板类型的模板形参
template<class T,template<class> class Compare = GreaterCompare> //缺省参数
class Heap
{
public:
	Heap()
	{}

	Heap(const T* arr,int len)
	{
		if (arr == NULL || len <= 0)
			return;

		for (int i = 0; i < len; ++i)
		{
			_heapArr.push_back(arr[i]);
		}

		//建堆:找第一个非叶子结点的父亲,然后向下调整
		for (int i = len - 1; i >= 0; --i)
		{
			int root = (i - 1) / 2;
			_AdjustDown(root);
		}
	}

	//末尾增加一个结点后,向上调整
	void Push(const T& x)
	{
		_heapArr.push_back(x);
		_AdjustUp(_heapArr.size() - 1);
	}

	//删除一个结点时,先将堆顶数据与末尾数据交换,然后删除末尾数据,最后向下调整
	void Pop()
	{
		swap(_heapArr[0], _heapArr[_heapArr.size() - 1]);
		_heapArr.pop_back();
		_AdjustDown(0);
	}

	T& Size()
	{
		return _heapArr.size();
	}

	bool Empty()
	{
		return _heapArr.empty();
	}

	T& Top()
	{
		return _heapArr[0];
	}

	void Print()
	{
		for (int i = 0; i < (int)_heapArr.size(); ++i)
		{
			cout << _heapArr[i] << " ";
		}
		cout << endl;
	}

	~Heap()
	{}

protected:
	//向下调整
	void _AdjustDown(int root)
	{
		Compare<T> com;
		int child = root * 2 + 1;
		while (child < (int)_heapArr.size())
		{
			if (child + 1 < (int)_heapArr.size() && com(_heapArr[child + 1], _heapArr[child]))
			{
				++child;
			}

			if (com(_heapArr[child], _heapArr[root]))
			{
				swap(_heapArr[child], _heapArr[root]);
				root = child;
				child = root * 2 + 1;
			}
			else
			{
				break;
			}
		}	
	}

	//向上调整
	void _AdjustUp(int child)
	{
		Compare<T> com;
		int root = (child - 1) / 2;
		while (child > 0)
		{
			if (com(_heapArr[child], _heapArr[root]))
			{
				swap(_heapArr[child], _heapArr[root]);
				child = root;
				root = (child - 1) / 2;
			}
			else
			{
				break;
			}
		}
	}

private:
	vector<T> _heapArr;
};

//测试用例
void TestHeap()
{
	int arr[10] = { 10, 11, 13, 12, 16, 18, 15, 17, 14, 19 };
	int len = sizeof(arr) / sizeof(arr[0]);
	//大堆
	Heap<int> hp1(arr, len);
	hp1.Print();
	//小堆
	Heap<int,LessCompare> hp(arr, len);
	hp.Print();

	hp.Push(21);
	hp.Print();

	hp.Pop();
	hp.Print();
}

测试结果如下图:




优先级队列的实现就是对上面堆的一层封装,代码如下:

//将堆封装成优先级队列
template <class T>
class PriorityQueue
{
public:
	PriorityQueue(Heap<T>& heap)
		:_heap(heap)
	{}

	~PriorityQueue()
	{}

	void QueuePush(const T& x)
	{
		_heap.Push();
	}

	void QueuePop()
	{
		_heap.Pop();
	}

	bool QueueEmpty()
	{
		return _heap.Empty();
	}

	int QueueSize
	{
		return _heap.Size();
	}

	T& QueueFront()
	{
		return _heap.Top();
	}

private:
	Heap<T> _heap;
};






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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值