0
0
DSA C++programming~10 mins

Build Heap from Array Heapify in DSA C++ - Execution Trace

Choose your learning style9 modes available
Concept Flow - Build Heap from Array Heapify
Start with input array
Identify last non-leaf node
For each node from last non-leaf to root
Perform heapify on current node
Heapify: Compare node with children
Swap with largest child if needed
Repeat heapify on swapped child
Move to previous node
All nodes heapified -> Max-Heap built
Start from the last non-leaf node and heapify each node upwards to the root to build a max-heap from the array.
Execution Sample
DSA C++
void buildHeap(int arr[], int n) {
  for (int i = n/2 - 1; i >= 0; i--) {
    heapify(arr, n, i);
  }
}
This code builds a max-heap from an unsorted array by calling heapify from the last non-leaf node up to the root.
Execution Table
StepOperationNode IndexNodes in ArrayPointer ChangesVisual State
1Start buildHeap-[4, 10, 3, 5, 1]Set i = 1 (n/2 -1)[4, 10, 3, 5, 1]
2Heapify at index 11[4, 10, 3, 5, 1]Compare arr[1]=10 with children arr[3]=5, arr[4]=1[4, 10, 3, 5, 1]
3Heapify at index 11[4, 10, 3, 5, 1]No swap needed (10 is largest)[4, 10, 3, 5, 1]
4Move to index 00[4, 10, 3, 5, 1]Compare arr[0]=4 with children arr[1]=10, arr[2]=3[4, 10, 3, 5, 1]
5Swap arr[0] and arr[1]0->1[4, 10, 3, 5, 1]Swap 4 and 10[10, 4, 3, 5, 1]
6Heapify at index 11[10, 4, 3, 5, 1]Compare arr[1]=4 with children arr[3]=5, arr[4]=1[10, 4, 3, 5, 1]
7Swap arr[1] and arr[3]1->3[10, 4, 3, 5, 1]Swap 4 and 5[10, 5, 3, 4, 1]
8Heapify at index 33[10, 5, 3, 4, 1]No children, stop[10, 5, 3, 4, 1]
9All nodes heapified-[10, 5, 3, 4, 1]Heap built[10, 5, 3, 4, 1]
💡 All non-leaf nodes heapified, max-heap property satisfied for entire array.
Variable Tracker
VariableStartAfter Step 2After Step 5After Step 7Final
i-100-
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]
heapify node-113-
Key Moments - 3 Insights
Why do we start heapify from index n/2 - 1 and not from the last element?
Because nodes from n/2 to n-1 are leaf nodes and already satisfy heap property. See execution_table step 1 where i starts at 1 for n=5.
Why do we heapify again at the child index after swapping?
Swapping may break heap property at the child, so we heapify recursively down. See steps 6-8 where after swapping at index 1, heapify continues at index 3.
What happens if no swap is needed during heapify?
Heapify stops for that node as heap property is satisfied. See step 3 where no swap occurs at index 1.
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 execution_table.
At which step does the heapify process stop for the node at index 3?
AStep 6
BStep 8
CStep 7
DStep 9
💡 Hint
Look for 'No children, stop' in the Action column in execution_table.
If the input array was already a max-heap, how would the execution_table change?
AMore swaps would occur
BHeapify would start from index 0 instead of n/2 -1
CNo swaps would occur during heapify
DHeapify would not be called at all
💡 Hint
Refer to step 3 where no swap is needed when heap property is already satisfied.
Concept Snapshot
Build Heap from Array (Heapify):
- Start from last non-leaf node (n/2 -1) down to root
- For each node, heapify: compare with children
- Swap with largest child if needed
- Repeat heapify down the subtree
- Result: array satisfies max-heap property
Full Transcript
To build a max-heap from an array, we start heapifying from the last non-leaf node, which is at index n/2 -1, moving upwards to the root. Heapify compares the node with its children and swaps with the largest child if needed, then continues heapifying down the subtree. This process ensures the max-heap property is satisfied for the entire array. Leaf nodes do not need heapify. The example array [4, 10, 3, 5, 1] is transformed step-by-step into [10, 5, 3, 4, 1].