fork download
  1. //
  2. // main.cpp
  3. // Min Heap
  4. //
  5. // Created by Himanshu on 02/10/21.
  6. //
  7.  
  8.  
  9. #include <iostream>
  10. #include <queue>
  11. #include <algorithm>
  12. using namespace std;
  13.  
  14. int main () {
  15.  
  16. vector<int> minHeap = {30, 100, 25, 40};
  17. make_heap(minHeap.begin(), minHeap.end(), greater<int>());
  18.  
  19. cout<<"Insert H(x) {20, 70}"<<endl;
  20. minHeap.push_back(20);
  21. push_heap(minHeap.begin(), minHeap.end(), greater<int>());
  22. minHeap.push_back(70);
  23. push_heap(minHeap.begin(), minHeap.end(), greater<int>());
  24.  
  25. cout<<"Minimum Element (H)"<<endl;
  26. cout<<minHeap.front()<<endl;
  27.  
  28. cout<<"Minimum Element (H) after Extract-Min (H)"<<endl;
  29. pop_heap(minHeap.begin(), minHeap.end(), greater<int>());
  30. minHeap.pop_back();
  31. cout<<minHeap.front()<<endl;
  32.  
  33. cout <<"Extracting out elements..."<<endl;
  34. while (!minHeap.empty()) {
  35. cout<< minHeap.front()<<" ";
  36. pop_heap(minHeap.begin(), minHeap.end(), greater<int>());
  37. minHeap.pop_back();
  38. }
  39. cout<<endl;
  40.  
  41. if (minHeap.empty()) {
  42. cout<<"Heap is empty"<<endl;
  43. } else {
  44. cout<<"Heap is not empty"<<endl;
  45. }
  46.  
  47. return 0;
  48. }
  49.  
  50.  
Success #stdin #stdout 0s 5500KB
stdin
Standard input is empty
stdout
Insert H(x) {20, 70}
Minimum Element (H)
20
Minimum Element (H) after Extract-Min (H)
25
Extracting out elements...
25 30 40 70 100 
Heap is empty