Keep the best-priority item at the root while maintaining only parent–child order, rather than sorting every element.
THE MENTAL MODEL
When should this come to mind?
Keep the best-priority item at the root while maintaining only parent–child order, rather than sorting every element.
Top k valuesRepeated min/max extractionBounded candidate set
What makes it work
In a zero-based binary heap, children are 2i + 1 and 2i + 2. Insertion bubbles up; root replacement sifts down. This lesson displays the heap's array representation.
When to choose something else
A heap does not provide sorted iteration or fast lookup of an arbitrary value.
See the pattern in action
Change the input. Follow the state. Make it yours.