Binary Heaps: Invariants, Linear-Time Build-Heap & Heap Sort
“Array indexing, Max-Heapify mechanics, algebraic-geometric proof of O(n) Build-Heap, and priority queue operations.”
1.The Binary Heap as a Compact Array Structure
§ 4.1.A Binary Heap is a complete binary tree stored as an array without explicit pointer overhead:
- The tree is completely filled on all levels except possibly the lowest, which is filled from left to right.
- For node at index i (1-indexed):
text{Parent}(i) = lfloor i / 2 rfloor, text{Left}(i) = 2i, text{Right}(i) = 2i + 1.
Heap Property:
- Max-Heap: A[text{Parent}(i)] >= A[i]. The maximum key is always at index 1.
- Min-Heap: A[text{Parent}(i)] <= A[i]. The minimum key is always at index 1.
2.Proof of Linear-Time Build-Heap Complexity
§ 4.2.To build a heap from an unordered array of n elements:
Call Max-Heapify bottom-up from index lfloor n/2 rfloor down to 1.
Mathematical Derivation:
- An n-element heap has height lfloor log_2 n rfloor.
- There are at most lceil n / 2^{h+1} rceil nodes at height h.
- Running Max-Heapify on a node of height h takes O(h) time.
Total time:
T(n) <= sum_{h=0}^{lfloor log_2 n rfloor} lceil frac{n}{2^{h+1}} rceil * O(h) <= frac{n}{2} sum_{h=0}^infty frac{h}{2^h}
The infinite arithmetic-geometric series sum_{h=0}^infty frac{h}{2^h} = 2.
Therefore: T(n) <= frac{n}{2} * 2 = O(n).
3. Worked Problem: Maximum Element Location in Min-Heap
In a Min-Heap, the maximum element is never at an internal node; it is always at a leaf node.
Problem: An array of n = 10 distinct elements forms a Min-Heap. What are the possible indices where the maximum element can reside (using 1-based indexing)?
Step 1: Identify internal nodes and leaves for n = 10:
Last internal node = floor(n / 2) = floor(10 / 2) = 5.
Internal nodes are indices: {1, 2, 3, 4, 5}.
Leaf nodes are indices: {6, 7, 8, 9, 10}.
Step 2: Apply Min-Heap property:
In a Min-Heap, every parent is smaller than both of its children.
If an element is located at any internal node i in {1, 2, 3, 4, 5}, at least one of its children is strictly larger than it.
Therefore, the global maximum CANNOT reside at any internal node.
Step 3: Conclusion:
The maximum element must reside in one of the leaf nodes.
Final Answer: The maximum element can reside at any index in {6, 7, 8, 9, 10}.
Primary Academic References & Exam Sources
Every concept in Notes From a B.Tech Brain is synthesized from authoritative academic texts, seminal peer-reviewed research, verified exam archives, and renowned university lectures.
Curated for clarity, rigorous numerical solving, and semester excellence:
Visual thinkers who want to see each algorithm step animated clearly.
GATE-targeted coverage with step-by-step GATE PYQ walkthroughs.