The Huffman Encoder is a lossless data compression algorithm used to reduce the size of data files. This algorithm creates an optimal prefix-free binary tree to represent characters from the input file. More frequent characters are assigned shorter codes, and less frequent characters are assigned longer codes, leading to efficient compression.
The Huffman Encoding algorithm compresses text data by creating a binary tree based on the frequency of characters. Each character is represented by a variable-length binary string called a Huffman code. The most frequent characters are placed closer to the root of the tree, and the less frequent ones are further away.
-
Build a Frequency Table:
- First, the algorithm scans the input data to calculate the frequency of each character.
-
Build the Huffman Tree:
- A binary tree is built where each leaf node contains a character and its frequency. The tree is constructed by repeatedly combining the two nodes with the lowest frequencies. Internal nodes represent merged nodes and are assigned a frequency equal to the sum of their children's frequencies.
-
Generate Huffman Codes:
- After constructing the Huffman tree, a binary code is assigned to each character. The path from the root to the character's leaf node gives the character’s code. Left branches are assigned
0, and right branches are assigned1.
- After constructing the Huffman tree, a binary code is assigned to each character. The path from the root to the character's leaf node gives the character’s code. Left branches are assigned
-
Compression:
- The original file is compressed by replacing each character with its corresponding Huffman code. The compressed data is then saved in a binary format, while the codes are stored in a text file.
-
Decompression:
- To decompress, the algorithm uses the Huffman codes and the binary data to reconstruct the original text.
Make sure you have Node.js and npm installed on your system.
- Make a folder
mkdir <foldername> #to store the huffman encoder
- Clone the Repository:
git clone https://github.com/PrathamSharda/hoffmanEncoder.git
- install dependencies:
npm install
- uderstanding interface:
node compress.cjs --help
- creating a testfolder:
mkdir <test Foldername> #create a test folder and add text files in them cd <test folderName> touch <test .txt filename>
- compression of files:
node compress.cjs -c <foldername> #replace folder name with test folder name
- decompression of files:
node compress.cjs -d <foldername> #replace folder name with test folder name