-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
286 lines (240 loc) · 8.69 KB
/
main.go
File metadata and controls
286 lines (240 loc) · 8.69 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
Huffman Encoding Visualizer,
Step-by-step functionality still in development.
File includes:
- Simple Huffman Implementation
- Priority Queue using gostd library
- Tree building with m1gwings/treedrawer
Functionality:
- Printing/Formatting of:
- Occurrences of symbols
- Huffman binary codes
Argument handling:
- Handling of flags:
--count: Display character occurrence count
--codes: Display the Huffman codes table
--tree: Visualize the Huffman tree
Repository: ~ github.com/ml3m
*/
package main
import (
"bufio"
"flag"
"fmt"
"os"
"sort"
"container/heap"
)
// the greatest library created <3
import (
"github.com/m1gwings/treedrawer/tree"
)
// Huffman Coding struct
type HuffmanNode struct {
char rune
freq int
left *HuffmanNode
right *HuffmanNode
}
// Priority Queue implementation for a min-heap
type PriorityQueue []*HuffmanNode
// len
func (pq PriorityQueue) Len() int { return len(pq) }
func (pq PriorityQueue) Less(i, j int) bool {
if pq[i].freq == pq[j].freq {
return pq[i].char < pq[j].char // consistent order by character
}
return pq[i].freq < pq[j].freq // basically Min-heap based on frequency
}
func (pq PriorityQueue) Swap(i, j int) { pq[i], pq[j] = pq[j], pq[i] }
func (pq *PriorityQueue) Push(x interface{}) { *pq = append(*pq, x.(*HuffmanNode)) }
// poping what needed.
func (pq *PriorityQueue) Pop() interface{} {
old := *pq
n := len(old)
node := old[n-1]
*pq = old[0 : n-1]
return node
}
// Function to read the first line from the input file
func readInputFile(filePath string) (string, error) {
file, err := os.Open(filePath)
if err != nil {
return "", fmt.Errorf("error opening file: %w", err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
if !scanner.Scan() {
return "", fmt.Errorf("error reading file: %w", scanner.Err())
}
return scanner.Text(), nil
}
// function that builds the Huffman tree for the given argument input
func BuildHuffmanTree(input string) *HuffmanNode {
freqMap := make(map[rune]int)
for _, char := range input {
freqMap[char]++
}
// create a slice to hold the frequencies and characters
type freqEntry struct {
char rune
freq int
}
frequencyEntries := make([]freqEntry, 0, len(freqMap))
for char, freq := range freqMap {
frequencyEntries = append(frequencyEntries, freqEntry{char: char, freq: freq})
}
// Sort the frequency entries first by frequency (descending), then by character (ascending)
// Sort by ASCII value -> provides a better visual experience
// these processings helps also for formating.
sort.Slice(frequencyEntries, func(i, j int) bool {
if frequencyEntries[i].freq == frequencyEntries[j].freq {
return frequencyEntries[i].char < frequencyEntries[j].char
}
return frequencyEntries[i].freq > frequencyEntries[j].freq // Sort by frequency descending
})
// Create a priority queue and add all characters to it
pq := &PriorityQueue{}
for _, entry := range frequencyEntries {
heap.Push(pq, &HuffmanNode{char: entry.char, freq: entry.freq})
}
// Build the Huffman tree
for pq.Len() > 1 {
right := heap.Pop(pq).(*HuffmanNode) // Pop right (higher frequency)
left := heap.Pop(pq).(*HuffmanNode) // Pop left (lower frequency)
newNode := &HuffmanNode{freq: left.freq + right.freq, left: left, right: right}
heap.Push(pq, newNode)
}
return heap.Pop(pq).(*HuffmanNode) // Return the root of the tree
}
// GenerateHuffmanCodes generates Huffman codes for each character
func GenerateHuffmanCodes(node *HuffmanNode, code string, codes map[rune]string) {
if node != nil {
if node.left == nil && node.right == nil {
codes[node.char] = code // Leaf node, store the code
}
GenerateHuffmanCodes(node.left, code+"0", codes)
GenerateHuffmanCodes(node.right, code+"1", codes)
}
}
// DrawHuffmanTree visualizes the Huffman tree using treedrawer (the greatest go library)
func DrawHuffmanTree(node *HuffmanNode, parent *tree.Tree, freqMap map[rune]int) {
if node != nil {
var current *tree.Tree
if node.left == nil && node.right == nil {
// Display character with frequency on leaf node
current = parent.AddChild(tree.NodeString(fmt.Sprintf("%c (%d)", node.char, freqMap[node.char])))
} else {
current = parent.AddChild(tree.NodeInt64(int64(node.freq))) // Internal node
}
DrawHuffmanTree(node.left, current, freqMap)
DrawHuffmanTree(node.right, current, freqMap)
}
}
// PrintFrequencyArray prints the frequency array sorted by frequency and ASCII
func PrintFrequencyArray(freqMap map[rune]int) {
fmt.Println("Frequency Array:")
fmt.Println("----------------")
type freqEntry struct {
char rune
freq int
}
frequencyEntries := make([]freqEntry, 0, len(freqMap))
for char, freq := range freqMap {
frequencyEntries = append(frequencyEntries, freqEntry{char: char, freq: freq})
}
sort.Slice(frequencyEntries, func(i, j int) bool {
if frequencyEntries[i].freq == frequencyEntries[j].freq {
return frequencyEntries[i].char < frequencyEntries[j].char // Sort by ASCII value
}
return frequencyEntries[i].freq > frequencyEntries[j].freq // Sort by frequency
})
for _, entry := range frequencyEntries {
if entry.char == ' ' {
fmt.Printf("%d (space)\n", entry.freq) // Handle space character separately
} else {
fmt.Printf("%d (%c)\n", entry.freq, entry.char)
}
}
}
// Function to parse command-line arguments and flags
func parseArgs() (inputFile string, showCodes, showCount, showTree, showAll bool) {
// Define flags
codesFlag := flag.Bool("codes", false, "Display the Huffman codes table")
countFlag := flag.Bool("count", false, "Display the character occurrence count")
treeFlag := flag.Bool("tree", false, "Display the Huffman tree visualization")
allFlag := flag.Bool("all", false, "Display all flags") // New flag for all
// Parse flags
flag.Parse()
// Get all command-line arguments
args := flag.Args() // Get non-flag arguments
// Process arguments
for _, arg := range args {
if arg == "--all" {
*allFlag = true
showCodes = true
showCount = true
showTree = true
continue // Skip processing other args if --all is found
}
// Assume the first non-flag argument is the input file
if inputFile == "" {
inputFile = arg
} else {
fmt.Println("Warning: More than one input file specified. Using only the first one.")
}
}
// Check if the input file is empty and not using --all
if inputFile == "" && !*allFlag {
fmt.Println("Usage: go run main.go <input_file> [--codes | --count | --tree | --all]")
os.Exit(1)
}
return inputFile, *codesFlag || showCodes, *countFlag || showCount, *treeFlag || showTree, *allFlag
}
func main() {
// Parse command-line arguments and flags
inputFile, showCodes, showCount, showTree, showAll := parseArgs()
// Ensure inputFile is provided if not using --all
if inputFile == "" && !showAll {
fmt.Println("Usage: go run main.go <input_file> [--codes | --count | --tree | --all]")
os.Exit(1)
}
// Read the input file
input, err := readInputFile(inputFile)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("This is the input given by the user:\n%s\n", input)
// Build the Huffman tree
root := BuildHuffmanTree(input)
// Frequency map
freqMap := make(map[rune]int)
for _, char := range input {
freqMap[char]++
}
// Handle flags
if showCount {
PrintFrequencyArray(freqMap) // Call the function to print frequency array
}
// Generate Huffman codes
codes := make(map[rune]string)
GenerateHuffmanCodes(root, "", codes)
if showCodes {
fmt.Println("Huffman Codes:")
fmt.Println("--------------")
fmt.Printf("%-5s %s\n", "Char", "Code")
for char, code := range codes {
fmt.Printf("%-5s %s\n", string(char), code)
}
fmt.Println()
}
// Handle tree visualization
if showTree {
// Create a tree for drawing
t := tree.NewTree(tree.NodeString("Huffman Tree"))
DrawHuffmanTree(root, t, freqMap)
// Draw the tree
fmt.Println(t)
}
}