[关闭]
@qiezhian 2014-11-15T08:44:35.000000Z 字数 2058 阅读 1571

二叉堆(最大堆和最小堆)

programming


  1. /*
  2. Filename: heap.cpp
  3. Function: 简单实现二叉堆结构,可实现最大堆和最小堆。
  4. Details: 调用FindTop()及DeleteTop()函数前请先check
  5. 一下IsEmpty()。
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <memory.h>
  10. #define DEFAULT_CAPACITY (10)
  11. template<typename T>
  12. class BinaryHeap
  13. {
  14. public:
  15. BinaryHeap( );
  16. BinaryHeap( int nCapacity, int nMode );
  17. ~BinaryHeap( );
  18. void Insert( T element );
  19. void MakeEmpty( );
  20. T DeleteTop( );
  21. T FindTop( );
  22. bool IsEmpty();
  23. bool IsFull();
  24. void Destroy();
  25. private:
  26. T *Elements;
  27. int Capacity;
  28. int Size;
  29. /*
  30. mode==0:min; mode==1:max
  31. */
  32. int mode;
  33. };
  34. template<typename T>
  35. BinaryHeap<T>::BinaryHeap( )
  36. {
  37. Capacity = DEFAULT_CAPACITY;
  38. Size = 0;
  39. mode = 0;
  40. Elements = (T*) malloc( Capacity*sizeof(T) );
  41. if( Elements==NULL )
  42. printf("Out of Spaces!\n");
  43. }
  44. template<typename T>
  45. BinaryHeap<T>::BinaryHeap( int nCapacity, int nMode )
  46. {
  47. Capacity = nCapacity;
  48. Size = 0;
  49. mode = nMode;
  50. Elements = (T*) malloc( nCapacity*sizeof(T) );
  51. if( Elements==NULL )
  52. printf( "Out of Spaces!\n" );
  53. }
  54. template<typename T>
  55. BinaryHeap<T>::~BinaryHeap()
  56. {
  57. Destroy();
  58. }
  59. /*
  60. */
  61. template<typename T>
  62. void BinaryHeap<T>::Destroy()
  63. {
  64. if( Elements!=NULL )
  65. free( Elements );
  66. }
  67. template<typename T>
  68. bool BinaryHeap<T>::IsEmpty()
  69. {
  70. return ( Size==0 );
  71. }
  72. template<typename T>
  73. bool BinaryHeap<T>::IsFull()
  74. {
  75. return ( Size==Capacity );
  76. }
  77. template<typename T>
  78. void BinaryHeap<T>::Insert( T element )
  79. {
  80. if( IsFull() )
  81. return;
  82. int i;
  83. if( mode==0 )
  84. for( i=++Size-1; i>0 && Elements[i]>element; i=(i-1)/2 )
  85. Elements[i] = Elements[(i-1)/2];
  86. else if( mode==1 )
  87. for( i=++Size-1; i>0 && Elements[i]<element; i=(i-1)/2 )
  88. Elements[i] = Elements[(i-1)/2];
  89. Elements[i] = element;
  90. }
  91. template<typename T>
  92. T BinaryHeap<T>::FindTop()
  93. {
  94. return Elements[0];
  95. }
  96. template<typename T>
  97. T BinaryHeap<T>::DeleteTop()
  98. {
  99. T minElement = Elements[0];
  100. T lastElement = Elements[--Size];
  101. int i, child;
  102. if( mode==0 )
  103. for( i=0; i*2+1<Size; i=child )
  104. {
  105. child = i*2+1;
  106. if( child!=Size-1 && Elements[child]>Elements[child+1] )
  107. child++;
  108. if( Elements[child]<lastElement )
  109. Elements[i] = Elements[child];
  110. else
  111. break;
  112. }
  113. else if( mode==1 )
  114. for( i=0; i*2+1<Size; i=child )
  115. {
  116. child = i*2+1;
  117. if( child!=Size-1 && Elements[child]<Elements[child+1] )
  118. child++;
  119. if( Elements[child]>lastElement )
  120. Elements[i] = Elements[child];
  121. else
  122. break;
  123. }
  124. Elements[i] = lastElement;
  125. return minElement;
  126. }
  127. template<typename T>
  128. void BinaryHeap<T>::MakeEmpty()
  129. {
  130. Size = 0;
  131. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注