最小优先队列 解决TopK问题
2015-08-21 21:46
357 查看
#include <vector> #include <algorithm> #include <limits> #include <iostream> using namespace std; //////// 优先队列 #define MAX_HEAP 300 class PriorityHeap { public: PriorityHeap(); ~PriorityHeap(); bool empty(); int size(); int top(); void push(int nValue); int pop(); private: int GetParentIndex(int nIndex); const int& GetParentValue(int nIndex); int GetLeftIndex(int nIndex); int GetRightIndex(int nIndex); void AdjustHeap(int nIndex); // 调整根节点为nIndex的子树 前提是left(nIndex) right(nIndex)都是大顶堆 int heap[MAX_HEAP]; int heapSize; }; PriorityHeap::PriorityHeap() { memset(heap, 0, sizeof(heap)); heapSize = 0; } PriorityHeap::~PriorityHeap() { } bool PriorityHeap::empty() { return heapSize > 0 ? false : true; } const int& PriorityHeap::GetParentValue(int nIndex) { if (nIndex < 0 || nIndex >= heapSize) { return 0; } int nParentIndex = GetParentIndex(nIndex); return heap[nParentIndex]; } int PriorityHeap::GetParentIndex(int nIndex) { //return nIndex/2 - 1 > 0 ? nIndex/2 - 1 : 0; return (nIndex+1)/2 - 1; } int PriorityHeap::GetLeftIndex(int nIndex) { return 2*nIndex + 1; } int PriorityHeap::GetRightIndex(int nIndex) { return 2*nIndex + 2; } void PriorityHeap::push(int nValue) { heapSize = heapSize + 1; int heapIndex = heapSize -1; heap[heapIndex] = nValue; int nParentIndex = GetParentIndex(heapIndex); while(heapIndex > 0 && heap[nParentIndex] > heap[heapIndex]) { int nTemp = heap[heapIndex]; heap[heapIndex] = heap[nParentIndex]; heap[nParentIndex] = nTemp; heapIndex = nParentIndex; nParentIndex = GetParentIndex(heapIndex); } } int PriorityHeap::top() { return heap[0]; } int PriorityHeap::size() { return heapSize; } int PriorityHeap::pop() { int nRoot = heap[0]; heapSize = heapSize - 1; heap[0] = heap[heapSize]; AdjustHeap(0); return nRoot; } void PriorityHeap::AdjustHeap(int nIndex) { int largestIndex = nIndex; int leftIndex = GetLeftIndex(nIndex); int rightIndex = GetRightIndex(nIndex); if (leftIndex < heapSize && heap[leftIndex] < heap[largestIndex]) { largestIndex = leftIndex; } if (rightIndex < heapSize && heap[rightIndex] < heap[largestIndex]) { largestIndex = rightIndex; } if (largestIndex != nIndex) { int nTemp = heap[largestIndex]; heap[largestIndex] = heap[nIndex]; heap[nIndex] = nTemp; AdjustHeap(largestIndex); } } void FindTopK() { int nValue; int k; PriorityHeap heap; cin >> k; while (cin >> nValue) { if (heap.size() < k) { heap.push(nValue); } else if(nValue > heap.top()) { heap.pop(); heap.push(nValue); } } while(!heap.empty()) { cout << heap.pop() << " " << endl; } system("pause"); } int main() { PriorityHeap Heap; int nValue; while (cin >> nValue) { Heap.push(nValue); } while(!Heap.empty()) { cout << Heap.pop() << " " << endl; } FindTopK(); return 0; }
相关文章推荐
- OpenCV最常用
- 详细介绍Linux重定向的使用
- Linux下的bash配置文件
- Nginx 学习
- OPENCV错误
- Linux系统管理之十四---服务
- 后端分布式系列:分布式存储-HDFS 架构解析
- 后端分布式系列:分布式存储-HDFS 架构解析
- shell编程——if条件判断(转)
- 后端分布式系列:分布式存储-HDFS 架构解析
- HDFS 架构解析
- getopt()函数
- 运维故障总结
- Opencv书籍
- redhat linux7.0安装没有检测到硬盘解决办法
- 使用GDI+可以方便的把OpenCV的图像矩阵类型数据显示在MFC的窗口中
- Linux中dd命令详解
- 2.0-apache日志切割
- OpenCV 谈opencv的长处与短处:模板匹配,金字塔
- linux基础操作--设置系统时间