Recognize the pattern
When should you reach for a bounded heap?
Look for “largest,” “smallest,” “closest,” “most frequent,” or “best K” when K is much smaller than the full input. A heap avoids the cost and disruption of fully sorting candidates you will never return.
Decision recipe
- 01Define the score and whether larger or smaller is better.
- 02Choose a root that exposes the weakest retained item.
- 03Keep heap size at most K; replace only when the boundary improves.
- 04Return the root for kth order statistics or all retained nodes for Top K.