当前位置:网站首页>C++优先级队列模拟实现
C++优先级队列模拟实现
2021-02-23 18:05:55 【噫_】
实现思路:
(1)优先级队列底层默认使用vector模拟堆实现,因此要对vector中的元素进行修改,使其具有堆的性质。
(2)建堆——堆的要求是任何一个结点都比其孩子结点小或者大。如果一个结点只是比其孩子结点小或者大,而它的孩子结点不满足堆的性质,则该结构不具有堆性质,因此我们可以先从底层开始往上层走,这样先让底层具有堆的性质,则再实现上层堆性质时就不必再考虑下层。由于叶子结点本身就具有堆的性质,因此从最后一个非叶子结点开始实现堆结构,如果是实现大堆,则比较父亲结点和孩子结点中较大的那个,如果小于,则交换,一个结点向下调整完毕后,调整下一个非叶子结点,直到调整到根节点结束。
(3)堆的插入——插入新的元素以后,由于原堆是具有堆性质的,因此将新插入的元素进行向上调整,如果是大堆,则它如果比其父节点大,则交换,直到达到根节点,或者比其父节点小。
(4)堆的删除——堆只能删除根节点,主要是为了像栈和队列那样具有意义,堆的删除即出最大或者最小的那个元素。可以将堆顶元素与堆尾元素进行交换,然后将堆的大小缩小一个元素大小,再对堆顶元素进行向下调整。
(5)通过传入模板类型来表示自己想创建大堆还是小堆,以及底层结构用什么。因为模板参数是一种类型,因此可以使用仿函数或者函数指针。
template<class T>
class my_less
{
public:
bool operator()(const T& left,const T& right)
{
return left < right;
}
};
template<class T>
class my_greater
{
public:
bool operator()(const T& left, const T& right)
{
return left > right;
}
};
template<class T,class Container=vector<T>,class Com=my_less<T>>
class my_priority_queue
{
private:
Container heap_;
Com com;
void ad_down(size_t pos)
{
size_t parent_pos = pos;
size_t child_pos = parent_pos * 2 + 1;
while (child_pos < heap_.size())
{
if (child_pos+1<heap_.size() && com(heap_[child_pos],heap_[child_pos+1]))
child_pos++;
if (com(heap_[parent_pos], heap_[child_pos]))
{
swap(heap_[parent_pos], heap_[child_pos]);
parent_pos = child_pos;
child_pos = parent_pos * 2 + 1;
}
else
{
return;
}
}
}
public:
my_priority_queue()
{}
template<class Iterator>
my_priority_queue(Iterator begin, Iterator end)
:heap_(begin, end)
{
int pos = (heap_.size() - 2) >> 1;
for (; pos >=0; --pos)
ad_down(pos);
}
void push(const T& data)
{
heap_.push_back(data);
size_t child_pos = heap_.size() - 1;
size_t parent_pos = (child_pos - 1) / 2;
while (child_pos)
{
if (com(heap_[parent_pos], heap_[child_pos]))
{
swap(heap_[parent_pos], heap_[child_pos]);
child_pos = parent_pos;
parent_pos = (child_pos - 1) / 2;
}
else
{
return;
}
}
}
void pop()
{
heap_[0] = heap_.back();
heap_.pop_back();
ad_down(0);
}
const T& top()const
{
return heap_.front();
}
size_t size()
{
return heap_.size();
}
};
版权声明
本文为[噫_]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/4249634/blog/4960935
边栏推荐
- Two solutions and solutions of garbled code on Microsoft edge page of win10 Home Edition gpedit.msc Solutions to the problem that commands cannot be used
- PAT_甲级_1110 Complete Binary Tree
- PAT_ Grade A_ 1110 Complete Binary Tree
- 实际工作中到底如何开展性能测试????
- How to carry out performance test in actual work????
- UNI-APP 记录
- Uni-app record
- PostgreSQL
- PostgreSQL
- 【STM32F407】第5章 RL-USB移植(MDK AC6)
猜你喜欢
-
单机最快MQ—Disruptor
-
PAT_甲级_1111 Online Map
-
[stm32f407] Chapter 5 rl-usb porting (MDK AC6)
-
Single fastest MQ - disruptor
-
PAT_ Grade A_ 1111 Online Map
-
如何避免微服务设计中的耦合问题
-
How to avoid coupling problem in microservice design
-
51信用卡股价年初至今上浮5倍,引入银行背景高管担任行政总裁
-
51 the share price of credit card has risen five times since the beginning of the year, and senior executives with bank background have been introduced as the chief executive
-
prometheus监控之进程监控(process-exporter)
随机推荐
- 华为轮值董事长胡厚崑:技术创新的同时要避免社会发展的分化
- 疫情推动“宅经济”,企业防御DDoS更加不能松懈
- 二分图最小点覆盖构造方案+König定理证明
- Anno&Viper -分布式锁服务端怎么实现
- 解决Win7 X64由于百联控件造成的蓝屏问题 (PassGuard_X64.sys)
- Process exporter of Prometheus monitoring
- 浅谈 Vite 2.0 原理,依赖预编译,插件机制是如何兼容 Rollup 的?
- Hu houkun, Huawei's rotating Chairman: avoid the differentiation of social development while making technological innovation
- The epidemic situation promotes "residential economy", and enterprises' defense against DDoS cannot be relaxed
- Construction scheme of minimum point cover of bipartite graph + proof of K ü nig theorem
- npm install 版本号^的坑
- Activity显示界面背后的故事:一文让你理清View的那些复杂关系
- Android面试官:Window连环十二问你顶得住吗?(快扶我起来,我还能问)
- 开发一个小程序,最好先做好课前工作
- SQL Server中DELETE和TRUNCATE的区别
- Simar 的 参考书
- 【招聘】分布式存储架构师 40K-80K*14薪
- How to implement anno & Viper - distributed lock server
- Solve the blue screen problem of win7 x64 caused by Bailian control (PassGuard)_ X64.sys)
- Talk about the vite 2.0 principle, dependence precompile, how is plug-in mechanism compatible with rollup?
- 哔哩哔哩视频爬取源码分享
- NPM install version number ^
- The story behind the activity display interface: let you clarify the complex relationships of view
- Android Interviewer: can you stand up to the 12 questions of windows? (help me up, I can still ask)
- Develop a small program, it is best to do a good job before class
- The difference between delete and truncate in SQL Server
- SIMAR's reference book
- [recruitment] 40k-80k * 14 salary for distributed storage architect
- Bili Bili video crawling source code sharing
- 面试这么久,第一次投诉,就这样没了……
- 二分图最小点覆盖构造方案+König定理证明
- Anno&Viper -分布式锁服务端怎么实现
- Interview so long, the first complaint, so gone
- Construction scheme of minimum point cover of bipartite graph + proof of K ü nig theorem
- How to implement anno & Viper - distributed lock server
- 混合云组网与管理(Wireguard+OpenVPN+LDAP)
- 如何将福禄克DSX2-5000、8000 CH恢复出厂设置
- Hybrid cloud networking and management (wireguard + OpenVPN + LDAP)
- How to restore the factory settings of fluke dsx2-5000 and 8000 Ch
- 线段树&数链剖分