目录
优先级队列的使用
在数据结构中我们学习了堆的概念,我们先来简单回顾一下,堆是一个完全二叉树,我们用顺序结构来存储。
其实,优先级队列本质上就是我们在数据结构中学习过的堆,优先级队列本质上也是一个适配器容器,因为我们用线性结构来存储,所以vector为我们所采用的适配器类型。
优先级队列的常用接口:push,pop,empty,top,size
#include<iostream>
using namespace std;
#include<queue>
int main()
{
priority_queue<int> pq;
//往优先级队列中插入元素
pq.push(1);
pq.push(2);
pq.push(3);
pq.push(4);
//统计元素的个数
cout << "优先级队列中的元素的个数为:" << pq.size() << endl;
//获取队列顶部的元素
cout << "优先级队列顶部的元素为:" << pq.top() << endl;
//判断队列是否为空,遍历并删除队列顶的元素
while (!pq.empty())
{
cout << pq.top() << " ";
pq.pop();
}
cout << endl;
return 0;
}
运行结果。
运行结果符合预期。
优先级队列的模拟实现
仿函数
仿函数是什么?
仿函数本质上也是一个类,不过这个类重载了()这个运算符,导致该类的对象可以像函数一样去使用,完成我们想要预期实现的功能。
在优先级队列中,我们使用到了Greater和Less这两个仿函数。如果要建立大堆我们使用Less,如果要建立小堆,我们使用Greater。
template<class T>
struct Less
{
bool operator()(const T& a,const T& b) const
{
return a < b;
}
};
template<class T>
struct Greater
{
bool operator()(const T& a, const T& b) const
{
return a > b;
}
};
向上调整算法
void AdjustUp(size_t child)
{
size_t parent = (child - 1) / 2;
while (child > 0)
{
Com com;
if (com(_con[parent], _con[child]))
{
swap(_con[parent], _con[child]);
child = parent;
parent = (child - 1) / 2;
}
else
{
break;
}
}
}
优先级队列的本质就是堆,所以我们在插入元素时,我们会用到向上调整算法。
向下调整算法
void AdjustDown(size_t parent)
{
size_t child = (parent * 2) + 1;
while(child< _con.size())
{
Com com;
if (child + 1 < _con.size() && com(_con[child], _con[child + 1]))
{
++child;
}
if (com(_con[parent], _con[child]))
{
swap(_con[parent],_con[child]);
parent=child;
child=parent*2+1;
}
else
{
break;
}
}
}
在删除元素和建堆时,我们会用到向下调整算法。
构造函数,使用迭代器区间进行初始化
template <class InputIterator>
priority_queue(InputIterator first,InputIterator last)
{
_con(first, last);
for (int i = (_con.size-1 - 1) / 2; i >= 0; i)
{
AdjustDown(i);
}
}
向队列中插入元素
void push(const T& x)
{
_con.push_back(x);
AdjustUp(_con.size() - 1);
}
通过复用_con对象的push_back接口,插入后使用向上调整算法调整堆。
删除队列的元素
void pop()
{
swap(_con[0], _con[_con.size() - 1]);
_con.pop_back();
AdjustDown(0);
}
优先级队列本质是堆,删除队列元素也即删除堆的堆顶的元素。堆顶元素先于堆底最后一个元素交换,然后复用_con对象的pop_back函数,然后进行向下调整算法调整堆。
求队列中元素的个数
size_t size() const
{
return _con.size();
}
复用了_con对象的size接口。
判断队列是否为空
bool empty()const
{
return _con.empty();
}
复用了_con对象的empty接口。
返回队列顶部的元素
const T& top()const
{
return _con[0];
}
返回队列顶部的元素其实也就是返回堆顶的元素。因为是顺序存储,所以范围适配器对象的第一个元素即可。
整体代码
using namespace std;
#include<vector>
namespace yjd {
template<class T>
struct Less
{
bool operator()(const T& a,const T& b) const
{
return a < b;
}
};
template<class T>
struct Greater
{
bool operator()(const T& a, const T& b) const
{
return a > b;
}
};
template<class T, class Container = vector<T>,class Com=Less<T> >
class priority_queue
{
private:
void AdjustUp(size_t child)
{
size_t parent = (child - 1) / 2;
while (child > 0)
{
Com com;
if (com(_con[parent], _con[child]))
{
swap(_con[parent], _con[child]);
child = parent;
parent = (child - 1) / 2;
}
else
{
break;
}
}
}
void AdjustDown(size_t parent)
{
size_t child = (parent * 2) + 1;
while(child< _con.size())
{
Com com;
if (child + 1 < _con.size() && com(_con[child], _con[child + 1]))
{
++child;
}
if (com(_con[parent], _con[child]))
{
swap(_con[parent],_con[child]);
parent=child;
child=parent*2+1;
}
else
{
break;
}
}
}
public:
priority_queue()
{
}
template <class InputIterator>
priority_queue(InputIterator first,InputIterator last)
{
_con(first, last);
for (int i = (_con.size-1 - 1) / 2; i >= 0; i)
{
AdjustDown(i);
}
}
void push(const T& x)
{
_con.push_back(x);
AdjustUp(_con.size() - 1);
}
void pop()
{
swap(_con[0], _con[_con.size() - 1]);
_con.pop_back();
AdjustDown(0);
}
size_t size() const
{
return _con.size();
}
bool empty()const
{
return _con.empty();
}
const T& top()const
{
return _con[0];
}
private:
Container _con;
};
}
main函数代码。
#include<iostream>
using namespace std;
#include<queue>
#include"PriorityQueue.h"
int main()
{
yjd::priority_queue<int> pq;
//往优先级队列中插入元素
pq.push(1);
pq.push(2);
pq.push(3);
pq.push(4);
pq.push(4);
//统计元素的个数
cout << "优先级队列中的元素的个数为:" << pq.size() << endl;
//获取队列顶部的元素
cout << "优先级队列顶部的元素为:" << pq.top() << endl;
//判断队列是否为空,遍历并删除队列顶的元素
while (!pq.empty())
{
cout << pq.top() << " ";
pq.pop();
}
cout << endl;
return 0;
}
运行结果。
运行结果符合预期。
本期的内容到此结束^_^