Debian Codegraph @

examples/binaryheap/binaryheap.go

raw symbols SCIP
1 // Copyright (c) 2015, Emir Pasic. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package main
6
7 import (
8 "cmp"
9
10 "github.com/emirpasic/gods/v2/trees/binaryheap"
11 )
12
13 // BinaryHeapExample to demonstrate basic usage of BinaryHeap
14 func main() {
15
16 // Min-heap
17 heap := binaryheap.New[int]() // empty (min-heap)
18 heap.Push(2) // 2
19 heap.Push(3) // 2, 3
20 heap.Push(1) // 1, 3, 2
21 heap.Values() // 1, 3, 2
22 _, _ = heap.Peek() // 1,true
23 _, _ = heap.Pop() // 1, true
24 _, _ = heap.Pop() // 2, true
25 _, _ = heap.Pop() // 3, true
26 _, _ = heap.Pop() // nil, false (nothing to pop)
27 heap.Push(1) // 1
28 heap.Clear() // empty
29 heap.Empty() // true
30 heap.Size() // 0
31
32 // Max-heap
33 inverseIntComparator := func(a, b int) int {
34 return -cmp.Compare(a, b)
35 }
36 heap = binaryheap.NewWith(inverseIntComparator) // empty (min-heap)
37 heap.Push(2) // 2
38 heap.Push(3) // 3, 2
39 heap.Push(1) // 3, 2, 1
40 heap.Values() // 3, 2, 1
41 }