import java.util.*;
class MinHeap {
private int[] heapArray;
private int capacity;
private int current_heap_size;
public MinHeap(int n) {
capacity = n;
heapArray = new int[capacity];
current_heap_size = 0;
}
private void swap(int[] arr, int a, int b) {
int temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
private int parent(int key) {
return (key - 1) / 2;
}
private int left(int key) {
return 2 * key + 1;
}
private int right(int key) {
return 2 * key + 2;
}
public boolean insertKey(int key) {
if (current_heap_size == capacity) {
return false;
}
int i = current_heap_size;
heapArray[i] = key;
current_heap_size++;
while (i != 0 && heapArray[i] < heapArray[parent(i)]) {
swap(heapArray, i, parent(i));
i = parent(i);
}
return true;
}
public void decreaseKey(int key, int new_val) {
heapArray[key] = new_val;
while (key != 0 && heapArray[key] < heapArray[parent(key)]) {
swap(heapArray, key, parent(key));
key = parent(key);
}
}
public int getMin() {
return heapArray[0];
}
public int extractMin() {
if (current_heap_size <= 0) {
return Integer.MAX_VALUE;
}
if (current_heap_size == 1) {
current_heap_size--;
return heapArray[0];
}
int root = heapArray[0];
heapArray[0] = heapArray[current_heap_size - 1];
current_heap_size--;
MinHeapify(0);
return root;
}
public void deleteKey(int key) {
decreaseKey(key, Integer.MIN_VALUE);
extractMin();
}
private void MinHeapify(int key) {
int l = left(key);
int r = right(key);
int smallest = key;
if (l < current_heap_size && heapArray[l] < heapArray[smallest]) {
smallest = l;
}
if (r < current_heap_size && heapArray[r] < heapArray[smallest]) {
smallest = r;
}
if (smallest != key) {
swap(heapArray, key, smallest);
MinHeapify(smallest);
}
}
public void increaseKey(int key, int new_val) {
heapArray[key] = new_val;
MinHeapify(key);
}
public void changeValueOnAKey(int key, int new_val) {
if (heapArray[key] == new_val) {
return;
}
if (heapArray[key] < new_val) {
increaseKey(key, new_val);
} else {
decreaseKey(key, new_val);
}
}
}
class MinHeapTest {
public static void main(String[] args) {
MinHeap h = new MinHeap(11);
h.insertKey(3);
h.insertKey(2);
h.deleteKey(1);
h.insertKey(15);
h.insertKey(5);
h.insertKey(4);
h.insertKey(45);
System.out.print(h.extractMin() + " ");
System.out.print(h.getMin() + " ");
h.decreaseKey(2, 1);
System.out.print(h.getMin());
}
}