fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. class MinHeap{
  5.  
  6. public:
  7. int *arr;
  8. int size{0};
  9. int capacity{1000};
  10.  
  11. public:
  12. MinHeap(){
  13. arr = new int[capacity];
  14. size = 0;
  15. }
  16. ~MinHeap(){
  17. delete[] arr;
  18. arr = NULL;
  19. size = 0;
  20. }
  21. //know my parent and my children
  22. int leftchild(int node){
  23. int ans = 2*node + 1;
  24. return ans >= size? -1 : ans;
  25. }
  26. int rightchild(int node){
  27. int ans = 2*node + 2;
  28. return ans >= size? -1 : ans;
  29. }
  30. int parent(int node){
  31. return node ==0 ? -1 : (node-1)/2;
  32. }
  33. //heapify up
  34. void heapifyUp(int child_pos){
  35. int parent_pos = parent(child_pos);
  36. //base case
  37. if(child_pos ==0||parent_pos==-1 || arr[parent_pos] <arr[child_pos]){
  38. return;
  39. }
  40. swap(arr[parent_pos],arr[child_pos]);
  41. heapifyUp(parent_pos);
  42. }
  43. //push data
  44. //Time O(log n)
  45. void push(int node){
  46. assert(!isFull());
  47. arr[size++] = node;
  48. heapifyUp(size-1);
  49. }
  50. //pop data
  51. void heapifyDown(int parent_pos){
  52. int left_child = leftchild(parent_pos);
  53. int right_child = rightchild(parent_pos);
  54.  
  55. int min_child = parent_pos;
  56.  
  57. if(left_child!= -1 && arr[left_child] < arr[min_child]){
  58. min_child = left_child;
  59. }
  60.  
  61. if(right_child!= -1 && arr[right_child] < arr[min_child]){
  62. min_child = right_child;
  63. }
  64.  
  65. if(min_child!= parent_pos){
  66. swap(arr[parent_pos],arr[min_child]);
  67. heapifyDown(min_child);
  68. }
  69.  
  70. }
  71. //Time O(log n)
  72. void pop(){
  73. assert(!isEmpty());
  74. swap(arr[0],arr[--size]);
  75. heapifyDown(0);
  76. }
  77. int top(){
  78. assert(!isEmpty());
  79. return arr[0];
  80. }
  81. bool isEmpty(){
  82. return size==0;
  83. }
  84. bool isFull(){
  85. return size==capacity;
  86. }
  87.  
  88.  
  89. };
  90. class MinHeap2 :public MinHeap{
  91. public:
  92. int size{};
  93. int capacity{1000};
  94. int *arr;
  95. //floyed approach
  96. MinHeap2(vector<int> &v){
  97. arr = new int[capacity];
  98. size = v.size();
  99. //O(n)
  100. for(int i=0;i<size;i++){
  101. arr[i] = v[i];
  102. }
  103. //O(n)
  104. //the position size/2 -1 is the first non leaf node
  105. for(int i=size/2-1;i>=0;i--){
  106. heapifyDown(i);
  107. }
  108. }
  109. void test_mx_heap(){
  110. priority_queue<int>mx_heap;
  111. //// will be mn heap ////
  112. /*
  113.   priority_queue<int,vector<int>,greater<int>>mn_heap;
  114.   */
  115. mx_heap.push(1);
  116. mx_heap.push(2);
  117. mx_heap.push(10);
  118. mx_heap.push(4);
  119. mx_heap.push(6);
  120. while(!mx_heap.empty()){
  121. cout << mx_heap.top() << " ";
  122. mx_heap.top();
  123. }
  124. cout <<'\n';
  125. }
  126. };
  127. class MaxHeaphw{
  128. private:
  129. MinHeap minheap;
  130. public:
  131. MaxHeaphw(const vector<int> &v){
  132. for(auto &x:v){
  133. minheap.push(-x);
  134. }
  135. }
  136. bool empty(){
  137. return minheap.isEmpty();
  138. }
  139. int top(){
  140. return -minheap.top();
  141. }
  142. void pop(){
  143. minheap.pop();
  144. }
  145. void push(int value){
  146. minheap.push(-value);
  147. }
  148. bool isEmpty(){
  149. return minheap.isEmpty();
  150. }
  151. bool isFull(){
  152. return minheap.isFull();
  153. }
  154.  
  155. };
  156. int main(){
  157. // MinHeap heap;
  158. // heap.push(7);
  159. // heap.push(1);
  160. // heap.push(5);
  161. // heap.push(2);
  162. // heap.push(3);
  163.  
  164. // //O(nlogn)
  165. // cout <<"The heap traversal is\n";
  166. // while (!heap.isEmpty()) {
  167. // cout << heap.top() << " ";
  168. // heap.pop();
  169. // }
  170. vector<int>v{1,11,3,4,6,7};
  171. MaxHeaphw mx(v);
  172. while(!mx.empty()){
  173. cout<<mx.top()<<" ";
  174. mx.pop();
  175. }
  176. return 0;
  177. }
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
11 7 6 4 3 1