0
0
DSA Javascriptprogramming~10 mins

Build Heap from Array Heapify in DSA Javascript - Execution Trace

Choose your learning style9 modes available
Concept Flow - Build Heap from Array Heapify
Start with array
Find last non-leaf node
For each node from last non-leaf to root
Heapify node
Compare node with children
Swap if child larger (max-heap)
Repeat heapify down subtree
Move to previous node
Done when root heapified
Start from the last non-leaf node and heapify each node up to the root to build a max-heap.
Execution Sample
DSA Javascript
function buildHeap(arr) {
  let n = arr.length;
  for (let i = Math.floor(n / 2) - 1; i >= 0; i--) {
    heapify(arr, n, i);
  }
}

function heapify(arr, n, i) {
  // heapify logic here
}
Builds a max-heap from an unsorted array by heapifying nodes from bottom up.
Execution Table
StepOperationNode IndexNodes in ArrayPointer ChangesVisual State
1Start buildHeap-[4, 10, 3, 5, 1]-4 -> 10 -> 3 -> 5 -> 1
2Find last non-leaf node---Last non-leaf index = 1
3Heapify node1[4, 10, 3, 5, 1]Compare arr[1]=10 with children arr[3]=5 and arr[4]=14 -> 10 -> 3 -> 5 -> 1
4Heapify node0[4, 10, 3, 5, 1]Compare arr[0]=4 with children arr[1]=10 and arr[2]=34 -> 10 -> 3 -> 5 -> 1
5Swap arr[0] and arr[1]0 & 1[4, 10, 3, 5, 1]Swap 4 and 1010 -> 4 -> 3 -> 5 -> 1
6Heapify node1[10, 4, 3, 5, 1]Compare arr[1]=4 with children arr[3]=5 and arr[4]=110 -> 4 -> 3 -> 5 -> 1
7Swap arr[1] and arr[3]1 & 3[10, 4, 3, 5, 1]Swap 4 and 510 -> 5 -> 3 -> 4 -> 1
8Heapify node3[10, 5, 3, 4, 1]Node 3 is leaf, no action10 -> 5 -> 3 -> 4 -> 1
9BuildHeap done-[10, 5, 3, 4, 1]-10 -> 5 -> 3 -> 4 -> 1
💡 All non-leaf nodes heapified, array is now a max-heap.
Variable Tracker
VariableStartAfter Step 3After Step 5After Step 7Final
arr[4, 10, 3, 5, 1][4, 10, 3, 5, 1][10, 4, 3, 5, 1][10, 5, 3, 4, 1][10, 5, 3, 4, 1]
i (heapify index)-101-
n (array length)55555
Key Moments - 3 Insights
Why do we start heapifying from the last non-leaf node instead of the root?
Because leaf nodes have no children to compare with, starting from the last non-leaf node ensures we heapify all subtrees bottom-up, as shown in steps 2 and 3.
Why do we swap nodes during heapify?
We swap to maintain the max-heap property where parent nodes are larger than children, as seen in steps 5 and 7 where swaps happen to move larger values up.
What happens if a node is a leaf during heapify?
No action is needed because leaf nodes have no children to compare with, as shown in step 8.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the array state after step 5?
A[10, 4, 3, 5, 1]
B[4, 10, 3, 5, 1]
C[10, 5, 3, 4, 1]
D[4, 5, 3, 10, 1]
💡 Hint
Check the 'Visual State' column at step 5 in the execution_table.
At which step does the heapify process swap the values 4 and 5?
AStep 4
BStep 7
CStep 6
DStep 8
💡 Hint
Look at the 'Operation' and 'Pointer Changes' columns around steps 6 and 7.
If the array was already a max-heap, how would the execution table change?
AHeapify would start from the root only.
BMore swaps would occur to fix the heap.
CNo swaps would occur during heapify steps.
DThe array size would change.
💡 Hint
Refer to the 'Swap' actions in the 'Pointer Changes' column in the execution_table.
Concept Snapshot
Build Heap from Array Heapify:
- Start from last non-leaf node (floor(n/2)-1)
- Heapify each node up to root
- Heapify compares node with children
- Swap if child larger (max-heap)
- Repeat heapify down subtree
- Result: array represents a max-heap
Full Transcript
To build a max-heap from an array, we start heapifying from the last non-leaf node moving up to the root. Heapify compares a node with its children and swaps if a child is larger to maintain the max-heap property. This process repeats down the subtree until the heap property is restored. Leaf nodes are skipped since they have no children. After all non-leaf nodes are heapified, the array represents a max-heap.