【数据结构】基于堆的优先级队列

前端之家收集整理的这篇文章主要介绍了【数据结构】基于堆的优先级队列前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. #include <iostream>
  2. #include <assert.h>
  3. #include<stdlib.h>
  4. #include <vector>
  5. using namespace std;
  6. template<typename T>
  7. struct Big
  8. {
  9. bool operator()(const T& l,const T& r)
  10. {
  11. return l>r;
  12. }
  13. };
  14. template<typename T>
  15. struct Less
  16. {
  17. bool operator()(const T& l,const T& r)
  18. {
  19. return l<r;
  20. }
  21. };
  22. template<class T,template<class> class Compare>
  23. class Heap//二叉堆
  24. {
  25.  
  26. public:
  27. Heap()
  28. {}
  29. void HeapSort(T* a,size_t size)
  30. {
  31. _a.reserve(size);
  32. for(size_t i=0;i<size;i++)
  33. {
  34. _a.push_back(a[i]);
  35. }
  36. for(int i=(_a.size()-2)/2;i>=0;--i)
  37. {
  38. Adjustdown(i);
  39. }
  40. print(_a,size);
  41. }
  42. void Adjustup(size_t child)
  43. {
  44. Compare<T> com;
  45. size_t parent =(child-1)/2;
  46. while(child>0)
  47. {
  48. if(com(_a[child],_a[parent]))
  49. {
  50. swap(_a[parent],_a[child]);
  51. child=parent;
  52. parent=(child-1)/2;
  53. }
  54. else
  55. {
  56. break;
  57. }
  58. }
  59. }
  60. void Adjustdown(size_t parent)
  61. {
  62.  
  63. size_t child =parent*2+1;
  64. while(child<_a.size())
  65. {
  66. Compare<T> com;
  67. if(child+1<_a.size()&&com(_a[child+1],_a[child]))
  68. {
  69. ++child;
  70. }
  71. if(com(_a[child],_a[parent]))
  72. {
  73. swap(_a[parent],_a[child]);
  74. parent=child;
  75. child=parent*2+1;
  76. }
  77. else
  78. {
  79. break;
  80. }
  81. }
  82. }
  83. bool Empty()
  84. {
  85. return _a.size()==0;
  86. }
  87. T& top()
  88. {
  89. assert(!_a.empty());
  90. return _a[0];
  91. }
  92. void Push(const T& x)
  93. {
  94. _a.push_back(x);
  95. size_t _size=_a.size();
  96. Adjustup(_size-1);
  97. print(_a,_size);
  98. }
  99. void Pop()
  100. {
  101. size_t _size=_a.size();
  102. assert(_size>0);
  103. swap(_a[0],_a[_size-1]);
  104. _a.pop_back();
  105. _size=_a.size();
  106. Adjustdown(0);
  107. print(_a,_size);
  108. }
  109. void print(vector<T> a,size_t size)
  110. {
  111.  
  112. for(int i=0;i<size;i++)
  113. {
  114. cout<<a[i]<<" ";
  115. }
  116. cout<<endl;
  117. }
  118. private:
  119. vector<T> _a;
  120. };
  121. template <class T,template<class> class Compare=Big>
  122. class PriorityQueue//优先级队列
  123. {
  124. public:
  125. void Push(const T& x)
  126. {
  127. hp.Push(x);
  128. }
  129. void Pop()
  130. {
  131. hp.Pop();
  132. }
  133. int Top()
  134. {
  135. return hp.top();
  136. }
  137. protected:
  138. Heap<T,Compare> hp;
  139. };
  140. void test()
  141. {
  142. int Tree[]={23,12,33,45,15,46,17,78,59};
  143. Heap<int,Big> h1;
  144. h1.HeapSort(Tree,sizeof(Tree)/sizeof(Tree[0]));
  145. }
  146. void test2()
  147. {
  148. int arr[] = { 12,10,43,23,22,67,9 };
  149. Heap<int,Big> h1;
  150. h1.HeapSort(arr,sizeof(arr)/sizeof(arr[0]));
  151. Heap<int,Less> h2;
  152. h2.HeapSort(arr,sizeof(arr)/sizeof(arr[0]));
  153. }
  154. void test3()
  155. {
  156. PriorityQueue<int,Big> pq;
  157. pq.Push(1);
  158. pq.Push(23);
  159. pq.Push(33);
  160. pq.Push(46);
  161. pq.Push(15);
  162. pq.Push(43);
  163. pq.Push(27);
  164. pq.Push(81);
  165. pq.Push(19);
  166. pq.Push(10);
  167. pq.Push(11);
  168. cout<<pq.Top()<<endl;
  169. }

猜你在找的数据结构相关文章