-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhuffman.go
192 lines (150 loc) · 3.55 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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package main
import(
"fmt"
"os"
"container/heap"
)
//HUFFMAN-ALGO
//huffman|c|
//n=|c|
//build min-heap Q with c
//for i=1 to n-1
//allocate a new node Z
//Z.left = X = extract_min(Q)
//Z.right = Y = extract_min(Q)
//Z.freq = X.freq + Y.freq
//insert(Z,Q)
//return (extract_min(Q))
//representing a node in the huffman tree
type HuffmanNode struct {
char rune
//rune -> represent any character in the unicode standard
freq int
left *HuffmanNode
right *HuffmanNode
}
//priority queue for implementing a min-heap for node
type PriorityQueue []*HuffmanNode
func (pq PriorityQueue) Len() int {
return len(pq)
}
func (pq PriorityQueue) Less(i,j int) bool {
return pq[i].freq < pq[j].freq
}
func (pq PriorityQueue) Swap(i,j int) {
pq[i], pq[j] = pq[j], pq[i]
}
func (pq *PriorityQueue) Push(x interface{}) {
//interface -> a type that lists methods without providing their code
*pq = append(*pq, x.(*HuffmanNode))
}
func (pq *PriorityQueue) Pop() interface{} {
old := *pq
n := len(old)
item := old[n-1]
*pq = old[0 : n-1]
return item
}
//buiding the huffman tree
func buildHuffmanTree(freqMap map[rune]int) *HuffmanNode {
pq := &PriorityQueue{}
heap.Init(pq)
//adding the chars to priority queue
for char, freq := range freqMap {
heap.Push(pq, &HuffmanNode{char : char, freq : freq})
}
//building the tree
for pq.Len() > 1 {
left := heap.Pop(pq).(*HuffmanNode)
right := heap.Pop(pq).(*HuffmanNode)
newNode := &HuffmanNode{
freq: left.freq + right.freq,
left: left,
right: right,
}
heap.Push(pq, newNode)
}
return heap.Pop(pq).(*HuffmanNode)
}
//generating the huffman codes
func generateCodes(node *HuffmanNode, code string, codes map[rune]string) {
if node == nil {
return
}
if node.left == nil && node.right == nil {
codes[node.char] = code
return
}
generateCodes(node.left, code+"0", codes)
generateCodes(node.right, code+"1", codes)
}
//encoding the text
func encodeText(text string, codes map[rune]string) string {
encoded := ""
for _, char := range text {
encoded += codes[char]
}
return encoded
}
//decoding the text
func decodeText(encoded string, root *HuffmanNode) string {
result := ""
node := root
for _, bit := range encoded {
if bit == '0' {
node = node.left
} else {
node = node.right
}
if node.left == nil && node.right == nil {
result += string(node.char)
node = root
}
}
return result
}
//reading file and counting frequencies
func readFile(filename string) (string, map[rune]int) {
data, err := os.ReadFile(filename)
if err != nil {
panic(fmt.Sprintf("Error reading file: %v", err))
}
text := string(data)
freqMap := make(map[rune]int)
for _, char := range text {
freqMap[char]++
}
return text, freqMap
}
//writing file
func writeFile(filename, content string) {
err := os.WriteFile(filename, []byte(content), 0644)
if err != nil {
panic(fmt.Sprintf("Error writing to file: %v", err))
}
}
func main() {
//getting of the text file
inputFile := "input.txt"
text, freqMap := readFile(inputFile)
//building of huffman tree
root := buildHuffmanTree(freqMap)
//generating codes
codes := make(map[rune]string)
generateCodes(root, "", codes)
//encoding
encoded := encodeText(text, codes)
fmt.Println("Encoded Text:", encoded)
//decoding
decoded := decodeText(encoded, root)
fmt.Println("Decoded Text:", decoded)
//saving in another file
outputFile := "encoded.txt"
writeFile(outputFile, encoded)
fmt.Println("Encoded data saved to", outputFile)
if decoded == text {
fmt.Println("Decoding successful!")
} else {
fmt.Println("Decoding failed!")
}
}