This repository contains Java code for Huffman encoding and decoding, along with supporting classes for working with Huffman codes. Huffman coding is a widely used algorithm for data compression that assigns variable-length codes to input characters based on their frequencies.
The project consists of the following Java classes:
Node
: Represents nodes used in the Huffman tree.Code
: Represents a character and its corresponding Huffman code.EncoderDecoder
: Provides encoding and decoding functionality.Haffman
: Implements the Huffman encoding and decoding logic.
Huffman encoding is the process of converting a given input text into a compressed form using Huffman codes. The Haffman
class provides methods to perform Huffman encoding. It constructs a Huffman tree, generates Huffman codes, and encodes the input text.
Huffman decoding is the process of converting a compressed text (encoded using Huffman codes) back to its original form. The Haffman
class also includes methods for Huffman decoding. It reads encoded data, reconstructs the Huffman tree, and decodes the text.
To run the Huffman encoding and decoding program, you need the following prerequisites:
- Java Development Kit (JDK) installed on your computer.
- A Java IDE or a text editor to edit and run the Java source code.
-
Clone the repository to your local machine using the following command:
git clone https://github.com/your-username/huffman-encoding.git
-
Open the project in your preferred Java IDE or text editor.
To encode a text file, follow these steps:
-
Use the
Haffman.fromTextFile(filePath)
method to create aHaffman
object from the text file. -
Call the
encode()
method to generate the Huffman-encoded string. -
Use the
writeEncodedToFile(outputFilePath)
method to save the encoded data to a file.
Example:
Haffman huffman = Haffman.fromTextFile("input.txt");
String encodedText = huffman.encode();
huffman.writeEncodedToFile("encoded_output.txt");
To decode an encoded file, follow these steps:
-
Use the
Haffman.fromEncodedFile(filePath)
method to create aHaffman
object from the encoded file. -
Call the
decode()
method to recover the original text. -
Use the
writeDecodedToFile(outputFilePath)
method to save the decoded text to a file.
Example:
Haffman huffman = Haffman.fromEncodedFile("encoded_input.txt");
String decodedText = huffman.decode();
huffman.writeDecodedToFile("decoded_output.txt");
Feel free to submit issues, create pull requests, or suggest improvements.
For any questions or support, please contact mahdimoghassemi@gmail.com.