-
Notifications
You must be signed in to change notification settings - Fork 2
/
huffman.go
112 lines (98 loc) · 1.94 KB
/
huffman.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
package main
import (
"bufio"
"fmt"
"log"
"os"
"strconv"
"github.com/emirpasic/gods/trees/binaryheap"
)
// Element is an element
type Element struct {
Nodes []int
Cost int
}
func byCost(a, b interface{}) int {
c1 := a.(Element)
c2 := b.(Element)
switch {
case c1.Cost > c2.Cost:
return 1
case c1.Cost < c2.Cost:
return -1
default:
return 0
}
}
func huffman(elements []int) (int, int) {
counters := make(map[int]int, 0) // Map vertex to count of times merged
heap := binaryheap.NewWith(byCost)
for _, element := range elements {
heap.Push(Element{
Nodes: []int{element},
Cost: element,
})
}
//-----------------------------------------
singleRound := func() {
t1, _ := heap.Pop()
t2, _ := heap.Pop()
nodes1 := t1.(Element).Nodes
nodes1 = append(nodes1, t2.(Element).Nodes...)
heap.Push(Element{
Nodes: nodes1,
Cost: t1.(Element).Cost + t2.(Element).Cost,
})
for _, vertex := range nodes1 {
if _, exist := counters[vertex]; !exist {
counters[vertex] = 1
} else {
counters[vertex] = counters[vertex] + 1
}
}
}
// Build the tree
for heap.Size() > 1 {
singleRound()
}
return findMinMax(counters)
}
func findMinMax(counters map[int]int) (int, int) {
min := 99999999999
max := 0
for _, v := range counters {
if v < min {
min = v
} else if v > max {
max = v
}
}
return min, max
}
func main() {
fmt.Println(huffman(loadData("./course3/week3/huffman/data.txt")))
}
func loadData(filepath string) []int {
data := make([]int, 0)
f, err := os.Open(filepath)
check(err)
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
parseRowIntoEntry(&data, scanner.Text())
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
return data[1:]
}
func parseRowIntoEntry(container *[]int, row string) {
if res, err := strconv.Atoi(row); err == nil {
*container = append(*container, res)
}
}
func check(err error) {
if err != nil {
log.Panicln(err)
}
}