All Projects → icza → huffman

icza / huffman

Licence: Apache-2.0 license
Huffman coding implementation in Go (Huffman tree, Symbol table, Huffman Reader + Writer).

Programming Languages

go
31211 projects - #10 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to huffman

Huffman-Coding
A C++ compression program based on Huffman's lossless compression algorithm and decoder.
Stars: ✭ 81 (+224%)
Mutual labels:  huffman-coding, huffman-tree
huffman-javascript
Huffman encode/decode text
Stars: ✭ 20 (-20%)
Mutual labels:  huffman-coding, huffman-tree
c-compiler
A compiler that accepts any valid program written in C. It is made using Lex and Yacc. Returns a symbol table, parse tree, annotated syntax tree and intermediate code.
Stars: ✭ 37 (+48%)
Mutual labels:  symbol-table
gpuhd
Massively Parallel Huffman Decoding on GPUs
Stars: ✭ 30 (+20%)
Mutual labels:  huffman-coding
huffman-coding
A C++ compression and decompression program based on Huffman Coding.
Stars: ✭ 31 (+24%)
Mutual labels:  huffman-coding
rust-huffman-compress
A Rust library for Huffman compression given a propability distribution over arbitrary symbols
Stars: ✭ 18 (-28%)
Mutual labels:  huffman-coding
LittleBit
LittleBit is a pure Huffman coding compression algorithm with the option of random access reading while offering competitive compression ratios.
Stars: ✭ 13 (-48%)
Mutual labels:  huffman-tree
pascal-interpreter
A simple interpreter for a large subset of Pascal language written for educational purposes
Stars: ✭ 21 (-16%)
Mutual labels:  symbol-table
YCSymbolTracker
No description or website provided.
Stars: ✭ 41 (+64%)
Mutual labels:  symbol-table
compiler
Implementing a complete Compiler for a simple C-like language using the C-tools Flex and Bison
Stars: ✭ 106 (+324%)
Mutual labels:  symbol-table

huffman

Build Status GoDoc Go Report Card codecov

Huffman coding implementation in Go (Huffman tree, Symbol table, Huffman Reader + Writer).

Huffman Tree

Use the Build() function to build a Huffman tree. Use the Print() function to print Huffman codes of all leaves of a tree (for verification).

Example:

leaves := []*Node{
	{Value: ' ', Count: 20},
	{Value: 'a', Count: 40},
	{Value: 'm', Count: 10},
	{Value: 'l', Count: 7},
	{Value: 'f', Count: 8},
	{Value: 't', Count: 15},
}
root := Build(leaves)
Print(root)

Output:

'a': 0
'm': 100
'l': 1010
'f': 1011
't': 110
' ': 111

Huffman Reader and Writer

GoDoc

The hufio package implements a Huffman Reader and Writer. You may use these to transmit Huffman code of your data.

This Reader and Writer internally manages a Symbol Table (the frequency of encountered symbols, updated dynamically). The Writer computes and sends the Huffman code of the data, the Reader receives the Huffman code and "reconstructs" the original data based on that.

The implementation uses a sliding window which is used to manage the symbol table. The sliding window is optional, that is, if no window is used, the symbol table is calculated based on all previously encountered symbols.

Writer + Reader example:

buf := &bytes.Buffer{}
w := NewWriter(buf)
if _, err := w.Write([]byte("Testing Huffman Writer + Reader.")); err != nil {
	log.Panicln("Failed to write:", err)
}
if err := w.Close(); err != nil {
	log.Panicln("Failed to close:", err)
}

r := NewReader(bytes.NewReader(buf.Bytes()))
if data, err := ioutil.ReadAll(r); err != nil {
	log.Panicln("Failed to read:", err)
} else {
	log.Println("Read:", string(data))
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].