Heaps and priority queues
Trees stored in arrays that always hand you the smallest or largest item.
- Binary heaps: array layout, sift up, sift down A binary heap is a complete tree stored in an array where every parent is smaller than its children. Sift up after insert and sift down…
- Building a heap in O(n) Heapify builds a heap from an array in O(n) by sifting down from the last internal node backward. Most nodes are near the bottom and move…
- Heap sort Heap sort builds a max-heap, then repeatedly moves the maximum to the end and shrinks the heap. It sorts in place in O(n log n).
- Priority queues in practice: top-k, k-way merge, scheduling Priority queues solve top-k, k-way merge and scheduling problems by always handing you the most urgent item next.