Skip to content

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.

Notifications You must be signed in to change notification settings

FARZINzx/HuffmanCoding

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 

Repository files navigation

Huffman Encoding and Decoding in Java


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.

Features

  1. File Upload: Upload a text file to be encoded.
  2. Character Frequency Calculation: Analyze the uploaded text to calculate the frequency of each character.
  3. Huffman Tree Construction: Build a Huffman Tree based on the character frequencies.
  4. Huffman Code Generation: Generate Huffman codes for each character from the Huffman Tree.
  5. Encoding: Encode the text using the generated Huffman codes.
  6. Decoding: Decode the encoded text back to its original form using the Huffman Tree.
  7. File Saving: Save the encoded text, Huffman codes, and decoded text to files.
  8. Graphical Display: Display the character frequencies, Huffman Tree, Huffman codes, encoded text, and decoded text in a GUI.

How It Works

  • 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.

Getting Started

Prerequisites

Java Development Kit (JDK) 8 or higher

Installing

Clone the repository:

git clone https://github.com/yourusername/huffman-encoding.git
cd huffman-encoding

Open the project in your IDE and build it.

Usage

  1. Run the HuffmanEncodingApplication main class.
  2. Javac HuffmanEncodingApplication.java
    Java HuffmanEncodingApplication  
  3. Upload a text file when prompted.
  4. The application will display the character frequencies, Huffman Tree, Huffman codes, encoded text, and decoded text in separate tabs.
  5. Save the encoded text, Huffman codes, and decoded text using the file save options.

Classes and Methods

FileUploader

  • 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.

TextAnalyzer

  • analyzeText(String text): Analyzes the text to calculate character frequencies.

HuffmanNode

Represents a node in the Huffman Tree.

HuffmanTreeBuilder

  • buildTree(Map<Character, Integer> frequencyMap): Builds the Huffman Tree from the character frequencies.

HuffmanCode

  • generateCodes(HuffmanNode root): Generates Huffman codes from the Huffman Tree.
  • encodeText(String text, Map<Character, String> huffmanCodes): Encodes the text using the Huffman codes.

HuffmanDecoder

  • decodeText(String encodedText, HuffmanNode root): Decodes the encoded text back to the original text using the Huffman Tree.

FileSaver

  • 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.

GUI Components

  • FrequencyPanel: Displays character frequencies.
  • HuffmanTreePanel: Displays the Huffman Tree.
  • HuffmanTablePanel: Displays Huffman codes.
  • EncodedTextPanel: Displays the encoded text.
  • DecodedTextPanel: Displays the decoded text.

Contributing

Contributions are welcome! If you have suggestions for improvements, please submit a pull request or open an issue.

Made with ❤️ by FARZINzx

About

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.

Topics

Resources

Stars

Watchers

Forks

Languages