function buildMaxHeap(arr: number[]): number[] {
const n = arr.length;
for (let i = Math.floor(n / 2) - 1; i >= 0; i--) {
heapify(arr, n, i);
}
return arr;
}
function heapify(arr: number[], n: number, i: number): void {
let largest = i;
const left = 2 * i + 1;
const right = 2 * i + 2;
if (left < n && arr[left] > arr[largest]) {
largest = left;
}
if (right < n && arr[right] > arr[largest]) {
largest = right;
}
if (largest !== i) {
[arr[i], arr[largest]] = [arr[largest], arr[i]];
heapify(arr, n, largest);
}
}
const input = [3, 9, 2, 1, 4, 5];
console.log(buildMaxHeap(input));The build max heap process starts heapifying from the last non-leaf node (index 2) up to the root (index 0). After heapifying, the array becomes [9, 4, 5, 1, 3, 2].
function buildMinHeap(arr: number[]): number[] {
const n = arr.length;
for (let i = Math.floor(n / 2) - 1; i >= 0; i--) {
minHeapify(arr, n, i);
}
return arr;
}
function minHeapify(arr: number[], n: number, i: number): void {
let smallest = i;
const left = 2 * i + 1;
const right = 2 * i + 2;
if (left < n && arr[left] < arr[smallest]) {
smallest = left;
}
if (right < n && arr[right] < arr[smallest]) {
smallest = right;
}
if (smallest !== i) {
[arr[i], arr[smallest]] = [arr[smallest], arr[i]];
minHeapify(arr, n, smallest);
}
}
const input = [7, 2, 6, 3, 9, 1];
console.log(buildMinHeap(input));Starting from index 2, heapify ensures the smallest element is at the root. The final min heap array is [1, 2, 7, 3, 9, 6].
function heapify(arr: number[], n: number, i: number): void {
let largest = i;
const left = 2 * i + 1;
const right = 2 * i + 2;
if (left < n && arr[left] > arr[largest]) {
largest = left;
}
if (right < n && arr[right] > arr[largest]) {
largest = right;
}
if (largest !== i) {
[arr[i], arr[largest]] = [arr[largest], arr[i]];
heapify(arr, n, largest);
}
}
function buildMaxHeap(arr: number[]): number[] {
const n = arr.length;
for (let i = Math.floor(n / 2) - 1; i >= 0; i--) {
heapify(arr, n, i);
}
return arr;
}
const input = [4, 10, 3, 5, 1];
console.log(buildMaxHeap(input));The condition uses 'left <= n' and 'right <= n' which allows index equal to n, causing out-of-bound access. It should be 'left < n' and 'right < n'.
Heapify cost per node depends on height. There are fewer nodes at higher levels with more cost, and many nodes at lower levels with less cost. Summing all gives O(n).
function heapify(arr: number[], n: number, i: number): void {
let largest = i;
const left = 2 * i + 1;
const right = 2 * i + 2;
if (left < n && arr[left] > arr[largest]) {
largest = left;
}
if (right < n && arr[right] > arr[largest]) {
largest = right;
}
if (largest !== i) {
[arr[i], arr[largest]] = [arr[largest], arr[i]];
heapify(arr, n, largest);
}
}
function buildMaxHeap(arr: number[]): void {
const n = arr.length;
for (let i = Math.floor(n / 2) - 1; i >= 0; i--) {
heapify(arr, n, i);
}
}
function extractMax(arr: number[]): number | null {
const n = arr.length;
if (n === 0) return null;
const max = arr[0];
arr[0] = arr[n - 1];
arr.pop();
heapify(arr, arr.length, 0);
return max;
}
const input = [4, 10, 3, 5, 1];
buildMaxHeap(input);
extractMax(input);
console.log(input);After building max heap, the array is [10, 5, 3, 4, 1]. Extracting max removes 10, replaces root with 1, then heapifies to [5, 4, 3, 1].
