`
holoblog
  • 浏览: 1224805 次
博客专栏
E0fcf0b7-6756-3051-9a54-90b4324c9940
SQL Server 20...
浏览量:18895
文章分类
社区版块
存档分类
最新评论

编程练习——堆heap

 
阅读更多

因为vs2005中c++的Heap构造有些问题,所以才重写了heap类。

不知道vs2008中这个是不是个bug。

  1. /*createdbychicochen
  2. *date2008/10/25
  3. */
  4. template<classT,classCmp>
  5. classHeap
  6. {
  7. private:
  8. vector<T>heap;
  9. voidShiftDown(intindex)
  10. {
  11. while(!IsLeaf(index))
  12. {
  13. intl=Lchild(index);
  14. intr=Rchild(index);
  15. if(r!=-1)
  16. {
  17. if(Cmp::Up(heap[r],heap[l]))
  18. {
  19. //puttheupnodetotheleft
  20. swap(heap[l],heap[r]);
  21. }
  22. }
  23. if(Cmp::Up(heap[l],heap[index]))
  24. {
  25. //uptheleftchild
  26. swap(heap[l],heap[index]);
  27. index=l;
  28. }
  29. else
  30. {
  31. break;
  32. }
  33. }
  34. }
  35. voidShiftUp(intindex)
  36. {
  37. intparent=-1;
  38. while(index!=0)
  39. {
  40. parent=Parent(index);
  41. if(Cmp::Up(heap[index],heap[parent]))
  42. {
  43. swap(heap[index],heap[parent]);
  44. index=parent;
  45. }
  46. else
  47. {
  48. break;
  49. }
  50. }
  51. }
  52. voidReHeapAll()
  53. {
  54. for(inti=Parent(heap.size()-1);i>=0;i--)
  55. {
  56. ShiftDown(i);
  57. }
  58. }
  59. voidReHeapOne(intindex)
  60. //setonepriorandre-positiontheindexnode
  61. {
  62. Tdata=heap[index];
  63. ShiftDown(index);
  64. if(Cmp::Eq(data,heap[index]))
  65. {
  66. ShiftUp(index);
  67. }
  68. }
  69. intParent(intindex)
  70. //parentnodeoftheindexnode
  71. {
  72. return(index-1)/2;
  73. }
  74. intLchild(intindex)
  75. //Leftchildoftheindexnode
  76. {
  77. if(!IsLeaf(index))
  78. returnindex*2+1;
  79. return-1;
  80. }
  81. intRchild(intindex)
  82. //Rightchildoftheindexnode
  83. {
  84. if(!IsLeaf(index))
  85. {
  86. intr=index*2+2;
  87. if(r<heap.size())
  88. returnr;
  89. }
  90. return-1;
  91. }
  92. intIsLeaf(intindex)
  93. {
  94. assert(index<heap.size());
  95. if(heap.size()==1)
  96. returntrue;
  97. intlastNode=Parent(heap.size()-1);
  98. returnlastNode<index;
  99. }
  100. intFindData(T&data)
  101. {
  102. intlen=heap.size();
  103. for(inti=0;i<len;i++)
  104. {
  105. if(Cmp::Eq(data,heap[i]))
  106. {
  107. returni;
  108. }
  109. }
  110. return-1;//cannotfindthedata
  111. }
  112. voidprintTree(intindex,intcount)
  113. {
  114. if(index<heap.size()&&index>=0)
  115. {
  116. printTree(Rchild(index),count+4);
  117. for(inti=0;i<count;i++)
  118. {
  119. cout<<"";
  120. }
  121. cout<<heap[index]<<endl;
  122. printTree(Lchild(index),count+4);
  123. }
  124. }
  125. public:
  126. Heap()
  127. {
  128. heap=vector<T>();
  129. }
  130. ~Heap()
  131. {
  132. //heapdestroyed
  133. }
  134. boolIsEmpty()
  135. {
  136. returnheap.size()==0;
  137. }
  138. voidInsert(T&data)
  139. {
  140. heap.push_back(data);
  141. ShiftUp(heap.size()-1);
  142. }
  143. voidDelete(intindex)
  144. {
  145. intlast=heap.size()-1;
  146. if(index>=0&&index<=last)
  147. {
  148. swap(heap[index],heap[last]);
  149. heap.pop_back();
  150. ReHeapOne(index);
  151. }
  152. }
  153. voidDelete(T&data)
  154. {
  155. intindex=FindData(data);
  156. if(index!=-1)
  157. Delete(index);
  158. }
  159. voidRemoveTop()
  160. {
  161. if(!IsEmpty()&&heap.size()>1)
  162. {
  163. swap(heap[0],heap[heap.size()-1]);
  164. heap.pop_back();
  165. ShiftDown(0);
  166. }
  167. elseif(heap.size()==1)
  168. {
  169. heap.pop_back();
  170. }
  171. }
  172. T&Top()
  173. {
  174. returnheap[0];
  175. }
  176. voidPrint()
  177. {
  178. printTree(0,1);
  179. }
  180. };
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics