-
Notifications
You must be signed in to change notification settings - Fork 0
/
heap_example_test.go
64 lines (49 loc) · 1.2 KB
/
heap_example_test.go
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
59
60
61
62
63
64
package fibheap_test
import (
"fmt"
"github.com/ksw2000/go-fibheap"
)
func ExampleHeap() {
h := &fibheap.Heap[int, string]{}
h.Insert(3, "three")
h.Insert(2, "two")
h.Insert(1, "one")
min := h.ExtractMin()
fmt.Println(min.Key(), min.Value)
min = h.Min()
fmt.Println(min.Key(), min.Value)
// Output: 1 one
//2 two
}
func ExampleHeap_Decreasing() {
h := &fibheap.Heap[int, string]{}
list := []*fibheap.Element[int, string]{}
list = append(list, h.Insert(5, "one"))
list = append(list, h.Insert(6, "two"))
list = append(list, h.Insert(7, "three"))
h.Decreasing(list[0], 1)
h.Decreasing(list[1], 2)
h.Decreasing(list[2], 3)
min := h.ExtractMin()
fmt.Println(min.Key(), min.Value)
min = h.ExtractMin()
fmt.Println(min.Key(), min.Value)
min = h.ExtractMin()
fmt.Println(min.Key(), min.Value)
// Output: 1 one
//2 two
//3 three
}
func ExampleHeap_Remove() {
h := &fibheap.Heap[int, any]{}
list := []*fibheap.Element[int, any]{}
list = append(list, h.Insert(5, nil))
list = append(list, h.Insert(6, nil))
list = append(list, h.Insert(7, nil))
h.Remove(list[0], 0)
h.Remove(list[1], 0)
fmt.Println("size:", h.Size())
fmt.Println("min:", h.Min().Key())
// Output: size: 3
//min: 7
}