#include<bits/stdc++.h>
using namespace std;
class MinHeap{
public:
int *arr;
int size{0};
int capacity{1000};
public:
MinHeap(){
arr = new int[capacity];
size = 0;
}
~MinHeap(){
delete[] arr;
arr = NULL;
size = 0;
}
//know my parent and my children
int leftchild(int node){
int ans = 2*node + 1;
return ans >= size? -1 : ans;
}
int rightchild(int node){
int ans = 2*node + 2;
return ans >= size? -1 : ans;
}
int parent(int node){
return node ==0 ? -1 : (node-1)/2;
}
//heapify up
void heapifyUp(int child_pos){
int parent_pos = parent(child_pos);
//base case
if(child_pos ==0||parent_pos==-1 || arr[parent_pos] <arr[child_pos]){
return;
}
swap(arr[parent_pos],arr[child_pos]);
heapifyUp(parent_pos);
}
//push data
//Time O(log n)
void push(int node){
assert(!isFull());
arr[size++] = node;
heapifyUp(size-1);
}
//pop data
void heapifyDown(int parent_pos){
int left_child = leftchild(parent_pos);
int right_child = rightchild(parent_pos);
int min_child = parent_pos;
if(left_child!= -1 && arr[left_child] < arr[min_child]){
min_child = left_child;
}
if(right_child!= -1 && arr[right_child] < arr[min_child]){
min_child = right_child;
}
if(min_child!= parent_pos){
swap(arr[parent_pos],arr[min_child]);
heapifyDown(min_child);
}
}
//Time O(log n)
void pop(){
assert(!isEmpty());
swap(arr[0],arr[--size]);
heapifyDown(0);
}
int top(){
assert(!isEmpty());
return arr[0];
}
bool isEmpty(){
return size==0;
}
bool isFull(){
return size==capacity;
}
};
class MinHeap2 :public MinHeap{
public:
int size{};
int capacity{1000};
int *arr;
//floyed approach
MinHeap2(vector<int> &v){
arr = new int[capacity];
size = v.size();
//O(n)
for(int i=0;i<size;i++){
arr[i] = v[i];
}
//O(n)
//the position size/2 -1 is the first non leaf node
for(int i=size/2-1;i>=0;i--){
heapifyDown(i);
}
}
void test_mx_heap(){
priority_queue<int>mx_heap;
//// will be mn heap ////
/*
priority_queue<int,vector<int>,greater<int>>mn_heap;
*/
mx_heap.push(1);
mx_heap.push(2);
mx_heap.push(10);
mx_heap.push(4);
mx_heap.push(6);
while(!mx_heap.empty()){
cout << mx_heap.top() << " ";
mx_heap.top();
}
cout <<'\n';
}
};
class MaxHeaphw{
private:
MinHeap minheap;
public:
MaxHeaphw(const vector<int> &v){
for(auto &x:v){
minheap.push(-x);
}
}
bool empty(){
return minheap.isEmpty();
}
int top(){
return -minheap.top();
}
void pop(){
minheap.pop();
}
void push(int value){
minheap.push(-value);
}
bool isEmpty(){
return minheap.isEmpty();
}
bool isFull(){
return minheap.isFull();
}
};
int main(){
// MinHeap heap;
// heap.push(7);
// heap.push(1);
// heap.push(5);
// heap.push(2);
// heap.push(3);
// //O(nlogn)
// cout <<"The heap traversal is\n";
// while (!heap.isEmpty()) {
// cout << heap.top() << " ";
// heap.pop();
// }
vector<int>v{1,11,3,4,6,7};
MaxHeaphw mx(v);
while(!mx.empty()){
cout<<mx.top()<<" ";
mx.pop();
}
return 0;
}