Skip to content

SwekeR-463/huffman-text-comp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Huffman Al(Go)

This is Go implementation of the Huffman coding algorithm. It reads a text file, compresses the text using Huffman encoding, and then decompresses it back to the original text.

Algorithm

Screenshot 2024-12-10 182243

Features

  • Build a Huffman tree based on character frequencies.
  • Encode text into a binary string using the Huffman codes.
  • Decode the binary string back to the original text.
  • Save the encoded data to a file.

How It Works

  1. Reads the input text file and calculates character frequencies.
  2. Constructs a Huffman tree using a priority queue (min-heap).
  3. Generates unique binary codes for each character based on the tree structure.
  4. Encodes the text using the generated codes.
  5. Decodes the encoded text back to its original form.
  6. Verifies that the decoding matches the original text.

File Structure

  • input.txt: Input text file to be encoded.
  • encoded.txt: Output file containing the encoded text.

How to Run

  1. Clone the repository:

    git clone https://github.com/SwekeR-463/huffman-text-comp.git
    cd huffman-go
  2. Place your input file as input.txt in the project directory.

  3. Build and run the program:

    go run huffman.go
  4. View the encoded text in the console and in the saved encoded.txt

Example

Given an input.txt with the context

hello world

The program outputs:

Encoded Text: 1010101110001110... (binary string)
Decoded Text: hello world
Encoded data saved to encoded.txt
Decoding successful!

Requirements

    Go 1.20 or higher

To-Do

  • To implement full text file compression

Releases

No releases published

Packages

No packages published

Languages