gitee.com/quant1x/gox@v1.21.2/util/examples/binaryheap/binaryheap.go (about) 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 "gitee.com/quant1x/gox/util/binaryheap" 9 "gitee.com/quant1x/gox/util/internal" 10 ) 11 12 // BinaryHeapExample to demonstrate basic usage of BinaryHeap 13 func main() { 14 15 // Min-heap 16 heap := binaryheap.NewWithIntComparator() // empty (min-heap) 17 heap.Push(2) // 2 18 heap.Push(3) // 2, 3 19 heap.Push(1) // 1, 3, 2 20 heap.Values() // 1, 3, 2 21 _, _ = heap.Peek() // 1,true 22 _, _ = heap.Pop() // 1, true 23 _, _ = heap.Pop() // 2, true 24 _, _ = heap.Pop() // 3, true 25 _, _ = heap.Pop() // nil, false (nothing to pop) 26 heap.Push(1) // 1 27 heap.Clear() // empty 28 heap.Empty() // true 29 heap.Size() // 0 30 31 // Max-heap 32 inverseIntComparator := func(a, b interface{}) int { 33 return -internal.IntComparator(a, b) 34 } 35 heap = binaryheap.NewWith(inverseIntComparator) // empty (min-heap) 36 heap.Push(2) // 2 37 heap.Push(3) // 3, 2 38 heap.Push(1) // 3, 2, 1 39 heap.Values() // 3, 2, 1 40 }