Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Find a Huffman tree encoding from an input string
ResourceFunction["HuffmanTree"]["string"] gives an optimal binary Huffman tree encoding for a string. |
Create the Huffman tree from an input string:
In[1]:= |
Out[1]= |
Create a case-insensitive Huffman tree:
In[2]:= |
Out[2]= |
The Huffman tree is the graph representation for the Huffman code words for the input string:
In[4]:= |
Out[4]= |
For instance the binary code word for "A" is 101 from above and it is retrieved in this way from the Huffman tree:
In[5]:= |
Out[5]= |
The input cannot be an empty string:
In[6]:= |
Out[6]= |
Single character or a string with all same character are not supported:
In[7]:= |
Out[7]= |
In[8]:= |
Out[8]= |
Two or more types of characters are valid:
In[9]:= |
Out[9]= |
The Huffman tree might not be unique for the same input if the data has the same sum of weights for some internal nodes and different conventions for left and right branches. Here, the two trees above shares the same depth information for each character:
In[10]:= |
Out[10]= |
Different strings can lead to the same tree:
In[11]:= |
Out[11]= |
Huffman tree is an efficient data structure to represent mood status of an emotional cat. Use emoji to represent emotions:
In[12]:= |
Out[13]= |
Create a long string to track the status for 50000 data points:
In[14]:= |
The cat's mood from first 15 observations:
In[15]:= |
Out[15]= |
Generate a tree based on the optimal binary Huffman encoding for all observations:
In[16]:= |
Out[16]= |
Wolfram Language 13.0 (December 2021) or above
This work is licensed under a Creative Commons Attribution 4.0 International License