算法导论 第六章:优先级队列
2015-07-14 19:10
399 查看
虽然堆排序的时间复杂度为O(nlgn),但在实际中,快速排序(下一章将介绍)往往要优于堆排序。尽管如此,堆数据结构在其他方面有着很大的用处,如用于优先级队列的实现。因为堆可让优先级队列的所有操作的时间复杂度为O(lgn)。最大优先级队列常用于共享主机上的作业调度,最小优先级队列常用于事件驱动的模拟中。
以最大优先级队列为例,其支持的操作如下:
1)INSERT(S,x): 向集合中插入元素x
![](https://oscdn.geek-share.com/Uploads/Images/Content/201507/14/94b2b83e93eddc619eab6036a8b563f1)
2)MAXIMUM(S): 返回集合S的最大值
![](https://oscdn.geek-share.com/Uploads/Images/Content/201507/14/1c54f015e2791fbc72bbeae74f10965f)
3)EXTRACT-MAX(S):删除集合S的最大值
![](https://oscdn.geek-share.com/Uploads/Images/Content/201507/14/58c1b130ff1ac18602e4357377731bd1)
4)INCREASE-KEY(S,x,k):将集合S中的元素x的值增加为k
![](https://oscdn.geek-share.com/Uploads/Images/Content/201507/14/0f5b63536dd42b56ae435be0822e8780)
最大优先级队列实现的完整代码如下:
运行结果如下:
![](https://oscdn.geek-share.com/Uploads/Images/Content/201507/14/3092788061b2087c5ffa7ac4d8461e02)
若有错误,请指正~~~
以最大优先级队列为例,其支持的操作如下:
1)INSERT(S,x): 向集合中插入元素x
2)MAXIMUM(S): 返回集合S的最大值
3)EXTRACT-MAX(S):删除集合S的最大值
4)INCREASE-KEY(S,x,k):将集合S中的元素x的值增加为k
最大优先级队列实现的完整代码如下:
#include<iostream> #include<cstdlib> #include<climits> #define PARENT(i) (i/2) #define LEFT(i) (2*i) #define RIGHT(i) (2*i+1) using namespace std; void Print(int *a) { int n=a[0]; for(int i=1;i<=n;i++) cout<<a[i]<<" "; cout<<endl; } int *Transform(int *a,int n) { int * A=new int[n+1]; A[0]=n; for(int i=0;i<n;i++) A[i+1]=a[i]; return A; } void swap(int &a,int &b) { int temp; temp=a; a=b; b=temp; } void Max_Heapify(int *A,int i) {//adjust the heap to maintain the heap property int heap_size=A[0]; int l=LEFT(i) ; //index of i's left child int r=RIGHT(i); int largest; //At each step,the largest of the element A[i],A[LEFT(i)] //A[RIGHT(i)] is determined,and its index is stored in largest. if(l<=heap_size && A[l]>A[i]) largest = l; else largest = i; if(r<=heap_size && A[r]>A[largest]) largest = r; if(largest!=i) //the subtree rooted at i violate the max-heap property { swap(A[i],A[largest]); Max_Heapify(A,largest); //after exchanging ,the subtree rooted at largest might violate the max-heap property } } void Build_MaxHeap(int *A) {//Transform array A into max-heap A,where A[0]=heap-size int A_length=A[0]; for(int i=A_length/2;i>=1;i--) Max_Heapify(A,i); } int GetHeapMaximum(int *A) {//return the elements of S with the largest key Build_MaxHeap(A); return A[1]; } int Heap_ExtractMax(int *A) { int max; int heap_size; heap_size=A[0]; if(heap_size<1) { cout<<"heap underflow"<<endl; return -1; } Build_MaxHeap(A); max=A[1]; A[1]=A[heap_size]; heap_size--; A[0]=heap_size; Max_Heapify(A,1); return max; } void Heap_IncreaseKey(int *A,int i,int key) { Build_MaxHeap(A); if(key<A[i]){ cout<<"new key is smaller than the current key."<<endl; return; } A[i]=key; while(i>1 && A[PARENT(i)]<A[i]) { swap(A[PARENT(i)],A[i]); i=PARENT(i); } } void Heap_InsertKey(int *A,int key) { int heap_size=A[0]; heap_size=heap_size+1; A[0]=heap_size; A[heap_size]=INT_MIN; Heap_IncreaseKey(A,heap_size,key); } int main(){ int a[]={4,1,3,2,16,9,10,14,8,7}; int n=sizeof(a)/sizeof(int); int *A=new int[n+1]; cout<<"---------------Init the set--------------------"<<endl; cout<<"After initing,S[1..n] is:"<<endl; A=Transform(a,n); //Transform a[0...n-1] into a[1...n];a[0]=a.length Print(A); cout<<"---------------Excute MAXIMUM------------------"<<endl; int hMaxN; hMaxN=GetHeapMaximum(A); cout<<"The largest key is:"<<hMaxN<<endl; cout<<"------------Excute EXTRACT-MAX-----------------"<<endl; hMaxN=Heap_ExtractMax(A); cout<<"The largest key is:"<<hMaxN<<endl; cout<<"After removing the largest,the heap is:"<<endl; Print(A); cout<<"------------Excute INCREASE-KEY----------------"<<endl; int key=11; int index=6; Heap_IncreaseKey(A,index,key); cout<<"After increasing ,the heap is:"<<endl; Print(A); cout<<"--------------Excute HEAP-INSERT---------------"<<endl; int ikey=13; Heap_InsertKey(A,ikey); Print(A); cout<<"-----------------------------------------------"<<endl; return 0; }
运行结果如下:
若有错误,请指正~~~
相关文章推荐
- 一看就懂:图解C#中的值类型、引用类型、栈、堆、ref、out
- python计算最大优先级队列实例
- 浅析栈区和堆区内存分配的区别
- PHP SPL标准库之数据结构堆(SplHeap)简单使用实例
- 浅谈C#中堆和栈的区别(附上图解)
- Java中堆和栈的区别详解
- java算法导论之FloydWarshall算法实现代码
- java中堆和栈的区别分析
- C++中静态存储区与栈以及堆的区别详解
- 大话JMM
- java 虚拟机--新生代与老年代GC
- java堆内存与栈内存
- 堆和栈的区别
- 转贴一份挺好的堆和栈区别的资料
- C语言之内存分配
- Red-Black Tree 的Java实现
- 逆序对
- 算法导论之堆
- 堆 栈
- LCS 最长公共子序列