This is Go implementation of the Huffman coding algorithm. It reads a text file, compresses the text using Huffman encoding, and then decompresses it back to the original text.
- Build a Huffman tree based on character frequencies.
- Encode text into a binary string using the Huffman codes.
- Decode the binary string back to the original text.
- Save the encoded data to a file.
- Reads the input text file and calculates character frequencies.
- Constructs a Huffman tree using a priority queue (min-heap).
- Generates unique binary codes for each character based on the tree structure.
- Encodes the text using the generated codes.
- Decodes the encoded text back to its original form.
- Verifies that the decoding matches the original text.
input.txt
: Input text file to be encoded.encoded.txt
: Output file containing the encoded text.
-
Clone the repository:
git clone https://github.com/SwekeR-463/huffman-text-comp.git cd huffman-go
-
Place your input file as
input.txt
in the project directory. -
Build and run the program:
go run huffman.go
-
View the encoded text in the console and in the saved
encoded.txt
Given an input.txt
with the context
hello world
The program outputs:
Encoded Text: 1010101110001110... (binary string)
Decoded Text: hello world
Encoded data saved to encoded.txt
Decoding successful!
- Go 1.20 or higher
- To implement full text file compression