优先级队列的Java ,C++ STL,堆实现
2011-08-24 20:06
513 查看
0 整理自网络
1. Java版(转) 要比较的对象必须实现Comparable接口,重写compareTo 方法,Java在红自带有优先级队列的实现PriorityQueue
Queue<ToDoItem> q = new PriorityQueue<ToDoItem>();
2. C++ STL 版
STL中也有自带的priority_queue 而且可以有2种比较的方式,greater和less
还可以通过 自定义函数法,是一个 仿函数
3. 自己实现,以建堆的方式实现优先级队列
1. Java版(转) 要比较的对象必须实现Comparable接口,重写compareTo 方法,Java在红自带有优先级队列的实现PriorityQueue
Queue<ToDoItem> q = new PriorityQueue<ToDoItem>();
import java.util.Collections; import java.util.PriorityQueue; import java.util.Queue; public class ToDoItem implements Comparable<ToDoItem>{ private char primary; private int secondary; private String item; public ToDoItem(char primary,int secondary,String item) { super(); this.primary = primary; this.secondary = secondary; this.item = item; } @Override public int compareTo(ToDoItem o) { // TODO Auto-generated method stub if(this.primary > o.primary) return 1; if(this.primary == o.primary) { if(this.secondary > o.secondary) return 1; else if(this.secondary == o.secondary) return 0; } return -1; } public String toString() { return Character.toString(primary) + this.secondary + " : " + this.item; } public static void main(String [] args) { Queue<ToDoItem> q = new PriorityQueue<ToDoItem>(); q.add(new ToDoItem('C', 4, "Empty trash")); q.add(new ToDoItem('A', 2, "Feed dog")); q.add(new ToDoItem('B', 7, "Feed bird")); q.add(new ToDoItem('C', 3, "Mow lawn")); q.add(new ToDoItem('A', 1, "Water lawn")); q.add(new ToDoItem('B', 1, "Feed cat")); while (!q.isEmpty()) { System.out.println(q.remove()); } Queue<ToDoItem> q1 = new PriorityQueue<ToDoItem>(1, Collections.reverseOrder()); q1.add(new ToDoItem('C', 4, "Empty trash")); q1.add(new ToDoItem('A', 2, "Feed dog")); q1.add(new ToDoItem('B', 7, "Feed bird")); q1.add(new ToDoItem('C', 3, "Mow lawn")); q1.add(new ToDoItem('A', 1, "Water lawn")); q1.add(new ToDoItem('B', 1, "Feed cat")); while (!q1.isEmpty()) { System.out.println(q1.remove()); } } }
2. C++ STL 版
STL中也有自带的priority_queue 而且可以有2种比较的方式,greater和less
priority_queue<node,vector<node>,greater<node> > queue1; priority_queue<node,vector<node>, less<node> > queue2;
其中比较可以通过operator还重载
friend bool operator > (const node &a,const node &b) { return (a.z > b.z); } friend bool operator < (const node &a,const node &b) { return (a.z < b.z); }
#include <iostream> #include <vector> #include <queue> using namespace std; struct node { int x; int y; int z; friend bool operator > (const node &a,const node &b) { return (a.z > b.z); } friend bool operator < (const node &a,const node &b) { return (a.z < b.z); } }; int main() { priority_queue<node,vector<node>,greater<node> > queue1; priority_queue<node,vector<node>, less<node> > queue2; node n[10]; for(int i = 0;i < 10;i ++) { n[i].x = i; n[i].y = 10 - i; n[i].z = i; queue1.push(n[i]); queue2.push(n[i]); } while(!queue1.empty()) { cout << queue1.top().x << " " << queue1.top().y <<" " << queue1.top().z << endl; queue1.pop(); } cout << endl; while(!queue2.empty()) { cout << queue2.top().x << " " << queue2.top().y << " " << queue2.top().z << endl; queue2.pop(); } cout << endl; return 0; }
还可以通过 自定义函数法,是一个 仿函数
#include <iostream> #include <queue> #include <vector> #include <functional> using namespace std; struct node { int x; int y; int z; }; struct node_greater_cmp { bool operator()(const node & a,const node & b) { return a.z>b.z; } }; struct node_less_cmp { bool operator()(const node &a,const node &b) { return a.z < b.z; } }; int main() { priority_queue<node,vector<node>,node_greater_cmp > queue1; priority_queue<node,vector<node>,node_less_cmp > queue2; node n[5]; for(int i = 0;i < 5; i ++) { n[i].x = i; n[i].y = 5 - i; n[i].z = i; queue1.push(n[i]); queue2.push(n[i]); } while(!queue1.empty()) { cout << queue1.top().x << " " << queue1.top().y <<" " << queue1.top().z << endl; queue1.pop(); } cout << endl; while(!queue2.empty()) { cout << queue2.top().x << " " << queue2.top().y << " " << queue2.top().z << endl; queue2.pop(); } cout << endl; return 0; }
3. 自己实现,以建堆的方式实现优先级队列
#include <iostream> using namespace std; const int INF = 999999; //堆调整 void maxHeapify(int *a, int i, int len) { int lt = 2*i, rt = 2*i+1; int largest; if(lt <= len && a[lt] > a[i]) largest = lt; else largest = i; if(rt <= len && a[rt] > a[largest]) largest = rt; if(largest != i) { int temp = a[i]; a[i] = a[largest]; a[largest] = temp; maxHeapify(a, largest, len); } } //建最大堆 void buildMaxHeap(int *a,int size) { for(int i = size / 2;i >= 1;i --) maxHeapify(a,i,size); } void print(int *a,int size) { for(int i = 1;i <= size; i ++) cout << a[i] << " "; cout << endl; } int heapMaximum(int *a) { return a[1]; } //提取并返回具有最大关键字的元素 int heapExtractMax(int *a,int &heapsize) { if(heapsize < 1) cout << "heap underflow !" << endl; int max = a[1]; a[1] = a[heapsize]; -- heapsize; maxHeapify(a,1,heapsize); return max; } //将a[i]增加到key,模拟优先级的提高 void heapIncreaseKey(int *a,int i,int key) { if(key < a[i]) cout << "new key is less than current key ! " << endl; a[i] = key; while(i > 1 && a[i/2] < a[i]) { int temp = a[i]; a[i] = a[i/2]; a[i/2] = temp; i /= 2; } } //插入关键字为key 的元素 void insert(int *a,int key,int &heapsize) { ++heapsize; a[heapsize] = -INF; heapIncreaseKey(a,heapsize,key); } int main() { int len,heapsize; int a[100] = {0, 15, 13, 9, 5, 12, 8, 7, 4, 0, 6, 2, 1}; len = heapsize = 12; buildMaxHeap(a,len); cout << "建堆后:" << endl; print(a,heapsize); cout << "当前最大的元素:" << endl; cout << heapMaximum(a) << endl; cout << "使用heapextractmax后:" << endl; int maxx = heapExtractMax(a,heapsize); print(a,heapsize); cout << "再次使用heapextractmax后:" << endl; heapExtractMax(a,heapsize); print(a,heapsize); cout << "使用heapIncreaceKey后:" << endl; heapIncreaseKey(a,2,15); print(a,heapsize); cout << "使用insert插入28后:" << endl; insert(a,28,heapsize); print(a,heapsize); cout << "使用insert插入100后:" << endl; insert(a,100,heapsize); print(a,heapsize); return 0; }
相关文章推荐
- 优先级队列 C++ 模板实现
- 【C++】优先级队列的模拟实现
- 队列的图文解析 和 对应3种语言的实现(C/C++/Java)
- java使用数组实现优先级队列
- C++ STL 有关容器部分 priority_queue 优先级队列的基本操作以及认识案例
- C++模板链表实现优先级队列
- 数据结构Java实现——②队列-->队列的“奇葩”二 优先级队列
- 两个栈实现一个队列——C++ 利用STL 栈stack实现
- STL中的优先级队列(priority_queue)的自己实现priqueue
- C++模板链表实现优先级队列
- c++之STL(8)优先级队列 priority_queue
- java基于有序链表的优先级队列实现
- java实现优先级队列
- C++模板链表实现优先级队列
- STL的堆算法实现优先级队列
- C++ 标准模板库STL 优先级队列 priority_queue 使用方法与应用介绍(一)
- STL中的优先级队列(priority_queue)的自己实现priqueue
- C++ STL之next_permutation小证明与Java实现
- C++STL优先级队列priority_queue
- 第6章 堆排序 java实现 简单版 泛型版 最大优先级队列