forked from TheAlgorithms/Go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
heap.go
98 lines (86 loc) · 2.05 KB
/
heap.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package heap
import (
"errors"
"github.com/TheAlgorithms/Go/constraints"
)
// Heap heap implementation using generic.
type Heap[T any] struct {
heaps []T
lessFunc func(a, b T) bool
}
// New gives a new heap object.
func New[T constraints.Ordered]() *Heap[T] {
less := func(a, b T) bool {
return a < b
}
h, _ := NewAny[T](less)
return h
}
// NewAny gives a new heap object. element can be anything, but must provide less function.
func NewAny[T any](less func(a, b T) bool) (*Heap[T], error) {
if less == nil {
return nil, errors.New("less func is necessary")
}
return &Heap[T]{
lessFunc: less,
}, nil
}
// Push pushes the element t onto the heap.
// The complexity is O(log n) where n = h.Len().
func (h *Heap[T]) Push(t T) {
h.heaps = append(h.heaps, t)
h.up(len(h.heaps) - 1)
}
// Top returns the minimum element (according to Less) from the heap.
// Top panics if the heap is empty.
func (h *Heap[T]) Top() T {
return h.heaps[0]
}
// Pop removes the minimum element (according to Less) from the heap.
// The complexity is O(log n) where n = h.Len().
func (h *Heap[T]) Pop() {
if len(h.heaps) <= 1 {
h.heaps = nil
return
}
h.swap(0, len(h.heaps)-1)
h.heaps = h.heaps[:len(h.heaps)-1]
h.down(0)
}
// Empty returns the heap is empty or not.
func (h *Heap[T]) Empty() bool {
return len(h.heaps) == 0
}
// Size returns the size of the heap
func (h *Heap[T]) Size() int {
return len(h.heaps)
}
func (h *Heap[T]) swap(i, j int) {
h.heaps[i], h.heaps[j] = h.heaps[j], h.heaps[i]
}
func (h *Heap[T]) up(child int) {
if child <= 0 {
return
}
parent := (child - 1) >> 1
if !h.lessFunc(h.heaps[child], h.heaps[parent]) {
return
}
h.swap(child, parent)
h.up(parent)
}
func (h *Heap[T]) down(parent int) {
lessIdx := parent
lChild, rChild := (parent<<1)+1, (parent<<1)+2
if lChild < len(h.heaps) && h.lessFunc(h.heaps[lChild], h.heaps[lessIdx]) {
lessIdx = lChild
}
if rChild < len(h.heaps) && h.lessFunc(h.heaps[rChild], h.heaps[lessIdx]) {
lessIdx = rChild
}
if lessIdx == parent {
return
}
h.swap(lessIdx, parent)
h.down(lessIdx)
}