A Go-based implementation of the Huffman encoding and decoding algorithm.
-
Clone the repository:
git clone https://github.com/omarahr/huffman-encoder-decoder.git cd huffman-encoder-decoder
-
Install dependencies (requires Go 1.20+):
go mod tidy
-
Build the project:
go build -o huff
Run the compiled binary from the command line:
To encode a file:
./huff input.txt -o output.txt
-o
: Output file for the encoded data.
To decode a file:
./huff -d compressed.txt -o decompressed.txt
-d
: decompress mode.-o
: Output file for the decoded data.
Huffman coding is a lossless data compression algorithm that works by assigning variable-length codes to characters based on their frequencies. Frequently occurring characters are assigned shorter codes, while rarer characters receive longer codes.
- Frequency Analysis: Calculate the frequency of each character in the input data.
- Huffman Tree Construction:
- Use a priority queue to build a binary tree.
- Merge nodes with the smallest frequencies iteratively.
- Code Assignment: Traverse the tree to assign binary codes to each character.
- Encoding: Replace characters in the input with their corresponding binary codes.
- Decoding: Reconstruct the original data using the binary codes and the Huffman tree.
This project is licensed under the MIT License. Feel free to use, modify, and distribute it as per the license terms.
- Inspired the solution from Coding Challenges by John Crickett
- Explanation from OpenDSA
- The Huffman algorithm is a foundational concept in computer science and data compression.
- Inspiration for this project came from David A. Huffman’s original work.