【数据结构】优先级队列的实现

前端之家收集整理的这篇文章主要介绍了【数据结构】优先级队列的实现前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

建立PriorityQueue.hpp:

  1. #define_CRT_SECURE_NO_WARNINGS1
  2. #include<iostream>
  3. usingnamespacestd;
  4. #include<assert.h>
  5. #include<vector>
  6.  
  7.  
  8. template<classT>
  9. structLess
  10. {
  11. booloperator()(constT&l,constT&r)
  12. {
  13. returnl<r;
  14. }
  15. };
  16.  
  17. template<classT>
  18. structGreater
  19. {
  20. booloperator()(constT&l,constT&r)
  21. {
  22. returnl>r;
  23. }
  24. };
  25.  
  26.  
  27. template<classT,template<class>classCompare=Less>
  28. classHeap
  29. {
  30. public:
  31. Heap()
  32. :_a(NULL)
  33. {}
  34.  
  35.  
  36. Heap(constT*a,size_tsize)
  37. {
  38. for(inti=0;i<size;i++)
  39. {
  40. _a.push_back(a[i]);
  41. }
  42. for(inti=(size-2)/2;i>=0;i--)
  43. {
  44. _adjustDown(i);
  45. }
  46. }
  47.  
  48.  
  49. void_adjustDown(size_tparent)
  50. {
  51. Compare<T>com;
  52. size_tchild=2*parent+1;
  53. size_tsize=_a.size();
  54. while(child<size)
  55. {
  56. if(child+1<size&&com(_a[child+1],_a[child]))
  57. {
  58. ++child;
  59. }
  60. if(com(_a[child],_a[parent]))
  61. {
  62. swap(_a[child],_a[parent]);
  63. parent=child;
  64. child=2*parent+1;
  65. }
  66. else
  67. {
  68. break;
  69. }
  70. }
  71. }
  72.  
  73.  
  74. voidPush(constT&x)
  75. {
  76. _a.push_back(x);
  77. _adjustUp(_a.size()-1);
  78. }
  79.  
  80.  
  81. void_adjustUp(size_tchild)
  82. {
  83. intparent=(child-1)/2;
  84. Compare<T>com;
  85. while(child>0)
  86. {
  87. if(com(_a[child],_a[parent]))
  88. {
  89. swap(_a[parent],_a[child]);
  90. child=parent;
  91. parent=(child-1)/2;
  92. }
  93. else
  94. {
  95. break;
  96. }
  97. }
  98. }
  99.  
  100.  
  101. size_tSize()
  102. {
  103. size_tsize=_a.size();
  104. returnsize;
  105. }
  106.  
  107.  
  108. boolEmpty()
  109. {
  110. assert(Size()>=0);
  111. returnSize()==0;
  112. }
  113.  
  114.  
  115. voidPop()
  116. {
  117. assert(_a.Size()>0);
  118. swap(_a[0],_a[Size-1]);
  119. _a.pop_back();
  120. _adjustDown(0);
  121. }
  122.  
  123.  
  124. T&GetTop()
  125. {
  126. return_a[0];
  127. }
  128.  
  129.  
  130. voidPrint()
  131. {
  132. cout<<"大堆序列:"<<endl;
  133. for(inti=0;i<_a.size();i++)
  134. {
  135. cout<<_a[i]<<"";
  136. }
  137. cout<<endl;
  138. }
  139. public:
  140. vector<T>_a;
  141. };
  142.  
  143.  
  144. template<classT,template<class>classCompare=Less>
  145. classPriorityQueue
  146. {
  147. public:
  148. voidPush(constT&x)
  149. {
  150. _hp.Push(x);
  151. }
  152.  
  153.  
  154. voidPop()
  155. {
  156. _hp.Pop();
  157. }
  158.  
  159.  
  160. size_tSize()
  161. {
  162. return_hp.Size();
  163. }
  164.  
  165.  
  166. boolEmpty()
  167. {
  168. _hp.Empty();
  169. }
  170.  
  171.  
  172. T&Top()
  173. {
  174. return_hp.GetTop();
  175. }
  176.  
  177.  
  178. voidPrint()
  179. {
  180. _hp.Print();
  181. }
  182. private:
  183. Heap<T,Compare>_hp;
  184. };

建立PriorityQueue.cpp文件

  1. #define_CRT_SECURE_NO_WARNINGS1
  2. #pragmaonce
  3. #include"PriorityQueue.hpp"
  4.  
  5. voidTest()
  6. {
  7. inta[]={10,11,13,12,16,18,15,17,14,19};
  8. PriorityQueue<int,Greater>pq;
  9. pq.Push(10);
  10. pq.Push(11);
  11. pq.Push(13);
  12. pq.Push(12);
  13. pq.Push(16);
  14. pq.Push(18);
  15. pq.Push(15);
  16. pq.Push(17);
  17. pq.Push(14);
  18. pq.Push(19);
  19.  
  20. pq.Print();
  21. }
  22.  
  23.  
  24. intmain()
  25. {
  26. Test();
  27. system("pause");
  28. return0;
  29. }

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