Algorithm notes: Huffman tree, Huffman coding

1 character’s onboard representation

2 prefix encoding

  • Characters are only placed in leaf nodes
  • Character encodings can have different lengths
  • Since characters are only placed in leaf nodes, the encoding of each character cannot be the prefix of other character encodings.
  • Prefix encoding can be uniquely decoded

3 Huffman tree

  • The Huffman tree is a minimum-cost binary tree in which all characters are contained in leaf nodes.
  • To minimize the cost of the entire tree, obviously the leaves with large weights should be as close to the root of the tree as possible, and the leaves with small weights should be as far away from the roots as possible .

4 Huffman algorithm

This description may not be easy to understand. Let’s look at an example:

4.1 Examples

4.2 Generation of Huffman code

  • The encoding of each character is the path from the root node to the character
    • The left branch is 0 and the right branch is 1

Guess you like

Origin blog.csdn.net/qq_40206371/article/details/132702835