#include <bits/stdc++.h>
using namespace std;
class MinHeap {
public:
int *arr;
int size{0};
int capacity{1000};
MinHeap() {
arr = new int[capacity]{};
}
~MinHeap() {
delete[] arr;
arr = nullptr;
}
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;
}
void heapifyUp(int child_pos) {
int parent_pos = parent(child_pos);
if (child_pos == 0 || parent_pos == -1 || arr[parent_pos] < arr[child_pos]) {
return;
}
swap(arr[parent_pos], arr[child_pos]);
heapifyUp(parent_pos);
}
void push(int node) {
assert(!isFull());
arr[size++] = node;
heapifyUp(size - 1);
}
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);
}
}
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;
}
void print_less_than(int val, int parent_pos = 0) {
if (parent_pos >= size || arr[parent_pos] >= val) {
return;
}
cout << arr[parent_pos] << " ";
int left = leftchild(parent_pos);
int right = rightchild(parent_pos);
if (left != -1) {
print_less_than(val, left);
}
if (right != -1) {
print_less_than(val, right);
}
}
};
int main() {
MinHeap heap;
heap.push(7);
heap.push(1);
heap.push(5);
heap.push(2);
heap.push(3);
cout << "The heap traversal is:\n";
while (!heap.isEmpty()) {
cout << heap.top() << " ";
heap.pop();
}
cout << '\n';
MinHeap mn;
mn.push(1);
mn.push(5);
mn.push(2);
mn.push(3);
mn.push(4);
mn.push(6);
cout << "Print elements less than 6:\n";
mn.print_less_than(6, 0);
cout << '\n';
return 0;
}