3. What is the output of the following code snippet?
const heap = [1, 3, 6, 5, 9, 8];
function extractMin(heap) {
const min = heap[0];
heap[0] = heap.pop();
heapifyDown(heap, 0);
return min;
}
function heapifyDown(heap, i) {
const left = 2 * i + 1;
const right = 2 * i + 2;
let smallest = i;
if (left < heap.length && heap[left] < heap[smallest]) smallest = left;
if (right < heap.length && heap[right] < heap[smallest]) smallest = right;
if (smallest !== i) {
[heap[i], heap[smallest]] = [heap[smallest], heap[i]];
heapifyDown(heap, smallest);
}
}
console.log(extractMin(heap));