To prove that building a heap from an arbitrary array of (n) elements can be done in O((n)) time complexity in the worst case, let's first understand what building a heap involves and then break down the process mathematically.
Understanding BuildHeap Process
The BuildHeap operation constructs a heap (a complete binary tree satisfying the heap property) from an unsorted array. For a max heap, the heap property is that for every node (i) other than the root, the value of (i) is less than or equal to the value of its parent. The same logic applies inversely to a min heap.
A naive approach might involve inserting elements into the heap one by one and re-establishing the heap property after each insertion, leading to a complexity of O((n\log n)), because inserting an element into a heap is O((\log n)), and doing it (n) times for (n) elements results in O((n\log n)).
However, the BuildHeap operation uses a more efficient bottom-up approach. It starts from the last non-leaf node to the root node, applying a heapify process (sifting down) to each, to ensure the subtree rooted at that node satisfies the heap property.
Mathematical Breakdown
The heap can be viewed as a complete binary tree. A complete binary tree of height (h) has at most (2^h) nodes at height (h), and the total number of nodes can be at most (2^{h+1} - 1).
- The last level of the tree has up to (2^h) nodes, but all these nodes are already heaps (since they are leaves, each is a trivial heap).
- The number of nodes at each level halves as we move up the tree, leading to a tighter bound on the work required to build the heap.
If we define the work required to heapify a subtree rooted at a node (i.e., the cost of the heapify operation) as proportional to the height of the node in the tree, then:
- Nodes at height (h) (leaves) require (0) work since we don't need to heapify them.
- Nodes at height (h-1) each require (O(1)) work.
- Nodes at height (h-2) each require (O(2)) work.
- ...
- The root node, at height (0), requires (O(h)) work.
The total work (W) required to build the heap can be summed up over all levels:
[W = \sum_{i=0}^{h} 2^i \cdot O(h-i)]
This may first appear to be (O(n\log n)), but evaluating it more closely using properties of geometric series and the fact that the height of the tree (h = \log n), shows that the sum is, in fact, (O(n)).
Intuitive Explanation
The key insight for the (O(n)) complexity is that although the height of the nodes increases as we move up the tree, the number of nodes at each level decreases exponentially. So, the majority of the heapify operations are done on the lower levels of the tree (closer to the leaves), which require less work than the operations on the higher levels. When summed up, the resulting total work done across all levels is linear relative to the number of nodes.
Conclusion
Through this analysis, we demonstrate that the BuildHeap operation can indeed be completed in (O(n)) time complexity in the worst case, which is a fundamental result in the theory of binary heaps and priority queues.