1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| package main
import ( "container/heap" "fmt" "testing" )
func TestIntHeap(t *testing.T) { h := &IntHeap{3, 4, 6, 7, 2, 1} heap.Init(h) heap.Push(h, 9) fmt.Printf("mininum: %d\n", heap.Pop(h)) heap.Push(h, 1) for h.Len() > 0 { fmt.Printf("%d ", heap.Pop(h)) } }
func TestTopKHeap(t *testing.T) { i, k := 4, 4 h := &IntMaxHeap{9, 4, 6, 7, 2, 1} l := len(*h) initNum := (*h)[:k] topk := NewTopKHeap(initNum, k)
for i < l { topk.Push((*h)[i]) i++ } topk.Push(8) topk.Push(8) fmt.Printf("topk's maxnum: %d\n", topk.Max()) fmt.Println(topk.Reversal()) for topk.Len() > 0 { fmt.Printf("%d ", topk.Pop()) } }
func TestHepPQ(t *testing.T) { items := map[string]int{"taskA": 1, "taskB": 2, "taskC": 3} p := make(PriorityQueue, len(items)) i := 0 for value, priority := range items { p[i] = &Item{value: value, priority: priority, index: i} i++ } heap.Init(&p) item := &Item{ value: "ooo", priority: 4, } heap.Push(&p, item) p.Update(item, item.value, 5) for p.Len() > 0 { item := heap.Pop(&p).(*Item) fmt.Printf("%.2d:%s\n", item.priority, item.value) } }
|