This project implements Huffman Encoding and Decoding in Java, complete with a graphical user interface (GUI) to visualize the Huffman Tree, Huffman Codes, character frequencies, encoded text, and decoded text. The application also supports file upload and saving encoded and decoded text.
- File Upload: Upload a text file to be encoded.
- Character Frequency Calculation: Analyze the uploaded text to calculate the frequency of each character.
- Huffman Tree Construction: Build a Huffman Tree based on the character frequencies.
- Huffman Code Generation: Generate Huffman codes for each character from the Huffman Tree.
- Encoding: Encode the text using the generated Huffman codes.
- Decoding: Decode the encoded text back to its original form using the Huffman Tree.
- File Saving: Save the encoded text, Huffman codes, and decoded text to files.
- Graphical Display: Display the character frequencies, Huffman Tree, Huffman codes, encoded text, and decoded text in a GUI.
- FileUploader: Provides functionality to select and upload a text file.
- TextAnalyzer: Analyzes the uploaded text to calculate character frequencies.
- HuffmanNode: Represents a node in the Huffman Tree.
- HuffmanTreeBuilder: Builds the Huffman Tree from the character frequencies.
- HuffmanCode: Generates Huffman codes from the Huffman Tree and encodes the text.
- HuffmanDecoder: Decodes the encoded text back to the original text using the Huffman Tree.
- FileSaver: Saves the encoded text, Huffman codes, and decoded text to files.
- GUI Components: Various panels (HuffmanTreePanel, HuffmanTablePanel, EncodedTextPanel, FrequencyPanel, DecodedTextPanel) to display the data.
Java Development Kit (JDK) 8 or higher
Clone the repository:
git clone https://github.com/yourusername/huffman-encoding.git
cd huffman-encoding
Open the project in your IDE and build it.
- Run the
HuffmanEncodingApplication
main class. - Upload a text file when prompted.
- The application will display the character frequencies, Huffman Tree, Huffman codes, encoded text, and decoded text in separate tabs.
- Save the encoded text, Huffman codes, and decoded text using the file save options.
Javac HuffmanEncodingApplication.java
Java HuffmanEncodingApplication
uploadFile()
: Opens a file chooser to select a text file.readFile(File file)
: Reads the contents of a file and returns it as a string.
analyzeText(String text)
: Analyzes the text to calculate character frequencies.
Represents a node in the Huffman Tree.
buildTree(Map<Character, Integer> frequencyMap)
: Builds the Huffman Tree from the character frequencies.
generateCodes(HuffmanNode root)
: Generates Huffman codes from the Huffman Tree.encodeText(String text, Map<Character, String> huffmanCodes)
: Encodes the text using the Huffman codes.
decodeText(String encodedText, HuffmanNode root)
: Decodes the encoded text back to the original text using the Huffman Tree.
saveEncodedText(String encodedText)
: Saves the encoded text to a file.saveHuffmanCodes(Map<Character, String> huffmanCodes)
: Saves the Huffman codes to a file.saveDecodedText(String decodedText)
: Saves the decoded text to a file.
FrequencyPanel
: Displays character frequencies.HuffmanTreePanel
: Displays the Huffman Tree.HuffmanTablePanel
: Displays Huffman codes.EncodedTextPanel
: Displays the encoded text.DecodedTextPanel
: Displays the decoded text.
Contributions are welcome! If you have suggestions for improvements, please submit a pull request or open an issue.
Made with ❤️ by FARZINzx