mirror of https://github.com/emirpasic/gods
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
41 lines
1.5 KiB
Go
41 lines
1.5 KiB
Go
8 years ago
|
// Copyright (c) 2015, Emir Pasic. All rights reserved.
|
||
|
// Use of this source code is governed by a BSD-style
|
||
|
// license that can be found in the LICENSE file.
|
||
9 years ago
|
|
||
6 years ago
|
package main
|
||
9 years ago
|
|
||
|
import (
|
||
|
"github.com/emirpasic/gods/trees/binaryheap"
|
||
|
"github.com/emirpasic/gods/utils"
|
||
|
)
|
||
|
|
||
8 years ago
|
// BinaryHeapExample to demonstrate basic usage of BinaryHeap
|
||
6 years ago
|
func main() {
|
||
9 years ago
|
|
||
|
// Min-heap
|
||
|
heap := binaryheap.NewWithIntComparator() // empty (min-heap)
|
||
|
heap.Push(2) // 2
|
||
|
heap.Push(3) // 2, 3
|
||
|
heap.Push(1) // 1, 3, 2
|
||
|
heap.Values() // 1, 3, 2
|
||
|
_, _ = heap.Peek() // 1,true
|
||
|
_, _ = heap.Pop() // 1, true
|
||
|
_, _ = heap.Pop() // 2, true
|
||
|
_, _ = heap.Pop() // 3, true
|
||
|
_, _ = heap.Pop() // nil, false (nothing to pop)
|
||
|
heap.Push(1) // 1
|
||
|
heap.Clear() // empty
|
||
|
heap.Empty() // true
|
||
|
heap.Size() // 0
|
||
|
|
||
|
// Max-heap
|
||
|
inverseIntComparator := func(a, b interface{}) int {
|
||
|
return -utils.IntComparator(a, b)
|
||
|
}
|
||
|
heap = binaryheap.NewWith(inverseIntComparator) // empty (min-heap)
|
||
|
heap.Push(2) // 2
|
||
|
heap.Push(3) // 3, 2
|
||
|
heap.Push(1) // 3, 2, 1
|
||
|
heap.Values() // 3, 2, 1
|
||
|
}
|