Huffman.Tree

Source

Summary

build(list1, tree)
from_codes(codes)

Takes a list of codes and their corresponding bytes, rebuilding the tree ( without weights)

get_key(tree, key_bits)

Gets a key based on its code. Returns the key, and the unused part of the supplied code

insert(tree, code, key)
new(frequencies)

Creates a new huffman tree with the included frequencies Huffman.Tree.new([{"a", 1}, {"b", 1}, {"c", 4}, {"x", 9}])

reduce(tree, acc, fun)

Calls the supplied function for each leaf node

to_list(tree)
to_map(tree, by \\ :codes)

Returns a the tree as a map. By default, maps codes (the encoded bits) to keys (the byte it decodes into.) Optional second argument can give the inverse ( keys => codes)

to_string(tree)

The string representation of a Huffman.Tree. Basically a map from codes to their decoded value

Functions

build(list1, tree)
Source
from_codes(codes)

Takes a list of codes and their corresponding bytes, rebuilding the tree ( without weights)

Source
get_key(tree, key_bits)

Gets a key based on its code. Returns the key, and the unused part of the supplied code.

iex> Huffman.Tree.new([{"a", 1}, {"b", 1}, {"c", 4}, {"x", 9}])
...> |> Huffman.Tree.get_key(<<0::1, 0::1, 1::1>>)
{"b", <<>>}

It returns the unused portion of the code as well since in decoding you aren’t always sure what the next code is going to be until you find the key that matches. This allows you to simply pass in the decoded part and it will return the first decoded key.

Source
insert(tree, code, key)
Source
new(frequencies)

Creates a new huffman tree with the included frequencies Huffman.Tree.new([{"a", 1}, {"b", 1}, {"c", 4}, {"x", 9}])

Source
reduce(tree, acc, fun)

Calls the supplied function for each leaf node.

Source
to_list(tree)
Source
to_map(tree, by \\ :codes)

Returns a the tree as a map. By default, maps codes (the encoded bits) to keys (the byte it decodes into.) Optional second argument can give the inverse ( keys => codes)

Source
to_string(tree)

The string representation of a Huffman.Tree. Basically a map from codes to their decoded value.

Source