-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinaryheap.go
142 lines (126 loc) · 3.24 KB
/
binaryheap.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
package gods
type BinaryHeapElement[K comparable] struct {
Key int
Value K
}
type BinaryHeap[K comparable] struct {
elements []BinaryHeapElement[K]
numElements int
position map[K]int
}
func NewBinaryHeap[K comparable](capacity int) *BinaryHeap[K] {
return &BinaryHeap[K]{elements: make([]BinaryHeapElement[K], capacity), numElements: 0, position: make(map[K]int)}
}
func (h *BinaryHeap[K]) Initialize(capacity int) {
h.elements = make([]BinaryHeapElement[K], capacity)
h.numElements = 0
h.position = make(map[K]int)
}
func (h *BinaryHeap[K]) Len() int {
return h.numElements
}
func (h *BinaryHeap[K]) ExtractMin() BinaryHeapElement[K] {
return h.deleteIndex(0)
}
func (h *BinaryHeap[K]) Peek() BinaryHeapElement[K] {
return h.elements[0]
}
func (h *BinaryHeap[K]) Insert(value K, key int) {
if h.numElements == len(h.elements) {
panic("heap full")
}
h.elements[h.numElements] = BinaryHeapElement[K]{key, value}
h.position[value] = h.numElements
h.numElements++
h.heapifyUp(h.numElements-1)
}
func (h *BinaryHeap[K]) ChangeKey(value K, newKey int) bool {
elementIndex, isInHeap := h.position[value]
if !isInHeap {
return false
}
oldKey := h.elements[elementIndex].Key
if oldKey == newKey {
return true
}
h.elements[elementIndex].Key = newKey
if oldKey < newKey {
h.heapifyDown(elementIndex)
} else {
h.heapifyUp(elementIndex)
}
return true
}
func (h *BinaryHeap[K]) DeleteElement(value K) (BinaryHeapElement[K], bool) {
elementIndex, isInHeap := h.position[value]
if !isInHeap {
return BinaryHeapElement[K]{}, false
}
return h.deleteIndex(elementIndex), true
}
func (h *BinaryHeap[K]) KeyOf(value K) (int, bool) {
elementIndex, isInHeap := h.position[value]
if !isInHeap {
return 0, false
}
return h.elements[elementIndex].Key, true
}
func (h *BinaryHeap[K]) deleteIndex(i int) BinaryHeapElement[K] {
toRemove := h.elements[i]
toSwap := h.elements[h.numElements-1]
h.elements[i] = toSwap
h.position[toSwap.Value] = i
h.elements[h.numElements-1] = BinaryHeapElement[K]{}
delete(h.position, toRemove.Value)
h.numElements--
h.heapifyDown(i)
return toRemove
}
func (h* BinaryHeap[K]) leftChildIndex(i int) int {
return 2*i+1
}
func (h* BinaryHeap[K]) parentIndex(i int) int {
if i == 0 {
return 0
}
return (i-1)/2
}
func (h *BinaryHeap[K]) heapifyUp(i int) {
if i > 0 {
pIndex := h.parentIndex(i)
currElement := h.elements[i]
parentElement := h.elements[pIndex]
if currElement.Key < parentElement.Key {
h.elements[pIndex] = currElement
h.position[currElement.Value] = pIndex
h.elements[i] = parentElement
h.position[parentElement.Value] = i
h.heapifyUp(pIndex)
}
}
}
func (h *BinaryHeap[K]) heapifyDown(i int) {
lcIndex := h.leftChildIndex(i)
if lcIndex > h.numElements - 1 {
return
}
var indexToSwap int
if lcIndex == h.numElements - 1 {
indexToSwap = lcIndex
} else {
if h.elements[lcIndex].Key < h.elements[lcIndex + 1].Key {
indexToSwap = lcIndex
} else {
indexToSwap = lcIndex + 1
}
}
childElement := h.elements[indexToSwap]
currElement := h.elements[i]
if childElement.Key < currElement.Key {
h.elements[indexToSwap] = currElement
h.position[currElement.Value] = indexToSwap
h.elements[i] = childElement
h.position[childElement.Value] = i
h.heapifyDown(indexToSwap)
}
}