// Priority Queue implementation in C++ using Max Heap

#include <iostream>
#include <vector>
using namespace std;

// Function to swap position of two elements
void swap(int *a, int *b) {
  int temp = *b;
  *b = *a;
  *a = temp;
}

// Function to heapify the tree
void heapify(vector<int> &hT, int i) {
  int size = hT.size();
  
  // Find the largest among root, left child and right child
  int largest = i;
  int l = 2 * i + 1;
  int r = 2 * i + 2;
  if (l < size && hT[l] < hT[largest])
    largest = l;
  if (r < size && hT[r] < hT[largest])
    largest = r;

  // Swap and continue heapifying if root is not largest
  if (largest != i) {
    swap(&hT[i], &hT[largest]);
    heapify(hT, largest);
  }
}

// Function to insert an element into the tree
void insert(vector<int> &hT, int newNum) {
  int size = hT.size();
  if (size == 0) {
    hT.push_back(newNum);
  } else {
    hT.push_back(newNum);
    for (int i = size / 2 ; i >= 0; i--) {
      heapify(hT, i);
    }
  }
}

// Function to delete an element from the tree
void deleteNode(vector<int> &hT, int num) {
  int size = hT.size();
  int i;
  for (i = 0; i < size; i++) {
    if (num == hT[i])
      break;
  }
  swap(&hT[i], &hT[size - 1]);

  hT.pop_back();
  for (int i = size / 2 - 1; i >= 0; i--) {
    heapify(hT, i);
  }
}

// Print the tree
void printArray(vector<int> &hT) {
  for (int i = 0; i < hT.size(); ++i)
    cout << hT[i] << " ";
  cout << "\n";
}

// Driver code
int main() {
  vector<int> heapTree;
//35 33 42 10 14 19 27 44 26 31
  insert(heapTree, 35);
  insert(heapTree, 33);
  insert(heapTree, 42);
  insert(heapTree, 10);
  insert(heapTree, 14);
  insert(heapTree, 19);
  insert(heapTree, 27);
  insert(heapTree, 44);
  insert(heapTree, 26);
  insert(heapTree, 31);

  cout << "Max-Heap array: ";
  printArray(heapTree);

 // deleteNode(heapTree, 4);

  cout << "After deleting an element: ";

  printArray(heapTree);
}