Search for a tool
Huffman Coding

Tool to compress / decompress with Huffman coding. Huffman coding is a data compression algorithm (lossless) which use a binary tree and a variable length code based on probability of appearance.


Huffman Coding -

Tag(s) : Compression

dCode and more

dCode is free and its tools are a valuable help in games, maths, geocaching, puzzles and problems to solve every day!
A suggestion ? a feedback ? a bug ? an idea ? Write to dCode!

Please, check our dCode Discord community for help requests!
NB: for encrypted messages, test our automatic cipher identifier!

Feedback and suggestions are welcome so that dCode offers the best 'Huffman Coding' tool for free! Thank you!

Huffman Coding

Huffman Decoder


(if this message do not disappear, try to refresh this page)
See also: LZW Compression

Huffman Coding Generator


Answers to Questions (FAQ)

What is Huffman coding? (Definition)

Huffman coding is a lossless data compression technique based on the statistics of the appearance of characters in the message.

Huffman assigns binary codes of variable length to the different characters according to their frequency of appearance in the message to be compressed (the most frequent benefiting from a short code in order to reduce the total size of the data).

How to encrypt using Huffman Coding cipher?

The Huffman code calculates the frequency of appearance of letters in the text, and sort the characters from the most frequent to the least frequent.

Example: The message DCODEMESSAGE contains 3 times the letter E, 2 times the letters D and S, and 1 times the letters A, C, G, M and O.

The Huffman algorithm will create a tree with leaves as the found letters and for value (or weight) their number of occurrences in the message. To create this tree, look for the 2 weakest nodes (smaller weight) and hook them to a new node whose weight is the sum of the 2 nodes. Repeat the process until having only one node, which will become the root (and that will have as weight the total number of letters of the message).

The binary code of each character is then obtained by browsing the tree from the root to the leaves and noting the path (0 or 1) to each node. The set of character-binary associations constitutes the dictionary.

Example: DCODEMOI generates a tree where D and the O, present most often, will have a short code. 'D = 00', 'O = 01', 'I = 111', 'M = 110', 'E = 101', 'C = 100', so 00100010010111001111 (20 bits) <dfn>huffman-tree</dfn>-dcodemoi

The dictionary/codebook is inseparable from the message, without it, the message cannot be decoded.

How to decrypt Huffman Code?

Decryption of the Huffman code requires knowledge of the matching tree or dictionary (characters <-> binary codes)

To decrypt, browse the tree from root to leaves (usually top to bottom) until you get an existing leaf (or a known value in the dictionary).

Example: Decode the message 00100010010111001111 with the dictionary described above. To research for 0 gives no correspondence, then continue with 00 which is code of the letter D, then 1 (does not exist), then 10 (does not exist), then 100 (code for C), etc.
The plain message is' DCODEMOI'

Why is Huffman used for compression?

By applying the algorithm of the Huffman coding, the most frequent characters (with greater occurrence) are coded with the smaller binary words, thus, the size used to code them is minimal, which increases the compression.

The compression ratio often exceeds 50%, especially if the message is long and made up mostly of the same characters.

However, Huffman does not work well with small data sizes, because the overhead of the tree representation can negate the compression gains.

How to recognize Huffman coded text?

The encoded message is in binary format (or in a hexadecimal representation) and must be accompanied by a correspondence tree/dictionary table for decryption.

The presence of variable length codes is an important feature.

The notions of a tree, tree structure or pruning (technique aiming to optimize the tree by dynamically removing branches/nodes) are clues.

How to decipher Huffman coding without the tree?

The tree/dictionary is needed to correctly assign codes to symbols. Without it, decryption becomes complicated or even impossible.

By making assumptions about the length of the message and the size of the binary words, it is possible to search for the probable list of words used by Huffman.

It should then be associated with the right letters, which represents a second difficulty for decryption and certainly requires automatic methods.

What are the variants of the Huffman cipher?

There are variants of Huffman when creating the tree / dictionary.

The dictionary can be static: each character / byte has a predefined code and is known or published in advance (so it does not need to be transmitted)

The dictionary can be semi-adaptive: the content is analyzed to calculate the frequency of each character and an optimized tree is used for encoding (it must then be transmitted for decoding). This is the version implemented on dCode

The dictionary can be adaptive: from a known tree (published before and therefore not transmitted) it is modified during compression and optimized as and when. The calculation time is much longer but often offers a better compression ratio.

Morse code uses variable length codes similar to Huffman coding.

When was Huffman coding invented?

It was published in 1952 by David Albert Huffman.

Source code

dCode retains ownership of the "Huffman Coding" source code. Except explicit open source licence (indicated Creative Commons / free), the "Huffman Coding" algorithm, the applet or snippet (converter, solver, encryption / decryption, encoding / decoding, ciphering / deciphering, breaker, translator), or the "Huffman Coding" functions (calculate, convert, solve, decrypt / encrypt, decipher / cipher, decode / encode, translate) written in any informatic language (Python, Java, PHP, C#, Javascript, Matlab, etc.) and all data download, script, or API access for "Huffman Coding" are not public, same for offline use on PC, mobile, tablet, iPhone or Android app!
Reminder : dCode is free to use.

Cite dCode

The copy-paste of the page "Huffman Coding" or any of its results, is allowed (even for commercial purposes) as long as you credit dCode!
Exporting results as a .csv or .txt file is free by clicking on the export icon
Cite as source (bibliography):
Huffman Coding on [online website], retrieved on 2024-06-24,

Need Help ?

Please, check our dCode Discord community for help requests!
NB: for encrypted messages, test our automatic cipher identifier!

Questions / Comments

Feedback and suggestions are welcome so that dCode offers the best 'Huffman Coding' tool for free! Thank you!
© 2024 dCode — El 'kit de herramientas' definitivo para resolver todos los juegos/acertijos/geocaching/CTF.