A heap is a specialized tree-based data structure that satisfies the heap property: for every node \(i\) other than the root, \(A[\text{parent}(i)] \ge A[i]\) (Max-Heap) or \(A[\text{parent}(i)] \le A[i]\) (Min-Heap).
Core Properties
Structure: A nearly complete binary tree.
Height: A heap of \(n\) nodes has height \(h = \lfloor \log n \rfloor\).
Bounds: \(2^h \le n \le 2^{h+1} - 1\).
Representation (Array): Usually implemented as a 1-indexed array.
Root: \(A[1]\)
Parent(i): \(\lfloor i/2 \rfloor\)
Left(i): \(2i\)
Right(i): \(2i + 1\)
Fact: The leaves are the nodes indexed by \(\lfloor n/2 \rfloor + 1, \dots, n\).
Key Algorithms
1. Max-Heapify (\(O(\log n)\))
Assumes the left and right subtrees of node \(i\) are already max-heaps, but \(A[i]\) might be smaller than its children. It "bubbles down" \(A[i]\) to its correct position.
2. Build-Max-Heap (\(O(n)\))
Converts an unordered array into a max-heap by calling Max-Heapify on all non-leaf nodes in bottom-up order (from \(n/2\) down to \(1\)).
Why \(O(n)\)? While \(O(n \log n)\) is an upper bound, the tight bound is \(O(n)\) because most nodes are near the leaves and only travel a small distance. The sum of heights is a convergent series: \(\sum_{h=0}^{\lfloor \log n \rfloor} \frac{n}{2^{h+1}} O(h) = O(n)\).
3. Heapsort (\(O(n \log n)\))
Build a max-heap from the array.
Swap the root (max element) with the last element of the heap.
Reduce heapsize and call Max-Heapify on the new root.
Repeat until the heap is empty.
Priority Queue API
A Max-Priority Queue maintains a set \(S\) and supports:
Insert(S, x): Adds element \(x\) (\(O(\log n)\)).
Maximum(S): Returns the max element (\(O(1)\)).
Extract-Max(S): Removes and returns the max element (\(O(\log n)\)).
Increase-Key(S, x, k): Increases \(x\)'s value to \(k\) (\(O(\log n)\)).