summaryrefslogtreecommitdiff
path: root/pkg/priority_queue/binary_heap.go
blob: 7e034e8277229160d82be8d45862a9705c54bef4 (plain)
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
/*
binary heap (min-heap) algorithm used as a core for the priority queue
*/

package priorityqueue

type BinHeap []PQItem

func NewBinHeap() *BinHeap {
	return &BinHeap{}
}

func (bh *BinHeap) Init(items []PQItem) {
	arraySize := len(items) - 1

	for i := arraySize/2 - 1; i >= 0; i-- {
		bh.shiftDown(items, i, arraySize)
	}

	for i := arraySize - 1; i >= 1; i-- {
		items[0], items[i] = items[i], items[0]
		bh.shiftDown(items, 0, i-1)
	}
}

func (bh *BinHeap) shiftDown(numbers []PQItem, k, n int) {
	// k << 1 is equal to k*2
	for k<<1 <= n {
		j := k << 1

		if j < n && numbers[j].Priority() < numbers[j+1].Priority() {
			j++
		}

		if !(numbers[k].Priority() < numbers[j].Priority()) {
			break
		}

		numbers[k], numbers[j] = numbers[j], numbers[k]
		k = j
	}
}
func (bh *BinHeap) fix() {}

func (bh *BinHeap) Push(_ PQItem) {}

func (bh *BinHeap) Pop() PQItem {
	bh.fix()
	// get min
	return nil
}