fork download
  1. //
  2. // main.cpp
  3. // Max 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> maxHeap = {30, 100, 25, 40};
  17. make_heap(maxHeap.begin(), maxHeap.end());
  18.  
  19. cout<<"Insert H(x) {20, 70}"<<endl;
  20. maxHeap.push_back(20);
  21. push_heap(maxHeap.begin(), maxHeap.end());
  22. maxHeap.push_back(70);
  23. push_heap(maxHeap.begin(), maxHeap.end());
  24.  
  25. cout<<"Maximum Element (H)"<<endl;
  26. cout<<maxHeap.front()<<endl;
  27.  
  28. cout<<"Maximum Element (H) after Extract-Max (H)"<<endl;
  29. pop_heap(maxHeap.begin(), maxHeap.end());
  30. maxHeap.pop_back();
  31. cout<<maxHeap.front()<<endl;
  32.  
  33. cout <<"Extracting out elements..."<<endl;
  34. while (!maxHeap.empty()) {
  35. cout<< maxHeap.front()<<" ";
  36. pop_heap(maxHeap.begin(), maxHeap.end());
  37. maxHeap.pop_back();
  38. }
  39. cout<<endl;
  40.  
  41. if (maxHeap.empty()) {
  42. cout<<"Heap is empty"<<endl;
  43. } else {
  44. cout<<"Heap is not empty"<<endl;
  45. }
  46.  
  47. return 0;
  48. }
  49.  
Success #stdin #stdout 0s 5684KB
stdin
Standard input is empty
stdout
Insert H(x) {20, 70}
Maximum Element (H)
100
Maximum Element (H) after Extract-Max (H)
70
Extracting out elements...
70 40 30 25 20 
Heap is empty