#include <iostream>
#include <cmath>
using namespace std;
class Heap {
private:
int *heap;
int capacity;
void heapify() {
int lastParentPos = floor(capacity/2);
for(int i = lastParentPos; i >= 0; --i) {
int parentPos = i;
while(parentPos <= lastParentPos) {
int childPos = 2 * parentPos;
int maxPos = childPos;
if ((childPos + 1 <= capacity) && heap[childPos + 1] > heap[maxPos]) {
maxPos = childPos + 1;
}
if (heap[maxPos] > heap[parentPos]) {
int temp = heap[maxPos];
heap[maxPos] = heap[parentPos];
heap[parentPos] = temp;
parentPos = maxPos;
}
else {
break;
}
}
}
}
public:
Heap(int arr[], int length) {
capacity = length;
heap = new int[capacity + 1];
for (int i = 0; i < length; ++i) {
heap[i+1] = arr[i];
}
heapify();
}
print() {
for (int i = 1; i <= capacity; ++i) {
cout << heap[i]<< " ";
}
cout << endl;
}
};
int main() {
int arr[] = {2, 9, 7, 6, 5, 8};
Heap *heap = new Heap(arr, sizeof(arr)/sizeof(int));
heap->print();
delete heap;
}