-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.go
50 lines (39 loc) · 802 Bytes
/
model.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
package main
// We need two types of structs implementing HuffmanEle: Node and Leaf
type HuffmanEle interface {
getValue() int
}
type Node struct {
freq int
left HuffmanEle
right HuffmanEle
}
type Leaf struct {
freq int
char rune
}
type HuffmanHeap []HuffmanEle
// Functions to implement Interfaces
func (n Node) getValue() int {
return n.freq
}
func (l Leaf) getValue() int {
return l.freq
}
func (h HuffmanHeap) Len() int {
return len(h)
}
func (h HuffmanHeap) Less(i int, j int) bool {
return h[i].getValue() < h[j].getValue()
}
func (h HuffmanHeap) Swap(i int, j int) {
h[i], h[j] = h[j], h[i]
}
func (h *HuffmanHeap) Push(i any) {
*h = append(*h, i.(HuffmanEle))
}
func (h *HuffmanHeap) Pop() any {
poppedEle := (*h)[len(*h)-1]
*h = (*h)[:len(*h)-1]
return poppedEle
}