This project uses Huffman Coding, a lossless compression algorithm, to compress and decompress text files.
A Node
in the Huffman Tree contains:
- Character data
- Frequency of the character
- Huffman code
- Pointers to left and right child nodes
The Huffman
class includes:
- Constructor: Accepts input and output file names
compress()
: Compresses the input filedecompress()
: Decompresses the Huffman coded file
- createMinHeap(): Reads the input file, calculates character frequencies, and creates a Min Heap.
- createTree(): Builds the Huffman tree using the Min Heap.
- createCodes(): Traverses the Huffman tree to assign binary codes to characters.
- saveEncodedFile(): Saves the encoded file with Huffman codes and the encoded input file.
- getTree(): Reconstructs the Huffman tree from the encoded file.
- saveDecodedFile(): Reads the encoded input file, converts Huffman codes back to characters, and saves the original file.
This project is just an implementation of Huffman coding, it is not as efficient as the compression algorithm used currently to compress files. Example: inputFile.txt (2.2MB) is compressed to compressedFile.huf (1.1MB) file and decompressed back to ouputFile.txt (2.2MB).