-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheap.go
94 lines (77 loc) · 1.77 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
package heap
import "errors"
type MinHeap struct {
data []int
length int
}
func (heap *MinHeap) Insert(value int) {
heap.data = append(heap.data, value)
heap.heapifyUp(heap.length)
heap.length += 1
}
func (heap *MinHeap) Delete() (int, error) {
if heap.length == 0 {
return 0, errors.New("empty heap")
}
out := heap.data[0]
heap.length -= 1
heap.data[0] = heap.data[heap.length]
heap.heapifyDown(0)
return out, nil
}
func (heap *MinHeap) Data() []int {
return heap.data[:heap.length]
}
func (heap *MinHeap) heapifyDown(idx int) {
leftChildIdx := heap.leftChildIdx(idx)
rightChildIdx := heap.rightChildIdx(idx)
if idx >= heap.length {
return
}
// Check whether node at `idx` has children
if leftChildIdx >= heap.length {
return
}
value := heap.data[idx]
leftChild := heap.data[leftChildIdx]
rightChild := heap.data[rightChildIdx]
if leftChild < rightChild && leftChild < value {
heap.swap(idx, leftChildIdx)
heap.heapifyDown(leftChildIdx)
return
}
if rightChild < leftChild && rightChild < value {
heap.swap(idx, rightChildIdx)
heap.heapifyDown(rightChildIdx)
}
}
func (heap *MinHeap) heapifyUp(idx int) {
if idx == 0 {
return
}
parentIndex := heap.parentIndex(idx)
value := heap.data[idx]
parent := heap.data[heap.parentIndex(idx)]
if value > parent {
return
}
heap.swap(idx, parentIndex)
heap.heapifyUp(parentIndex)
}
func (heap *MinHeap) parentIndex(idx int) int {
return (idx - 1) / 2
}
func (heap *MinHeap) leftChildIdx(idx int) int {
return (2 * idx) + 1
}
func (heap *MinHeap) rightChildIdx(idx int) int {
return (2 * idx) + 2
}
func (heap *MinHeap) swap(idx1 int, idx2 int) {
temp := heap.data[idx1]
heap.data[idx1] = heap.data[idx2]
heap.data[idx2] = temp
}
func NewMinHeap() *MinHeap {
return &MinHeap{data: []int{}}
}