Wolfram Research

Function Repository Resource:

HuffmanDecode (2.0.0) current version: 3.0.0 »

Source Notebook

Decode data specified by a Huffman encoding

Contributed by: Stephen Wolfram

ResourceFunction["HuffmanDecode"][assoc]

decodes an association representing a Huffman encoding.

Details and Options

Used in A New Kind of Science, page 1071.

Examples

Basic Examples (2) 

Decode a Huffman encoding:

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

Decode a larger example:

In[2]:=
ResourceFunction["HuffmanDecode", ResourceVersion->"2.0.0"][<|"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) 

For lists of characters, HuffmanDecode is the inverse of HuffmanEncode:

In[3]:=
encoded = ResourceFunction["HuffmanEncode"][Characters["the cat in the hat"]]
Out[3]=
In[4]:=
StringJoin[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