堆和优先级队列
来源:互联网 发布:免费录音机软件下载 编辑:程序博客网 时间:2024/06/05 17:44
vs2013下编写的项目工程见 我的 github: https://github.com/excelentone/DataStruct
heap.h
#include<iostream>#include<cassert>#include <vector>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> >template<class T, template<class T> class Compare = Less >class Heap{public: // ´´½¨Ò»¸ö¿Õ¶Ñ Heap() {} Heap(const T array[], size_t size) { _heap.resize(size); for (size_t i = 0; i < size; i++) { _heap[i] = array[i]; } AdjustDown(); } void AdjustDown() { int flag = (_heap.size() - 2) / 2; for (int i = flag; i >= 0; i--) { _AdjustDown(i); } } size_t Size()const { return _heap.size(); } bool Empty()const { return _heap.empty(); } void Insert(const T& data) { _heap.push_back(data); _AdjustUp(_heap.size() - 1); } void Remove() { assert(_heap.size() != 0); swap(_heap[0], _heap[_heap.size() - 1]); _heap.pop_back(); AdjustDown(); } void Print() { for (size_t i = 0; i < _heap.size(); i++) { cout << _heap[i] << " "; } cout << endl; } const T &Top() { return _heap[0]; }protected: void _AdjustDown(size_t parent) { int child = 2 * parent + 1; Compare<T> com; while (child <= _heap.size()) { if (((child + 1) < _heap.size()) && com(_heap[child + 1], _heap[child])) { child += 1; } if (com(_heap[child], _heap[parent])) { swap(_heap[parent], _heap[child]); parent = child; child = 2 * parent + 1; } else { break; } } } void _AdjustUp(size_t child) { int parent = (child - 1) / 2; while (child > 0) { if (Compare<T>()(_heap[child], _heap[parent])) { swap(_heap[child], _heap[parent]); child = parent; parent = (child - 1) / 2; } else break; } }protected: vector<T> _heap;};
PriorityQueue.cpp
#include"Heap.h"template<class T, class Compare = Less<T>>class PriorityQueue{public: PriorityQueue() :_hp() {} PriorityQueue(T *arr, size_t size) :_hp(arr, size) {} void Push(const T& data) { _hp.Insert(data); } void Pop() { _hp.Remove(); } const T& Top()const { return _hp.top(); } size_t Size()const { return _hp.Size(); } bool Empty()const { return _hp.Empty(); } void Print() { _hp.Print(); }protected: Heap<T, Less> _hp;};void test1(){ int arr[] = { 53, 17, 78, 9, 45, 65, 87, 23 }; Heap<int, Less> hp(arr, sizeof(arr) / sizeof(arr[0])); hp.Insert(5); hp.Print(); // hp.Remove(); hp.Print();}void test2(){ int arr[] = { 53, 17, 78, 9, 45, 65, 87, 23 }; PriorityQueue<int> pq;//pq(arr, sizeof(arr) / sizeof(arr[0])); pq.Push(53); pq.Push(17); pq.Push(78); pq.Pop(); pq.Print();}int main(){ test1(); test2(); system("pause");}
阅读全文
0 0
- 堆和优先级队列
- 堆和优先级队列
- 堆排序和优先级队列
- 堆排序和优先级队列
- 堆、优先级队列和堆排序
- 【算法导论】堆排序和优先级队列
- 优先级队列和堆的应用
- 堆与优先级队列
- 堆与优先级队列
- 堆优先级队列
- 优先级队列+堆
- 堆构造优先级队列
- hdu1509堆 优先级队列
- 最大堆优先级队列
- 二叉堆&&优先级队列
- 堆(优先级队列)
- 【数据结构】堆&优先级队列
- 堆&优先级队列
- caffe学习笔记2:数据层及其参数
- c语言控制台推箱子小游戏
- spring bean加载过程
- JAX-RS 2.0如何验证查询参数
- Oracle 更新Opatch、打补丁
- 堆和优先级队列
- mongodb配置文件启动参数详解
- eval的用处
- Linux 上安装 rlwrap
- 各种内部排序算法复杂度的比较和排序方法的选择
- 洛谷P3258 [JLOI2014]松鼠的新家
- 数据结构封装之《LinkList单向链表》
- POJ 1949 Chores 笔记
- javascript中的内置类型