【C++】优先级队列的模拟实现
来源:互联网 发布:玩客云京东抢购软件 编辑:程序博客网 时间:2024/06/05 02:29
优先级队列是在堆的基础上进行的一个扩展,主要是运用大堆来实现优先级队列,让优先度高的出队列,最大优先队列可以选用大堆,最小优先队列可以选用小堆来实现。
下面以最大优先队列来讲解其原理。
最大优先队列一般包括将一个元素插入到集合S中、返回集合S中具有最大key的元素、返回并删除集合S中具有最大key的元素等。
堆的实现代码如下
下面给出实现代码:
#include <vector>#include <assert.h>using namespace std;template<class T>struct Less{ bool operator()(const T& left, const T& right) { return left < right; }};template<class T>struct Greater{ bool operator()(const T& left, const T& right) { return left > right; }};template<class T, class Compare = Less<T>>class Heap{public: Heap() {} Heap(const T array[], size_t size) { _heap.reserve(size); for (size_t i = 0; i < size; i++) { _heap.push_back(array[i]); } for (int j = (_heap.size() - 2) / 2; j >= 0; j--) { _AdjustDown(j); } } void Insert(const T& data) { assert(data); assert(!_heap.empty()); _heap.push_back(data); _Adjustup(_heap.size() - 1); } void Remove() { assert(!_heap.empty()); swap(_heap[0], _heap[size() - 1]); _heap.pop_back(); _AdjustDown(0); } void Push(const T& x) { //插入到尾部,再从最后一个元素开始向上调整 _a.push_back(x); AdjustUp(_a.size()-1); } void Pop() { //将堆顶与最后一个元素交换,再从堆顶下滑调整 assert(!_a.empty()); swap(_a[0], _a[_a.size() - 1]); _a.pop_back(); if (_a.size()>1) //如果堆中的元素大于一个,再进行调整 { AdjustDown(0, _a.size()); } } size_t Size()const { return _heap.size(); } bool Empty()const { return _heap.empty(); } const T& Top()const { assert(!_heap()); return _heap[0]; } void _AdjustDown(size_t parent) { assert(parent); assert(!_heap.empty()); size_t child = parent * 2 - 1; while (parent < _heap.size()) { Compare com; if (child + 1<_heap.size()&& com(_heap[child + 1], _heap[child])) { ++child; } if (child<_heap.size() && com(_heap[child], _heap[parent])) { swap(_heap[child], _heap[parent]); parent = child; child = parent * 2 + 1; } else break; } } void _AdjustUp(size_t child) { assert(!_heap.empty()); while(child) { Compare com; size_t parent = (child - 1) / 2; if (com(_heap[child], _heap[parent])) { swap(_heap[child], _heap[parent]); child = parent; } else break; } }private: vector<T> _heap;};template<class T, class Compare = Less<T>>class PriorityQueue{public: PriorityQueue() {} PriorityQueue(T* a, size_t size) :_h(a, size) { } void Push(const T& data) { _hp.push(data); } void Pop() { _hp.pop(); } const T& Top()const { _hp.Top(); } size_t Size()const { return _heap.size(); } bool Empty()const { return _hp.Empty(); }protected: Heap<T, Compare> _hp;};
0 0
- 【C++】优先级队列的模拟实现
- C语言实现的优先级队列
- 优先级队列的实现
- 优先级队列的实现
- 利用优先级队列实现事件驱动模拟
- 【C++】模拟实现队列
- (C++)栈和队列的模拟实现
- 数据结构之带优先级的队列(C语言实现)
- 关于优先级队列的实现
- 优先级队列的一种实现
- 堆实现的优先级队列
- HDU1509(优先级队列+模拟)
- hdu1896(模拟+优先级队列)
- 队列的模拟实现
- C++模拟实现堆heap和优先级队列poriorty_queue
- 【数据结构】堆排序和模拟实现优先级队列!!
- 一般队列和循环队列及优先级队列的实现
- 【day0414 C++】优先级队列 priority_queue的使用
- 在vs code中使用ftp-sync插件实现客户端与服务器端代码的同步
- 深入理解Java注解类型(@Annotation)
- c++获取剪切板的内容
- python变量笔记
- 管道的四种读写情况
- 【C++】优先级队列的模拟实现
- 错误:在自己制作的dll库时,出现错误"*100ud.dll"丢失
- 数据库TPCC benchmark测试工具对比
- 51Nod-1804-小C的多边形
- oracle帐号scott被锁定如何解锁
- Ajax概念介绍
- 共享内存
- Oracle
- 错误:在使用win32应用程序时,一开始运行就中断