08-heaps
Top-K with a Heap
Keep only the largest k values by maintaining a small min-heap.
Algorithm
Steps
- Store the heap in an array.
- Compare parent and child indexes instead of building explicit tree nodes.
- Swap only when the heap order is violated.
- Print the deterministic final heap state for replay comparison.
bounded heap
For top-k largest values, a min-heap of size k keeps the current cutoff at the root.
Complexity
- Time: O(n log k)
- Space: O(k)
Dart DSA Implementation
basic.dart
Replay: real traced execution (multi-file project)
String listString(List<int> values) => "[${values.join(", ")}]";
void heapInsert(List<int> heap, int value) {
heap.add(value);
var child = heap.length - 1;
while (child > 0) {
final parent = (child - 1) ~/ 2;
if (heap[parent] <= heap[child]) break;
final tmp = heap[parent]; heap[parent] = heap[child]; heap[child] = tmp;
child = parent;
}
}
int heapPop(List<int> heap) {
final smallest = heap[0];
heap[0] = heap.removeLast();
var parent = 0;
while (true) {
final left = parent * 2 + 1;
final right = left + 1;
if (left >= heap.length) break;
var child = left;
if (right < heap.length && heap[right] < heap[left]) child = right;
if (heap[parent] <= heap[child]) break;
final tmp = heap[parent]; heap[parent] = heap[child]; heap[child] = tmp;
parent = child;
}
return smallest;
}
void main() { final heap = <int>[]; for (final value in [5, 1, 9, 3, 7, 2]) { heapInsert(heap, value); if (heap.length > 3) heapPop(heap); } heap.sort((a, b) => b.compareTo(a)); print(listString(heap)); }
top-k heap ← [5]
27}28void main() { final heap = <int>[]; for (final value in [5, 1, 9, 3, 7, 2]) { heapInsert(heap, value); if (heap.length > 3) heapPop(heap); } heap.sort((a, b) => b.compareTo(a)); print(listString(heap)); }values this step[5]top-k heap5valuetop-k heap ← [1, 5]
27}28void main() { final heap = <int>[]; for (final value in [5, 1, 9, 3, 7, 2]) { heapInsert(heap, value); if (heap.length > 3) heapPop(heap); } heap.sort((a, b) => b.compareTo(a)); print(listString(heap)); }values this step[1, 5]top-k heap1valuetop-k heap ← [1, 5, 9]
27}28void main() { final heap = <int>[]; for (final value in [5, 1, 9, 3, 7, 2]) { heapInsert(heap, value); if (heap.length > 3) heapPop(heap); } heap.sort((a, b) => b.compareTo(a)); print(listString(heap)); }values this step[1, 5, 9]top-k heap9valuetop-k heap ← [3, 5, 9]
27}28void main() { final heap = <int>[]; for (final value in [5, 1, 9, 3, 7, 2]) { heapInsert(heap, value); if (heap.length > 3) heapPop(heap); } heap.sort((a, b) => b.compareTo(a)); print(listString(heap)); }values this step[3, 5, 9]top-k heap3valuetop-k heap ← [5, 7, 9]
27}28void main() { final heap = <int>[]; for (final value in [5, 1, 9, 3, 7, 2]) { heapInsert(heap, value); if (heap.length > 3) heapPop(heap); } heap.sort((a, b) => b.compareTo(a)); print(listString(heap)); }values this step[5, 7, 9]top-k heap7valuetop-k heap ← [5, 7, 9]
27}28void main() { final heap = <int>[]; for (final value in [5, 1, 9, 3, 7, 2]) { heapInsert(heap, value); if (heap.length > 3) heapPop(heap); } heap.sort((a, b) => b.compareTo(a)); print(listString(heap)); }values this step[5, 7, 9]top-k heap2valuestdout ← [9, 7, 5]
27}28void main() { final heap = <int>[]; for (final value in [5, 1, 9, 3, 7, 2]) { heapInsert(heap, value); if (heap.length > 3) heapPop(heap); } heap.sort((a, b) => b.compareTo(a)); print(listString(heap)); }values this step[9, 7, 5]stdout[5, 7, 9]top-k heap
Output
[9, 7, 5]