/huffman

Huffman Lossless compression PoC

Primary LanguageGo

Huffman Tree

This is a poc for huffman lossless compression for text / byte arrays

Example

    // Ideally, this is better for larger strings eg. the one in the examples/example.go file
    import (
       encoder "github.com/obbap1/huffman/encoder"
       decoder "github.com/obbap1/huffman/decoder"
    )
    input := "hello"
    encoderHuffman := encoder.Init(input)
    compressedStr, huffmanNode, stats := encoderHuffman.Do() 

    // decode
    decoderHuffman := decoder.Init(compressedStr, huffmanNode)
    originalStr, stats := decoderHuffman.Do()
    fmt.Printf("%s, %s and %s", compressedStr, originalStr, input)

stats

The stats is a struct with keys
OriginalByteSize: The original number of bytes before compression
CompressedByteSize: The number of bytes after compression
PercentageSaved: The percentage saved after compression

Examples

There are examples in the examples folder.

Tests

You can run tests with go test ./.... You can also use act to run the tests from github actions