This is the project which I did in my 4th semester as part of the Data Structures and Algorithms subject. In this project, I implemented the Huffman Coding Algorithm for file compression and decompression. The algorithm is used to compress text files by encoding characters into variable-length codes based on their frequencies in the file. The project includes functionalities for both compressing and decompressing text files.
- File Compression: Compresses a text file using Huffman coding.
- File Decompression: Decompresses the
.binfile and restores the original text. - Efficient Data Encoding: Uses shorter codes for frequent characters and longer codes for less frequent ones.
- Padding: Adds necessary padding to ensure that the final binary string is a multiple of 8 bits for storage.
-
Compression:
- The program reads the input text file and calculates the frequency of each character.
- A Huffman Tree is built using the character frequencies, where the most frequent characters are assigned shorter codes.
- The text is encoded into a binary string using these codes.
- Padding is added to ensure that the binary string is a multiple of 8 bits.
- The binary string is then converted into bytes and saved in a
.binfile.
-
Decompression:
- The program reads the compressed
.binfile, extracts the binary data, and removes the padding. - The binary data is decoded using the Huffman Tree to reconstruct the original text.
- The program reads the compressed
- C++ Compiler (C++11 or later)
- Standard C++ libraries (
<bits/stdc++.h>,<fstream>,<bitset>)
-
Compile the Program:
- Use a C++ compiler to compile the project:
g++ -o huffman_compression_decompression huffman_compression_decompression.cpp
- Use a C++ compiler to compile the project:
-
Run the Program:
- Create an object of the
HuffmanCodeclass by providing the path to the input text file:HuffmanCode huff("./input.txt"); // path to the input file huff.compressFile(); // Compress the file huff.decompressFile(); // Decompress the file
- Create an object of the
-
Input and Output:
- The program generates a
.bincompressed file and a.txtdecompressed file.
- The program generates a