Documentation ¶
Overview ¶
Package huffman implements a Huffman entropy coding.
https://en.wikipedia.org/wiki/Huffman_coding
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
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Node ¶
type Node struct { Parent *Node // Optional parent node, for fast code read-out Left *Node // Optional left node Right *Node // Optional right node Count int // Relative frequency Value ValueType // Optional value, set if this is a leaf }
Node in the Huffman tree.
func Build ¶
Build builds a Huffman tree from the specified leaves. The content of the passed slice is modified, if this is unwanted, pass a copy. Guaranteed that the same input slice will result in the same Huffman tree.
func BuildSorted ¶
BuildSorted builds a Huffman tree from the specified leaves which must be sorted by Node.Count. The content of the passed slice is modified, if this is unwanted, pass a copy. Guaranteed that the same input slice will result in the same Huffman tree.