ungureanuvladvictor

MinHeap

Nov 18th, 2013
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. #include <algorithm>
  2. #include <exception>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. #ifndef _MINHEAP_
  8. #define _MINHEAP_
  9.  
  10. template <class T>
  11. class MinHeap{
  12. private:
  13. const int initSize = 50;
  14. T *info;
  15. int size;
  16. int maxSize;
  17.  
  18. public:
  19. MinHeap();
  20. ~MinHeap();
  21. void MinHeapify(int position);
  22. bool add(T val);
  23. T extract();
  24. bool decreaseKey(int position, T newVal);
  25. T getVal(int position);
  26. };
  27.  
  28. template <class T>
  29. MinHeap<T>::MinHeap(){
  30. this->maxSize = this->initSize;
  31. this->info = new T[this->maxSize];
  32. this->size = 0;
  33. }
  34.  
  35. template <class T>
  36. MinHeap<T>::~MinHeap(){
  37. delete[] this->info;
  38. }
  39.  
  40. template <class T>
  41. bool MinHeap<T>::add(T data){
  42. if (this->size + 1 <= this->maxSize) {
  43. this->info[this->size] = data;
  44. this->size++;
  45. this->decreaseKey(this->size - 1, data);
  46. return true;
  47. }
  48. else {
  49. this->maxSize += this->maxSize;
  50. T *temp = new T[this->maxSize];
  51. copy(this->info, this->info + this->size - 1, temp);
  52. delete[] this->info;
  53. this->info = temp;
  54. this->info[this->size] = data;
  55. this->size++;
  56. this->decreaseKey(this->size - 1, data);
  57. return true;
  58. }
  59. return false;
  60. }
  61.  
  62. // Method that builts a mean heap starting from a node
  63. // position - the node to start from building.
  64. template <class T>
  65. void MinHeap<T>::MinHeapify(int position) {
  66. if (position < (this->size - 1) / 2) {
  67. int l = 2 * position + 1;
  68. int r = l + 1;
  69. int min;
  70.  
  71. if (l < this->size && this->info[l] <= this->info[position])
  72. min = l;
  73. else
  74. min = position;
  75.  
  76. if (r < this->size && this->info[min] >= this->info[r])
  77. min = r;
  78.  
  79. if (min != position)
  80. swap(this->info[min], this->info[position]);
  81.  
  82. MinHeapify(min);
  83. }
  84. }
  85.  
  86. // Method that extract an element
  87. // return - the elements that was extracted
  88. template <class T>
  89. T MinHeap<T>::extract() {
  90. if (this->size > 0) {
  91. T min = this->info[0];
  92.  
  93. this->info[0] = this->info[this->size - 1];
  94. this->size--;
  95. this->MinHeapify(0);
  96.  
  97. return min;
  98. }
  99. cout << "Heap is empty!";
  100. return -1;
  101. }
  102.  
  103. template <class T>
  104. bool MinHeap<T>::decreaseKey(int position, T newVal){
  105. if (position >= this->size || position < 0){
  106. cout << "Position out of bounds!" << endl;
  107. return false;
  108. }
  109.  
  110. if (this->info[position] < newVal) {
  111. cout << "Bad new value *bigger*" << endl;
  112. return false;
  113. }
  114.  
  115. this->info[position] = newVal;
  116. while (position > 0 && this->info[(position - 1) / 2] > this->info[position]) {
  117. swap(this->info[(position - 1) / 2], this->info[position]);
  118. position = (position - 1) / 2;
  119. }
  120.  
  121. return true;
  122. }
  123.  
  124. template <class T>
  125. T MinHeap<T>::getVal(int position){
  126. if (position > this->size || position <= 0){
  127. cout << "Position out of bounds!" << endl;
  128. return 0;
  129. }
  130. else {
  131. return this->info[position];
  132. }
  133. }
  134.  
  135.  
  136. #endif
Advertisement
Add Comment
Please, Sign In to add comment