-
Notifications
You must be signed in to change notification settings - Fork 50
/
heap.go
198 lines (179 loc) · 3.99 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Fast generic heap algorithms, faster than [container/heap]
package stl4go
// MakeMinHeap build a min-heap on slice array.
//
// Complexity: O(len(array))
func MakeMinHeap[T Ordered](array []T) {
// heapify
n := len(array)
for i := n/2 - 1; i >= 0; i-- {
heapDown(array, i, n)
}
}
// IsMinHeap checks whether the elements in slice array are a min heap.
//
// Complexity: O(len(array)).
func IsMinHeap[T Ordered](array []T) bool {
parent := 0
for child := 1; child < len(array); child++ {
if array[parent] > array[child] {
return false
}
if (child & 1) == 0 {
parent++
}
}
return true
}
// PushMinHeap pushes a element v into the min heap.
//
// Complexity: O(log(len(*heap))).
func PushMinHeap[T Ordered](heap *[]T, v T) {
*heap = append(*heap, v)
heapUp(*heap, len(*heap)-1)
}
// PopMinHeap removes and returns the minimum element from the heap.
//
// Complexity: O(log n) where n = len(*heap).
func PopMinHeap[T Ordered](heap *[]T) T {
h := *heap
n := len(h) - 1
heapSwap(h, 0, n)
heapDown(h, 0, n)
*heap = h[0:n]
return h[n]
}
// RemoveMinHeap removes and returns the element at index i from the min heap.
//
// Complexity: is O(log(n)) where n = len(*heap).
func RemoveMinHeap[T Ordered](heap *[]T, i int) T {
h := *heap
n := len(h) - 1
if n != i {
heapSwap(h, i, n)
if !heapDown(h, i, n) {
heapUp(h, i)
}
}
*heap = h[0:n]
return h[n]
}
func heapSwap[T any](heap []T, i, j int) {
heap[i], heap[j] = heap[j], heap[i]
}
func heapUp[T Ordered](heap []T, j int) {
for {
i := (j - 1) / 2 // parent
if i == j || !(heap[j] < heap[i]) {
break
}
heapSwap(heap, i, j)
j = i
}
}
func heapDown[T Ordered](heap []T, i0, n int) bool {
i := i0
for {
j1 := 2*i + 1
if j1 >= n || j1 < 0 { // j1 < 0 after int overflow
break
}
j := j1 // left child
if j2 := j1 + 1; j2 < n && heap[j2] < heap[j1] {
j = j2 // = 2*i + 2 // right child
}
if !(heap[j] < heap[i]) {
break
}
heapSwap(heap, i, j)
i = j
}
return i > i0
}
// MakeHeapFunc build a min-heap on slice array with compare function less.
//
// Complexity: O(len(array))
func MakeHeapFunc[T any](array []T, less LessFn[T]) {
// heapify
n := len(array)
for i := n/2 - 1; i >= 0; i-- {
heapDownFunc(array, i, n, less)
}
}
// IsHeapFunc checks whether the elements in slice array are a min heap (accord to less).
//
// Complexity: O(len(array)).
func IsHeapFunc[T any](array []T, less LessFn[T]) bool {
parent := 0
for child := 1; child < len(array); child++ {
if !less(array[parent], array[child]) {
return false
}
if (child & 1) == 0 {
parent++
}
}
return true
}
// PushHeapFunc pushes a element v into the heap.
//
// Complexity: O(log(len(*heap))).
func PushHeapFunc[T any](heap *[]T, v T, less LessFn[T]) {
*heap = append(*heap, v)
heapUpFunc(*heap, len(*heap)-1, less)
}
// PopHeapFunc removes and returns the minimum (according to less) element from the heap.
//
// Complexity: O(log n) where n = len(*heap).
func PopHeapFunc[T any](heap *[]T, less LessFn[T]) T {
h := *heap
n := len(h) - 1
heapSwap(h, 0, n)
heapDownFunc(h, 0, n, less)
*heap = h[0:n]
return h[n]
}
// RemoveHeapFunc removes and returns the element at index i from the heap.
//
// Complexity: is O(log(n)) where n = len(*heap).
func RemoveHeapFunc[T any](heap *[]T, i int, less LessFn[T]) T {
h := *heap
n := len(h) - 1
if n != i {
heapSwap(h, i, n)
if !heapDownFunc(h, i, n, less) {
heapUpFunc(h, i, less)
}
}
*heap = h[0:n]
return h[n]
}
func heapUpFunc[T any](heap []T, j int, less LessFn[T]) {
for {
i := (j - 1) / 2 // parent
if i == j || !less(heap[j], heap[i]) {
break
}
heapSwap(heap, i, j)
j = i
}
}
func heapDownFunc[T any](heap []T, i0, n int, less LessFn[T]) bool {
i := i0
for {
j1 := 2*i + 1
if j1 >= n || j1 < 0 { // j1 < 0 after int overflow
break
}
j := j1 // left child
if j2 := j1 + 1; j2 < n && less(heap[j2], heap[j1]) {
j = j2 // = 2*i + 2 // right child
}
if !less(heap[j], heap[i]) {
break
}
heapSwap(heap, i, j)
i = j
}
return i > i0
}