Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. /*insert(int value)
  2. shift_up(i) - needed for insert
  3. get_max - returns the max item, without removing it
  4. get_size() - return number of elements stored
  5. is_empty() - returns true if heap contains no elements
  6. extract_max - returns the max item, removing it
  7. shift_down(i) - needed for extract_max
  8. remove(i) - removes item at index x
  9. heapify - create a heap from an array of elements, needed for heap_sort
  10. heap_sort() - take an unsorted array and turn it into a sorted array in-place using a max heap*/
  11. #include <vector>
  12. #include <iostream>
  13. #include <algorithm>
  14. #include <climits>
  15. using namespace std;
  16. class heap{
  17. public:
  18. heap();
  19. heap(const heap& other);
  20. heap& operator=(heap src);
  21. ~heap();
  22. int parent(int i);
  23. int right_child(int i);
  24. int left_child(int i);
  25. void insert(int value);
  26. void shift_up(int i);
  27. int get_max();
  28. int get_size();
  29. bool is_empty();
  30. int extract_max();
  31. void shift_down(int i);
  32. void remove(int i);
  33. void heapify(const vector<int>&); //Build array into a heap
  34. void heap_sort(const vector<int>&);
  35. // private:
  36. vector<int> v;
  37. int size;
  38. // int max_size;
  39. };
  40. // only constructer is needed as we are using a built in library so no destructor
  41. heap::heap():size(0),v({0}){}
  42.  
  43. heap::heap(const heap& other){
  44. size = other.size;
  45. // max_size = other.max_size;
  46. v = other.v;
  47. }
  48.  
  49. heap& heap::operator=(heap src){
  50. swap(size, src.size);
  51. // swap(max_size, other.max_size);
  52. swap(v,src.v);
  53. }
  54.  
  55. heap::~heap(){}
  56.  
  57. int heap::parent(int i){
  58. return floor(i/2);
  59. }
  60. int heap::left_child(int i){
  61. return 2*i;
  62. }
  63. int heap::right_child(int i){
  64. return 2*i + 1;
  65. }
  66. void heap::insert(int value){
  67. v.push_back(value);
  68. size += 1;
  69. shift_up(size);
  70. }
  71. void heap::shift_up(int i){
  72. while (i > 1 && (v[parent(i)] < v[i])){
  73. swap(v[parent(i)], v[i]);
  74. i = parent(i);
  75. }
  76. }
  77.  
  78. int heap::get_max(){
  79. return v[1];
  80. }
  81.  
  82. int heap::get_size(){
  83. return size;
  84. }
  85. bool heap::is_empty(){
  86. (size>0)?true:false;
  87. }
  88. int heap::extract_max(){
  89.  
  90. int maxi = v[1];
  91.  
  92. v[1] = v[size];
  93.  
  94. size -= 1;
  95.  
  96. shift_down(1);
  97.  
  98. return maxi;
  99. }
  100. void heap::shift_down(int i){
  101. int maxIndex = i;
  102.  
  103. int l = left_child(i);
  104.  
  105. if(l <= size && v[maxIndex] < v[l])
  106. maxIndex = l;
  107.  
  108. int r = right_child(i);
  109.  
  110. if(r <= size && v[maxIndex] < v[r])
  111. maxIndex = r;
  112.  
  113. if (i != maxIndex)
  114. swap(v[i], v[maxIndex]);
  115.  
  116. shift_down(maxIndex);
  117. }
  118. void heap::remove(int value){
  119. int i = 0;
  120. int focus;
  121. while(i < size){
  122. if(v.at(i) == value){
  123. focus = i;
  124. break;
  125. }
  126. i += 1;
  127. }
  128. v[focus] = INT_MAX;
  129. shift_up(focus);
  130. extract_max();
  131. }
  132. void heap::heapify(const vector<int> &A){
  133. int j = 0;
  134. for(auto x: A){
  135. v[j] = x;
  136. j += 1;
  137. }
  138. for(int i = parent(A.size()); i >= 0; ++i){ //running time n
  139. shift_down(i);
  140. }
  141. }
  142. void heap::heap_sort(const vector<int> &A){
  143. heapify(A);
  144. while(size > 0){
  145. swap(v[0],v[size-1]);
  146. size -= 1;
  147. shift_down(0);
  148. }
  149. }
  150. int main(){
  151. heap h1;
  152. // cout << h1.v[0] << endl;
  153. h1.insert(23);
  154. h1.insert(43);
  155. cout << h1.get_max() << endl;
  156. /*h1.remove(43);
  157. cout << h1.get_max() << endl;*/
  158. /*h1.insert(34);
  159. h1.insert(93);
  160. h1.insert(31);
  161. h1.insert(21);
  162. h1.insert(78);
  163. cout << h1.get_max() << endl;*/
  164. h1.extract_max();
  165. cout << h1.get_max() << endl;
  166.  
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement