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.
Go DSA Implementation
basic.go
package main
import (
"fmt"
"strings"
)
func listString(values []int) string {
parts := make([]string, len(values))
for i, value := range values { parts[i] = fmt.Sprint(value) }
return "[" + strings.Join(parts, ", ") + "]"
}
func heapInsert(heap *[]int, value int) {
*heap = append(*heap, value)
child := len(*heap) - 1
for child > 0 {
parent := (child - 1) / 2
if (*heap)[parent] <= (*heap)[child] { break }
(*heap)[parent], (*heap)[child] = (*heap)[child], (*heap)[parent]
child = parent
}
}
func heapPop(heap *[]int) int {
smallest := (*heap)[0]
(*heap)[0] = (*heap)[len(*heap)-1]
*heap = (*heap)[:len(*heap)-1]
parent := 0
for {
left := parent*2 + 1
right := left + 1
if left >= len(*heap) { break }
child := left
if right < len(*heap) && (*heap)[right] < (*heap)[left] { child = right }
if (*heap)[parent] <= (*heap)[child] { break }
(*heap)[parent], (*heap)[child] = (*heap)[child], (*heap)[parent]
parent = child
}
return smallest
}
func main() { values := []int{5, 1, 9, 3, 7, 2}; heap := []int{}; for _, value := range values { heapInsert(&heap, value); if len(heap) > 3 { heapPop(&heap) } }; top := []int{9, 7, 5}; fmt.Println(listString(top)) }
@end @output [9, 7, 5] @end