liveBook · Manning

Heapify

As explained in Advanced Algorithms and Data Structures

Heapify is a bottom-up procedure that transforms an arbitrary collection represented as a heap array into a valid heap.

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

How Advanced Algorithms and Data Structures covers it

How deep it goes
Explains bottom-up traversal, pushDown repair, linear construction, and branching-factor effects without deriving the full swap bound.
Examples
Uses a small min-heap diagram, array-index traversal, and extracting the largest k elements from a static set.
What it assumes
Assumes array-based heap representation and pushDown, while establishing why leaves and processed child sub-heaps are already valid.
What you can do after
Build it from scratch in linear time, then use the resulting heap for priority-based extraction.
How it connects
Connects heapify with heaps, priority queues, pushDown, binary heaps, and d-way heaps.

Also explained in

Compare how 2 books explain heapify →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Advanced Algorithms and Data Structures. The section links point to the parts of the book it draws on.