Heap Concept Structure and Properties in DSA C - Time & Space Complexity
We want to understand how the time to build and use a heap changes as the number of elements grows.
How does the heap structure affect the speed of operations like insertion and removal?
Analyze the time complexity of building a heap from an array.
void heapify(int arr[], int n, int i) {
int largest = i;
int left = 2 * i + 1;
int 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) {
int temp = arr[i];
arr[i] = arr[largest];
arr[largest] = temp;
heapify(arr, n, largest);
}
}
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 adjusting nodes from the bottom up.
Look at the loops and recursive calls that repeat work.
- Primary operation: The
heapifyfunction called on each node from the middle to the root. - How many times: The loop runs about
n/2times, and eachheapifycan recurse down the tree height.
As the number of elements grows, the work to fix the heap grows but not simply by multiplying.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20 to 30 operations |
| 100 | About 300 to 400 operations |
| 1000 | About 4000 to 5000 operations |
Pattern observation: The operations grow a bit faster than linearly but much slower than squaring the input size.
Time Complexity: O(n)
This means building a heap from an array takes time roughly proportional to the number of elements.
[X] Wrong: "Building a heap takes O(n log n) time because heapify is called n times and each call takes log n."
[OK] Correct: Most heapify calls happen near the bottom where the tree height is small, so the total work adds up to O(n), not O(n log n).
Understanding heap building time helps you explain why heaps are efficient for priority queues and sorting, showing your grasp of data structure performance.
"What if we used a different method that inserts elements one by one into an empty heap? How would the time complexity change?"
Practice
Solution
Step 1: Understand max-heap property
A max-heap requires each parent node to be greater than or equal to its children to maintain the heap order.Step 2: Check other options
Every parent node is less than or equal to its children describes a min-heap, C is not true for heaps, and D is partially true but incomplete without heap order.Final Answer:
Every parent node is greater than or equal to its children -> Option AQuick Check:
Max-heap = Parent ≥ Children [OK]
- Confusing max-heap with min-heap property
- Thinking all leaf nodes have same value
- Ignoring heap order property
Solution
Step 1: Recall array indexing for heap
In zero-based arrays, left child index = 2*i + 1 and right child index = 2*i + 2 for parent at i.Step 2: Verify options
Parent at index i, left child at 2*i + 1, right child at 2*i + 2 matches zero-based indexing. Parent at index i, left child at 2*i, right child at 2*i + 1 is for one-based indexing. Options A and C are incorrect formulas.Final Answer:
Parent at index i, left child at 2*i + 1, right child at 2*i + 2 -> Option CQuick Check:
Zero-based heap indexing = 2*i+1, 2*i+2 [OK]
- Using one-based indexing formulas in zero-based arrays
- Mixing up left and right child formulas
- Using division instead of multiplication for child indices
[40, 30, 35, 15, 10, 25]. What will be the array after inserting 50 and re-heapifying?Solution
Step 1: Insert 50 at the end
Array becomes [40, 30, 35, 15, 10, 25, 50].Step 2: Re-heapify by bubbling up 50
Parent of index 6 is (6-1)//2=2 (35 < 50), swap 6 and 2: [40, 30, 50, 15, 10, 25, 35]. Now at 2, parent 0 (40 < 50), swap 2 and 0: [50, 30, 40, 15, 10, 25, 35]. Heap property restored.Final Answer:
[50, 30, 40, 15, 10, 25, 35] -> Option AQuick Check:
Insert 50, bubble up to root [OK]
- Not bubbling up after insertion
- Swapping with wrong parent index
- Leaving new element at the end without re-heapify
i in a max-heap array arr of size n. What is the error?
void heapify(int arr[], int n, int i) {
int largest = i;
int left = 2 * i + 1;
int 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) {
int temp = arr[i];
arr[i] = arr[largest];
arr[largest] = temp;
heapify(arr, n, largest);
}
}Solution
Step 1: Analyze child index calculation
Left and right child indices are correctly calculated as 2*i+1 and 2*i+2 for zero-based arrays.Step 2: Check recursion and swapping logic
Swapping happens only if largest changes, then recursive call heapifies the affected subtree. No base case needed explicitly as recursion stops when largest == i.Final Answer:
No error; code correctly heapifies the subtree -> Option DQuick Check:
Heapify logic correct and complete [OK]
- Thinking explicit base case needed for recursion
- Miscomputing child indices
- Swapping unconditionally without checking largest
[5, 10, 15, 20, 25, 30]. You want to replace the root with 22 and restore the min-heap property. What will be the resulting array after heapifying?Solution
Step 1: Replace root with 22
Array becomes [22, 10, 15, 20, 25, 30].Step 2: Heapify down from root
Compare 22 with children 10 and 15. 10 is smallest, swap 22 and 10: [10, 22, 15, 20, 25, 30]. Then heapify at index 1: compare 22 with children 20 and 25, 20 is smaller, swap: [10, 20, 15, 22, 25, 30]. No further swaps needed.Final Answer:
[10, 20, 15, 22, 25, 30] -> Option BQuick Check:
Replace root, bubble down to restore min-heap [OK]
- Swapping with larger child instead of smaller
- Not continuing heapify after first swap
- Confusing min-heap with max-heap rules
Ready to prove it?
Pass the test and this pattern moves unknown
