mirror of
https://github.com/emirpasic/gods
synced 2024-11-18 09:25:50 +00:00
41 lines
1.5 KiB
Go
41 lines
1.5 KiB
Go
// 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.
|
|
|
|
package examples
|
|
|
|
import (
|
|
"github.com/emirpasic/gods/trees/binaryheap"
|
|
"github.com/emirpasic/gods/utils"
|
|
)
|
|
|
|
// BinaryHeapExample to demonstrate basic usage of BinaryHeap
|
|
func BinaryHeapExample() {
|
|
|
|
// 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
|
|
}
|