Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

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

License

NotificationsYou must be signed in to change notification settings

icza/huffman

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build StatusGo ReferenceGo Report Cardcodecov

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

Huffman Tree

Use theBuild() function to build a Huffman tree. Use thePrint() function to print Huffman codesof 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

Thehufio package implements a HuffmanReader andWriter. You may use these to transmit Huffman code of your data.

ThisReader andWriter internally manages a Symbol Table (the frequency of encountered symbols, updated dynamically).TheWriter computes and sends the Huffman code of the data, theReader receives the Huffman code and "reconstructs"the original data based on that.

The implementation uses asliding 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 onall 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))}

About

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

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

 

[8]ページ先頭

©2009-2025 Movatter.jp