What if you could organize a messy pile instantly without checking every item?
Why Build Heap from Array Heapify in DSA Typescript?
Imagine you have a messy pile of books on a table, and you want to organize them quickly so the biggest book is always on top. Doing this by checking and moving one book at a time by hand takes forever.
Manually sorting or organizing each book one by one is slow and tiring. You might miss some books or move them multiple times, making the process error-prone and frustrating.
Heapify is like a smart helper that quickly arranges the pile so the biggest book is on top by fixing small parts of the pile step-by-step. It does this efficiently without checking every book again and again.
for (let i = 0; i < array.length; i++) { for (let j = i + 1; j < array.length; j++) { if (array[j] > array[i]) { let temp = array[i]; array[i] = array[j]; array[j] = temp; } } }
function heapify(array: number[], size: number, root: number) {
let largest = root;
let left = 2 * root + 1;
let right = 2 * root + 2;
if (left < size && array[left] > array[largest]) largest = left;
if (right < size && array[right] > array[largest]) largest = right;
if (largest !== root) {
[array[root], array[largest]] = [array[largest], array[root]];
heapify(array, size, largest);
}
}Heapify lets us build a heap quickly from any list, making tasks like sorting or priority handling fast and easy.
When your phone manages which app notifications to show first, it uses a heap to quickly find the most important alerts without checking all apps every time.
Manual sorting is slow and error-prone for large lists.
Heapify fixes parts of the list efficiently to build a heap.
This method speeds up tasks like sorting and priority management.
Practice
heapify process when building a heap from an array?Solution
Step 1: Understand heap property
A heap is a special tree where parent nodes follow a specific order compared to children (max-heap or min-heap).Step 2: Role of heapify
Heapify rearranges elements so that the heap property holds for every subtree, starting from the bottom.Final Answer:
To rearrange the array elements to satisfy the heap property -> Option DQuick Check:
Heapify = Rearrange for heap property [OK]
- Thinking heapify sorts the array
- Confusing heapify with finding max element
- Assuming heapify reverses the array
arr of length n?Solution
Step 1: Identify last non-leaf node index
In a heap array, last non-leaf node is at index Math.floor(n/2) - 1.Step 2: Loop from last non-leaf to root
We heapify from this index down to 0 to ensure all subtrees satisfy heap property.Final Answer:
for(let i = Math.floor(n / 2) - 1; i >= 0; i--) { heapify(arr, n, i); } -> Option BQuick Check:
Start heapify at last non-leaf node [OK]
- Starting heapify from 0 upwards
- Starting heapify from last element
- Looping beyond last non-leaf node
arr = [3, 9, 2, 1, 4, 5], what is the array after building a max-heap using heapify?Solution
Step 1: Start heapify from index 2 (Math.floor(6/2)-1)
Heapify at index 2: children 5 (index 5), swap 2 and 5 to get [3,9,5,1,4,2].Step 2: Heapify at index 1 and 0
Index 1: children 1 and 4, max is 9 at index 1, no swap. Index 0: children 9 and 5, swap 3 and 9. Final array: [9,4,5,1,3,2]Final Answer:
[9, 4, 5, 1, 3, 2] -> Option AQuick Check:
Max-heap root is 9, children smaller [OK]
- Not swapping correctly during heapify
- Confusing min-heap with max-heap
- Leaving original array unchanged
function heapify(arr: number[], n: number, i: number) {
let largest = i;
let left = 2 * i + 1;
let 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 buildHeap(arr: number[]) {
let n = arr.length;
for (let i = 0; i <= Math.floor(n / 2); i++) {
heapify(arr, n, i);
}
}
What is the error in this code?Solution
Step 1: Analyze buildHeap loop range
The loop runs from 0 up to Math.floor(n/2), but heapify must start from last non-leaf node down to root.Step 2: Correct loop direction and range
Loop should be: for(let i = Math.floor(n/2) - 1; i >= 0; i--) to ensure all subtrees heapify properly.Final Answer:
The loop in buildHeap should run from Math.floor(n/2)-1 down to 0 -> Option AQuick Check:
Heapify bottom-up loop direction matters [OK]
- Looping heapify top-down instead of bottom-up
- Incorrect child index calculations
- Not swapping elements during heapify
arr = [4, 10, 3, 5, 1, 2]. After building a min-heap using heapify, what will be the array representation?Solution
Step 1: Build min-heap bottom-up
Start heapify from index 2 (Math.floor(6/2)-1): heapify at index 2 swaps 3 and 2 to get [4,10,2,5,1,3].Step 2: Heapify at index 1 and 0
At index 1: children 5 and 1, swap 10 and 1 -> [4,1,2,5,10,3]. At index 0: children 1 and 2, swap 4 and 1 -> [1,4,2,5,10,3].Final Answer:
[1, 4, 2, 5, 10, 3] -> Option CQuick Check:
Min-heap root is smallest element 1 [OK]
- Confusing min-heap with max-heap results
- Not swapping correctly during heapify
- Assuming sorted array equals heap
Ready to prove it?
Pass the test and this pattern moves unknown
