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