-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_heap_test.go
More file actions
44 lines (38 loc) · 844 Bytes
/
Copy pathexample_heap_test.go
File metadata and controls
44 lines (38 loc) · 844 Bytes
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
package gdk_test
import (
"fmt"
"github.com/researchlab/gdk"
)
type student struct {
name string
age int
}
func ExampleNewHeap() {
h := gdk.NewHeap([]student{}, func(s1, s2 student) int {
return s1.age - s2.age // age 小的先出
})
for i := 100; i > 0; i-- {
h.Push(student{fmt.Sprintf("name%d", i), i})
}
// 每次获取最小年龄的student
student := h.Pop()
fmt.Println(student.age)
student = h.Pop()
fmt.Println(student.age)
// Output:
// 1
// 2
}
// An IntHeap is a min-heap of ints
type IntHeap []int
// This example inserts several ints into an IntHeap, checks the minimum,
// and removes them in order of priority.
func Example_intHeap() {
h := gdk.NewHeap(IntHeap{2, 1, 5}, func(i1, i2 int) int { return i1 - i2 })
h.Push(3)
for h.Len() > 0 {
fmt.Printf("%d ", h.Pop())
}
// Output:
// 1 2 3 5
}