-
Notifications
You must be signed in to change notification settings - Fork 0
/
heaps.go
123 lines (103 loc) · 2.74 KB
/
heaps.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
package main
import (
"fmt"
"log"
)
// MaxHeap struct has a slice that holds the array
type MaxHeap struct {
array []int
}
// Insert adds an element to the heap
func (h *MaxHeap) Insert(key int) {
h.array = append(h.array, key)
h.maxHeapifyUp(len(h.array) - 1)
}
// Extract return the largest key, and removes it from the heap
func (h *MaxHeap) Extract() int {
// Return the extracted item
extracted := h.array[0]
length := len(h.array) - 1
if len(h.array) == 0 {
log.Fatal("Cannot extract! Bacause is a invalid heap with 0 items inside")
return -1
}
// Get the last key index and put into the first index (Root)
h.array[0] = h.array[length]
// Now here is a slice method
h.array = h.array[:length]
h.maxHeapifyDown(0)
return extracted
}
// Swap the values the current index is larger or biger than parent
func (h *MaxHeap) maxHeapifyUp(index int) {
// MaxHeapify will heapify from brottom to top
for h.array[parent(index)] < h.array[index] {
// This part is swaping until it find its place
h.swap(parent(index), index)
// Update value
index = parent(index)
}
}
// MaxHeapifyDown will heapify top to bottom
func (h *MaxHeap) maxHeapifyDown(index int) {
// Declare the variables to compare
lastIndex := len(h.array) - 1
l, r := left(index), right(index)
childToCompare := 0
// Loop while index has at least one child
for l <= lastIndex {
if l == lastIndex {
// When left child is the only child
childToCompare = l
} else if h.array[l] > h.array[r] {
// When left child is larger
childToCompare = l
} else {
// When the right child is larger
childToCompare = r
}
// Compare array value of current index to larger child and swap if smaller
if h.array[index] < h.array[childToCompare] {
h.swap(index, childToCompare)
index = childToCompare
l, r = left(index), right(index)
} else {
// If the current index is actually larger than larger child that means that he found the correct place
return
}
}
}
// Get the parent index value
// i = parent
func parent(i int) int {
return (i - 1) / 2
}
// Get the left child index value
// i = parent
func left(i int) int {
return 2*i + 1
}
// Get the right child index value
// i = parent
func right(i int) int {
return 2*i + 2
}
// Swap keys in the array
func (h *MaxHeap) swap(i1, i2 int) {
h.array[i1], h.array[i2] = h.array[i2], h.array[i1]
}
func main() {
// Creating a Heap (& => Address variable)
m := &MaxHeap{}
fmt.Println(m)
// Add some keys
buildHeap := []int{10, 20, 30, 5, 7, 9, 11, 13, 15, 17}
for _, v := range buildHeap {
m.Insert(v)
fmt.Println(m)
}
for i := 0; i < 5; i++ {
m.Extract()
fmt.Println(m)
}
}