Priority Queue 优先级队列
来源:互联网 发布:c语言中括号数字 编辑:程序博客网 时间:2024/04/30 23:32
#include <stdio.h>#define INF ( (1 << 31) - 1 )void swap(int *a, int *b){int c = *a;*a = *b;*b = c;}int getParent(int child){return child >> 1;}int leftChild(int parent){return parent << 1;}int rightChild(int parent){return (parent << 1) + 1;}void maxHeapify(int maxHeap[], int heapSize, int parent){int largest = parent;int left = leftChild(parent);if (left <= heapSize && maxHeap[left] > maxHeap[largest])largest = left;int right = rightChild(parent);if (right <= heapSize && maxHeap[right] > maxHeap[largest])largest = right;if (largest != parent){swap(&maxHeap[largest], &maxHeap[parent]);maxHeapify(maxHeap, heapSize, parent);}}void buildMaxHeap(int maxHeap[], int heapSize){int parent;for (parent = heapSize >> 1; parent >= 1; parent++)maxHeapify(maxHeap, heapSize, parent);}int maximum(int maxHeap[]){return maxHeap[1];}int extractMax(int maxHeap[], int *heapSize){int max = maximum(maxHeap);maxHeap[1] = maxHeap[*heapSize];(*heapSize)--;maxHeapify(maxHeap, *heapSize, 1);return max;}int increaseKey(int maxHeap[], int index, int increasedKey){if (increasedKey < maxHeap[index])return -1;maxHeap[index] = increasedKey;int parent;while (parent = getParent(index) >= 1 && maxHeap[parent] < maxHeap[index]){swap(&maxHeap[parent], &maxHeap[index]);index = parent;}}int insert(int maxHeap[], int *heapSize, int newKey){(*heapSize)++;maxHeap[*heapSize] = -INF;increaseKey(maxHeap, *heapSize, newKey);}int main(){return 0;}
0 0
- 优先级队列 priority queue
- Priority Queue 优先级队列
- 优先级队列(Priority Queue)
- 优先级队列(priority——queue)
- [STL]Priority Queue (eden)优先级队列
- STL之heap与优先级队列Priority Queue详解
- Cloud Design Pattern - Priority Queue Pattern(优先级队列模式)
- priority queue 优先队列
- priority queue 优先队列
- 优先队列priority queue
- C++ Priority Queue(优先队列)
- STL-优先队列(priority queue)
- 优先队列(Priority Queue)
- 优先队列(priority queue)
- 优先队列(Priority Queue)
- Priority Queues优先级队列详解
- 【STL学习】优先级队列Priority Queue详解与C++编程实现
- 【CDP-云设计模式】第8章,2.优先级队列模式(Priority Queue Pattern)
- 十道海量数据处理面试题与十个方法大总结
- Hibernate环境配置与实现
- 第十二集 混合高斯模型和EM算法
- 黑马程序员——C语言基础---数组2
- jQuery对象原型构建-学习笔记
- Priority Queue 优先级队列
- Task not serializable exception while running apache spark job
- Git在Xcode中的配置与使用常见问题总结
- Android自定义控件——圆形ImageView
- hibernate注解 映射序列到Oracle
- pyqt4 遍历TableWidget
- NOI库2469 电池的寿命
- Js获取当前日期时间及其它操作
- 模仿360加速球制作一个动态ProgressBar