DSA
Heap

Heap

Push()

Pop()

Heapify() - O(n)

// for min heap
function heapify(arr) {
    arr.push(arr[0]);
    this.heap = arr;
 
    let curr = Math.floor((this.heap.length - 1) / 2);
    while (curr > 0) {
        let i = curr;
 
        while (2 * 1 < this.heap.length) {
        // perculate down
        if (
            2 * 1 < this.heap.length &&                // children within heap
            this.heap[2 * i + 1] < this.heap[i] &&     // right child < parent
            this.heap[2 * i + 1] < this.heap[2 * i]    // right child < left child
        ) {
            // swap right child
            let tmp = this.heap[i];
            this.heap[i] = this.heap[2 * i + 1];
            this.heap[2 * i + 1] = tmp;
            i = 2 * i + 1;
        } else if (this.heap[2 * i + 1] < this.heap[i]) {
            // swap left child
            let tmp = this.heap[i];
            this.heap[i] = this.heap[2 * i + 1];
            this.heap[2 * i + 1] = tmp;
            i = 2 * i + 1;
        } else {
            break;
        }
        }
        curr--;
    }
    return;
}