Function Repository Resource:

HuffmanDecode

Source Notebook

Decode data specified by a Huffman encoding

Contributed by: Wolfram Staff (original content by Stephen Wolfram)

ResourceFunction["HuffmanDecode"][assoc]

returns the decoding of an association assoc representing a Huffman encoding as a string.

Details

Huffman encoding is featured in A New Kind of Science, page 1071.
The input association in ResourceFunction["HuffmanDecode"] must have the keys "Symbols", "CodeWords" and "Encoding", in the format returned by the resource function HuffmanEncode.

Examples

Basic Examples (2) 

Decode a Huffman encoding:

In[1]:=
ResourceFunction[
 "HuffmanDecode"][<|"Encoding" -> {1, 1, 0, 1, 0, 0, 1, 1}, "Symbols" -> {"a", "b", "c"}, "CodeWords" -> {{1, 1}, {0}, {1, 0}}|>]
Out[1]=

Decode a larger example:

In[2]:=
ResourceFunction[
 "HuffmanDecode"][<|"Symbols" -> {" ", "a", "b", "c", "d", "e", "i", "m", "n", "o", "p", "r", "s", "u", "y"}, "CodeWords" -> {{0, 0, 1}, {0, 0, 0, 1}, {1, 0, 1, 1, 0}, {1, 0, 0, 0}, {1, 1, 0, 1}, {1, 1, 1, 0}, {1, 0, 0, 1}, {1, 0, 1, 1, 1}, {0, 1, 0}, {1, 1, 1, 1}, {1, 1, 0, 0, 0}, {1, 0, 1, 0}, {0, 1, 1}, {1, 1, 0, 0, 1}, {0, 0, 0, 0}}, "Encoding" -> {1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0,
     1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1,
     0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0,
     0, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0,
     1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1,
     0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0}|>]
Out[2]=

Properties and Relations (1) 

HuffmanDecode is the inverse of the resource function HuffmanEncode:

In[3]:=
encoded = ResourceFunction["HuffmanEncode"]["the cat in the hat"]
Out[3]=
In[4]:=
ResourceFunction["HuffmanDecode"][encoded]
Out[4]=

Version History

  • 3.0.0 – 20 March 2023
  • 2.0.0 – 07 June 2019
  • 1.0.0 – 10 December 2018

Source Metadata

Related Resources

License Information