//
// main.cpp
// Max Heap
//
// Created by Himanshu on 02/10/21.
//
#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;
int main () {
vector<int> maxHeap = {30, 100, 25, 40};
make_heap(maxHeap.begin(), maxHeap.end());
cout<<"Insert H(x) {20, 70}"<<endl;
maxHeap.push_back(20);
push_heap(maxHeap.begin(), maxHeap.end());
maxHeap.push_back(70);
push_heap(maxHeap.begin(), maxHeap.end());
cout<<"Maximum Element (H)"<<endl;
cout<<maxHeap.front()<<endl;
cout<<"Maximum Element (H) after Extract-Max (H)"<<endl;
pop_heap(maxHeap.begin(), maxHeap.end());
maxHeap.pop_back();
cout<<maxHeap.front()<<endl;
cout <<"Extracting out elements..."<<endl;
while (!maxHeap.empty()) {
cout<< maxHeap.front()<<" ";
pop_heap(maxHeap.begin(), maxHeap.end());
maxHeap.pop_back();
}
cout<<endl;
if (maxHeap.empty()) {
cout<<"Heap is empty"<<endl;
} else {
cout<<"Heap is not empty"<<endl;
}
return 0;
}
Ly8KLy8gIG1haW4uY3BwCi8vICBNYXggSGVhcAovLwovLyAgQ3JlYXRlZCBieSBIaW1hbnNodSBvbiAwMi8xMC8yMS4KLy8KCgojaW5jbHVkZSA8aW9zdHJlYW0+CiNpbmNsdWRlIDxxdWV1ZT4KI2luY2x1ZGUgPGFsZ29yaXRobT4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCmludCBtYWluICgpIHsKICAgIAogICAgdmVjdG9yPGludD4gbWF4SGVhcCA9IHszMCwgMTAwLCAyNSwgNDB9OwogICAgbWFrZV9oZWFwKG1heEhlYXAuYmVnaW4oKSwgbWF4SGVhcC5lbmQoKSk7CiAgICAKICAgIGNvdXQ8PCJJbnNlcnQgSCh4KSB7MjAsIDcwfSI8PGVuZGw7CiAgICBtYXhIZWFwLnB1c2hfYmFjaygyMCk7CiAgICBwdXNoX2hlYXAobWF4SGVhcC5iZWdpbigpLCBtYXhIZWFwLmVuZCgpKTsKICAgIG1heEhlYXAucHVzaF9iYWNrK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