-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinHeap.go
430 lines (391 loc) · 10.5 KB
/
binHeap.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
package gheap
type (
// This allows calculation of either integer or float scores
Number interface {
~int | ~int32 | ~int64 | ~float32 | ~float64
}
HeapElement[S Number, T any] struct {
Key S
Data T
}
MaxHeap[S Number, T any] []HeapElement[S, T]
MinHeap[S Number, T any] []HeapElement[S, T]
MinMaxHeap[S Number, T any] []HeapElement[S, T]
)
func GreaterThan[S Number](a, b S) bool {
return a > b
}
func LessThan[S Number](a, b S) bool {
return a < b
}
func parent(i int) int {
return (i - 1) / 2
}
func leftChild(i int) int {
return i*2 + 1
}
func rightChild(i int) int {
return i*2 + 2
}
// *****************************************
// Below are functions that implement MinHeap
// *****************************************
func (b MinHeap[S, T]) shiftUp(i int) {
p := parent(i)
for i > 0 && b[p].Key > b[i].Key {
b[p], b[i] = b[i], b[p]
i = p
p = parent(i)
}
}
func (b MinHeap[S, T]) shiftDown(i int) {
for {
maxIndex := i
l := leftChild(i)
if l < len(b) && b[l].Key < b[maxIndex].Key {
maxIndex = l
}
r := rightChild(i)
if r < len(b) && b[r].Key < b[maxIndex].Key {
maxIndex = r
}
if i == maxIndex { // If i is maxIndex, we are done!
break
}
b[i], b[maxIndex] = b[maxIndex], b[i]
i = maxIndex
}
}
// Insert adds element to the heap and maintains the
// min heap order
func (b *MinHeap[S, T]) Insert(element HeapElement[S, T]) {
*b = append(*b, element)
b.shiftUp(len(*b) - 1) // Maintain the heap property
}
// RemoveMin returns the element with the highest key
// and removes it from the heap.
func (b *MinHeap[S, T]) RemoveMin() (result HeapElement[S, T]) {
result = (*b)[0]
(*b)[0] = (*b)[len(*b)-1] // Replace the value at the root with the last leaf
*b = (*b)[:len(*b)-1] // Shorten slice by one
b.shiftDown(0) // Maintain the heap property
return
}
// PeekMain returns the element with the lowest key
// without removing it.
func (b MinHeap[S, T]) PeekMin() HeapElement[S, T] {
return b[0]
}
// Copy returns a shallow copy of the heap
func (b MinHeap[S, T]) Copy() (c MinHeap[S, T]) {
c = append([]HeapElement[S, T]{}, b...)
return
}
// *****************************************
// Below are functions that implement MaxHeap
// *****************************************
func (b MaxHeap[S, T]) shiftUp(i int) {
p := parent(i)
for i > 0 && b[p].Key < b[i].Key {
b[p], b[i] = b[i], b[p]
i = p
p = parent(i)
}
}
func (b MaxHeap[S, T]) shiftDown(i int) {
for {
maxIndex := i
l := leftChild(i)
if l < len(b) && b[l].Key > b[maxIndex].Key {
maxIndex = l
}
r := rightChild(i)
if r < len(b) && b[r].Key > b[maxIndex].Key {
maxIndex = r
}
if i == maxIndex { // If i is maxIndex, we are done!
break
}
b[i], b[maxIndex] = b[maxIndex], b[i]
i = maxIndex
}
}
// Insert adds element to the heap and maintains the
// max heap order
func (b *MaxHeap[S, T]) Insert(element HeapElement[S, T]) {
*b = append(*b, element)
b.shiftUp(len(*b) - 1) // Maintain the heap property
}
// PeekMax returns the element with the highest key
// without removing it.
func (b MaxHeap[S, T]) PeekMax() HeapElement[S, T] {
return b[0]
}
// RemoveMax returns the element with the highest key
// and removes it from the heap.
func (b *MaxHeap[S, T]) RemoveMax() (result HeapElement[S, T]) {
result = (*b)[0]
(*b)[0] = (*b)[len(*b)-1] // Replace the value at the root with the last leaf
*b = (*b)[:len(*b)-1] // Shorten slice by one
b.shiftDown(0) // Maintain the heap property
return
}
// Copy returns a shallow copy of the heap
func (b MaxHeap[S, T]) Copy() (c MaxHeap[S, T]) {
c = append([]HeapElement[S, T]{}, b...)
return
}
// *****************************************
// Below are functions that implement MinMaxHeap
// *****************************************
// IsMinLevel returns true if and only if i is the index
// of an element in a minimum level of the MinMaxHeap
func IsMinLevel(i int) (isMin bool) {
i++
for i > 0 {
i >>= 1
isMin = !isMin
}
return
}
// pushGranny takes an index i and if it has a grand parent to which it
// compares affirmatively to the key of i, the elements are swapped
// and the process is repeated until there is no grand parent.
func (h MinMaxHeap[S, T]) pushGranny(i int, compare func(a, b S) bool) {
for i > 2 { // Every index greater than 2 has a grand parent
gp := parent(parent(i))
if compare(h[i].Key, h[gp].Key) {
h[i], h[gp] = h[gp], h[i]
}
i = gp
}
}
func (h MinMaxHeap[S, T]) pushUp(i int) {
if i > 0 {
p := parent(i)
if IsMinLevel(i) {
if h[i].Key > h[p].Key {
h[i], h[p] = h[p], h[i]
h.pushGranny(p, GreaterThan[S])
} else {
h.pushGranny(i, LessThan[S])
}
} else {
if h[i].Key < h[p].Key {
h[i], h[p] = h[p], h[i]
h.pushGranny(p, LessThan[S])
} else {
h.pushGranny(i, GreaterThan[S])
}
}
}
}
// pushDown maintains heap order following a removal operation
// the compare function is either greaterThan or lessThan
func (h MinMaxHeap[S, T]) pushDown(i int, compare func(a, b S) bool) {
for {
l := leftChild(i)
if l >= len(h) { // No Children for this node, we are done.
break
}
minIndex := l
r := rightChild(i)
// The children and grand children indices of i that need
// to be tested are arranged in ascending order.
for _, v := range [5]int{r, leftChild(l), rightChild(l), leftChild(r), rightChild(r)} {
if v >= len(h) {
break // No kids beyond this point
}
if compare(h[v].Key, h[minIndex].Key) {
minIndex = v
}
}
// Now that minIndex is identified, compare to i.
if compare(h[minIndex].Key, h[i].Key) {
h[minIndex], h[i] = h[i], h[minIndex]
if minIndex > r { // minIndex is a grand child of i
p := parent(minIndex)
if compare(h[p].Key, h[minIndex].Key) {
h[minIndex].Key, h[p].Key = h[p].Key, h[minIndex].Key
}
}
}
i = minIndex
}
}
// Insert adds element to the heap and maintains the
// minmax heap order
func (b *MinMaxHeap[S, T]) Insert(element HeapElement[S, T]) {
*b = append(*b, element)
b.pushUp(len(*b) - 1)
}
// RemoveMin returns the element with the lowest key
// and removes it from the heap
func (b *MinMaxHeap[S, T]) RemoveMin() (result HeapElement[S, T]) {
result = (*b)[0]
(*b)[0] = (*b)[len(*b)-1] // Replace the value at the root with the last leaf
(*b) = (*b)[:len(*b)-1] // Shorten slice by one
b.pushDown(0, LessThan[S]) // Maintain heap property
return
}
// RemoveMax returns the element with the highest key
// and removes it from the heap
func (b *MinMaxHeap[S, T]) RemoveMax() (result HeapElement[S, T]) {
switch len(*b) {
case 0:
return
case 1:
result = (*b)[0]
*b = (*b)[:0]
return
case 2:
result = (*b)[1]
*b = (*b)[:1]
default:
i := 1
if (*b)[1].Key < (*b)[2].Key {
i = 2
}
result = (*b)[i]
(*b)[i] = (*b)[len(*b)-1] // Replace the value at the removed node
*b = (*b)[:len(*b)-1] // Shorten slice by one
b.pushDown(i, GreaterThan[S]) // maintain heap property
}
return
}
// PeekMin returns the element with the lowest key
// without removing it.
func (b *MinMaxHeap[S, T]) PeekMin() HeapElement[S, T] {
return (*b)[0]
}
// PeekMax returns the element with the highest key
// without removing it.
func (b MinMaxHeap[S, T]) PeekMax() (result HeapElement[S, T]) {
switch len(b) {
case 0:
return
case 1:
result = b[0]
case 2:
result = b[1]
default:
if b[1].Key < b[2].Key {
result = b[2]
} else {
result = b[1]
}
}
return
}
// Copy returns a shallow copy of the heap
func (b MinMaxHeap[S, T]) Copy() (c MinMaxHeap[S, T]) {
c = append([]HeapElement[S, T]{}, b...)
return
}
// GetIterator returns a function that iterates over the elements in the MinMaxHeap b,
// without altering the contents of b.
// If ascending is true the iterator returns the elements in ascending order,
// otherwise the elements are returned in decending order.
// The return value r indicates how many elements remain in the iterator.
// Additional calls to the iterator after all the elements are exhausted (r < 0) will not
// seq fault, but the returned element will contain default values for the Key
// and Data types of the HeapElement.
// Instances of the iterator maintain an internal MaxHeap which will
// gradually grow as more elements are requested. The greatest size of this
// slice occurs near the half way point of the full iteration, and decreases after
// until the size of the slice and the remaining count are zero.
// Depending on the organization of b, the maximum size of the iterator slice map
// be around 1/2 of the length of b, but is usually around 1/3 or less.
func (b MinMaxHeap[S, T]) GetIterator(ascending bool) func() (e HeapElement[S, T], r int) {
maxHeap := MinMaxHeap[S, int]{}
var addToIterator func(i int)
if ascending {
addToIterator = func(i int) {
maxHeap.Insert(HeapElement[S, int]{-b[i].Key, i})
}
} else {
addToIterator = func(i int) {
maxHeap.Insert(HeapElement[S, int]{b[i].Key, i})
}
}
// Set the bounds for the initial nodes
start, end := 1, 3 // Seed the two greatest max nodes
if ascending {
start, end = 0, 1 // Seed the root min node
}
for i := start; i < len(b) && i < end; i++ {
addToIterator(i)
}
remaining := len(b)
return func() (e HeapElement[S, T], rem int) {
remaining--
rem = remaining
if len(maxHeap) == 0 {
return
}
top := maxHeap.RemoveMax().Data
e = b[top]
if IsMinLevel(top) != ascending { // Going back up the tree
if top > 2 {
gp := parent(parent(top))
if leftChild(leftChild(gp)) == top {
addToIterator(gp)
}
}
return
}
l := leftChild(top)
if l >= len(b) { // top has no children nodes, go back up
if top > 2 {
p := parent(top)
if leftChild(p) == top {
addToIterator(p)
}
}
return
}
// Check both child node's children
for _, v := range [2]int{rightChild(top), l} {
vl := leftChild(v)
if vl < len(b) {
addToIterator(vl)
vr := rightChild(v)
if vr < len(b) {
addToIterator(vr)
}
} else {
if v < len(b) { // No child nodes go back up
addToIterator(v)
}
}
}
return
}
}
// *****************************************
// Below are some slice helper functions
// *****************************************
// Fill sets all indicies of a to b
func Fill[T any](a []T, b T) {
for i := range a {
a[i] = b
}
}
// Equals returns true if the elements of a and b are the same
func Equals[T comparable](a, b []T) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}
// Reverse reverse of order of s
func Reverse[T any](s []T) {
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
}