/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class MaxHeap {
int capacity;
int size;
int arr[];
MaxHeap(int capacity) {
this.capacity = capacity;
size = 0;
this.arr = new int[capacity];
}
int parent(int i) {
return (i - 1) / 2;
}
int left(int i) {
return 2 * i + 1;
}
int right(int i) {
return 2 * i + 2;
}
void insert(int ele) {
if (size == capacity) {
System.
out.
println("Heap overflow"); return;
}
arr[size] = ele;
int k = size;
size++;
while (k != 0 && arr[k] > arr[parent(k)]) {
int temp = arr[parent(k)];
arr[parent(k)] = arr[k];
arr[k] = temp;
k = parent(k);
}
}
void heapify(int i) {
int largest = i;
int l = left(i);
int r = right(i);
if (l < size && arr[l] > arr[largest]) {
largest = l;
}
if (r < size && arr[r] > arr[largest]) {
largest = r;
}
if (largest != i) {
int temp = arr[i];
arr[i] = arr[largest];
arr[largest] = temp;
heapify(largest);
}
}
int deleteMax() {
if (size <= 0) {
System.
out.
println("Heap is empty"); return -1;
}
if (size == 1) {
size--;
return arr[0];
}
int root = arr[0];
arr[0] = arr[size - 1];
size--;
heapify(0);
return root;
}
int getMax() {
return arr[0];
}
MaxHeap h = new MaxHeap(20);
h.insert(4);
h.insert(1);
h.insert(2);
h.insert(6);
h.insert(7);
h.insert(3);
h.insert(8);
h.insert(5);
System.
out.
println("Max value is " + h.
getMax()); h.insert(99);
System.
out.
println("Max value is " + h.
getMax()); System.
out.
println("Deleted max value: " + h.
deleteMax()); System.
out.
println("Max value after deletion is " + h.
getMax()); }
}
LyogcGFja2FnZSB3aGF0ZXZlcjsgLy8gZG9uJ3QgcGxhY2UgcGFja2FnZSBuYW1lISAqLwoKaW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS5sYW5nLio7CmltcG9ydCBqYXZhLmlvLio7CgovKiBOYW1lIG9mIHRoZSBjbGFzcyBoYXMgdG8gYmUgIk1haW4iIG9ubHkgaWYgdGhlIGNsYXNzIGlzIHB1YmxpYy4gKi8KY2xhc3MgTWF4SGVhcCB7CiAgICBpbnQgY2FwYWNpdHk7CiAgICBpbnQgc2l6ZTsKICAgIGludCBhcnJbXTsKCiAgICBNYXhIZWFwKGludCBjYXBhY2l0eSkgewogICAgICAgIHRoaXMuY2FwYWNpdHkgPSBjYXBhY2l0eTsKICAgICAgICBzaXplID0gMDsKICAgICAgICB0aGlzLmFyciA9IG5ldyBpbnRbY2FwYWNpdHldOwogICAgfQoKICAgIGludCBwYXJlbnQoaW50IGkpIHsKICAgICAgICByZXR1cm4gKGkgLSAxKSAvIDI7CiAgICB9CgogICAgaW50IGxlZnQoaW50IGkpIHsKICAgICAgICByZXR1cm4gMiAqIGkgKyAxOwogICAgfQoKICAgIGludCByaWdodChpbnQgaSkgewogICAgICAgIHJldHVybiAyICogaSArIDI7CiAgICB9CgogICAgdm9pZCBpbnNlcnQoaW50IGVsZSkgewogICAgICAgIGlmIChzaXplID09IGNhcGFjaXR5KSB7CiAgICAgICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbigiSGVhcCBvdmVyZmxvdyIpOwogICAgICAgICAgICByZXR1cm47CiAgICAgICAgfQogICAgICAgIGFycltzaXplXSA9IGVsZTsKICAgICAgICBpbnQgayA9IHNpemU7CiAgICAgICAgc2l6ZSsrOwogICAgICAgIHdoaWxlIChrICE9IDAgJiYgYXJyW2tdID4gYXJyW3BhcmVudChrKV0pIHsKICAgICAgICAgICAgaW50IHRlbXAgPSBhcnJbcGFyZW50KGspXTsKICAgICAgICAgICAgYXJyW3BhcmVudChrKV0gPSBhcnJba107CiAgICAgICAgICAgIGFycltrXSA9IHRlbXA7CiAgICAgICAgICAgIGsgPSBwYXJlbnQoayk7CiAgICAgICAgfQogICAgfQoKICAgIHZvaWQgaGVhcGlmeShpbnQgaSkgewogICAgICAgIGludCBsYXJnZXN0ID0gaTsKICAgICAgICBpbnQgbCA9IGxlZnQoaSk7CiAgICAgICAgaW50IHIgPSByaWdodChpKTsKCiAgICAgICAgaWYgKGwgPCBzaXplICYmIGFycltsXSA+IGFycltsYXJnZXN0XSkgewogICAgICAgICAgICBsYXJnZXN0ID0gbDsKICAgICAgICB9CiAgICAgICAgaWYgKHIgPCBzaXplICYmIGFycltyXSA+IGFycltsYXJnZXN0XSkgewogICAgICAgICAgICBsYXJnZXN0ID0gcjsKICAgICAgICB9CiAgICAgICAgaWYgKGxhcmdlc3QgIT0gaSkgewogICAgICAgICAgICBpbnQgdGVtcCA9IGFycltpXTsKICAgICAgICAgICAgYXJyW2ldID0gYXJyW2xhcmdlc3RdOwogICAgICAgICAgICBhcnJbbGFyZ2VzdF0gPSB0ZW1wOwogICAgICAgICAgICBoZWFwaWZ5KGxhcmdlc3QpOwogICAgICAgIH0KICAgIH0KCiAgICBpbnQgZGVsZXRlTWF4KCkgewogICAgICAgIGlmIChzaXplIDw9IDApIHsKICAgICAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKCJIZWFwIGlzIGVtcHR5Iik7CiAgICAgICAgICAgIHJldHVybiAtMTsKICAgICAgICB9CiAgICAgICAgaWYgKHNpemUgPT0gMSkgewogICAgICAgICAgICBzaXplLS07CiAgICAgICAgICAgIHJldHVybiBhcnJbMF07CiAgICAgICAgfQogICAgICAgIGludCByb290ID0gYXJyWzBdOwogICAgICAgIGFyclswXSA9IGFycltzaXplIC0gMV07CiAgICAgICAgc2l6ZS0tOwogICAgICAgIGhlYXBpZnkoMCk7CiAgICAgICAgcmV0dXJuIHJvb3Q7CiAgICB9CgogICAgaW50IGdldE1heCgpIHsKICAgICAgICByZXR1cm4gYXJyWzBdOwogICAgfQoKICAgIHB1YmxpYyBzdGF0aWMgdm9pZCBtYWluKFN0cmluZ1tdIGFyZ3MpIHRocm93cyBqYXZhLmxhbmcuRXhjZXB0aW9uIHsKICAgICAgICBNYXhIZWFwIGggPSBuZXcgTWF4SGVhcCgyMCk7CiAgICAgICAgaC5pbnNlcnQoNCk7CiAgICAgICAgaC5pbnNlcnQoMSk7CiAgICAgICAgaC5pbnNlcnQoMik7CiAgICAgICAgaC5pbnNlcnQoNik7CiAgICAgICAgaC5pbnNlcnQoNyk7CiAgICAgICAgaC5pbnNlcnQoMyk7CiAgICAgICAgaC5pbnNlcnQoOCk7CiAgICAgICAgaC5pbnNlcnQoNSk7CiAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKCJNYXggdmFsdWUgaXMgIiArIGguZ2V0TWF4KCkpOwogICAgICAgIGguaW5zZXJ0KDk5KTsKICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oIk1heCB2YWx1ZSBpcyAiICsgaC5nZXRNYXgoKSk7CiAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKCJEZWxldGVkIG1heCB2YWx1ZTogIiArIGguZGVsZXRlTWF4KCkpOwogICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbigiTWF4IHZhbHVlIGFmdGVyIGRlbGV0aW9uIGlzICIgKyBoLmdldE1heCgpKTsKICAgIH0KfQo=