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. @end @complexity
- Time: O(n log k)
- Space: O(k) @end
bounded heap
For top-k largest values, a min-heap of size k keeps the current cutoff at the root.
Rust DSA Implementation
basic.rs
fn list_string(values: &[i32]) -> String {
format!("[{}]", values.iter().map(|v| v.to_string()).collect::<Vec<_>>().join(", "))
}
fn heap_insert(heap: &mut Vec<i32>, value: i32) {
heap.push(value);
let mut child = heap.len() - 1;
while child > 0 {
let parent = (child - 1) / 2;
if heap[parent] <= heap[child] { break; }
heap.swap(parent, child);
child = parent;
}
}
fn heap_pop(heap: &mut Vec<i32>) -> i32 {
let smallest = heap[0];
let last = heap.pop().unwrap();
heap[0] = last;
let mut parent = 0;
loop {
let left = parent * 2 + 1;
let right = left + 1;
if left >= heap.len() { break; }
let mut child = left;
if right < heap.len() && heap[right] < heap[left] { child = right; }
if heap[parent] <= heap[child] { break; }
heap.swap(parent, child);
parent = child;
}
smallest
}
fn main() { let values = [5, 1, 9, 3, 7, 2]; let mut heap = Vec::new(); for value in values { heap_insert(&mut heap, value); if heap.len() > 3 { heap_pop(&mut heap); } } heap.sort_by(|a, b| b.cmp(a)); println!("{}", list_string(&heap)); }
@end @output [9, 7, 5] @end