Heapify transforms an unordered array of elements into a valid heap without inserting items one at a time. It is useful when a complete static collection must become a priority queue, especially when the collection is much larger than the number of items eventually extracted. section 2.6.7 section 2.7.1
Build the heap from the bottom upward:
- Treat every leaf as a valid one-element heap. section 2.6.7
- Visit internal array positions from right to left, starting at the last internal node. section 2.6.7
- Call pushDown at each position; because its children have already become valid heaps, pushDown repairs the current sub-heap. section 2.6.7
- Continue until the root is processed, producing a valid heap across the entire array. section 2.6.7
Small-array heapification
In a min-heap, begin with leaves as the only verified sub-heaps. Repair the first internal node by swapping it with the smaller child when necessary, move leftward, and finally push the temporary root downward until it reaches its proper position.
Heapify creates a heap in linear time, O(n), and uses the array representation rather than O(n) additional heap storage. Afterward, extracting the largest k items from a max-heap costs O(k log n), for O(n + k log n) overall. section 2.7.1
Figure 2.12 Heapification of a small array. The boxes with rounded corners surround valid sub-heaps; that is, portions of the tree for which we have verified that they abide by the heap properties. Step 1: Initially, only leaves are valid min-heap; here, smaller numbers mean higher priority. Step 2: The element from which we start our iteration is the first internal node in the heap, at index 2 in our array. In the bottom part of the leftmost figure, it is pointed at by an arrow. We repair the heap properties for the sub-heap rooted at this node by swapping its content with its child’s. Step 3: We move the arrow pointing to the current element one position to the left and repeat the operation for the sub-heap rooted at index 1. Step 4: Finally, the whole array is heapified, by pushing down the temporary root (with priority 5) until we find its right place.
Follow the array positions and see how verified sub-heaps expand until the root is repaired. section 2.6.7

