Advanced programmers lessons - Architect of the road (10) - Huffman tree

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/m0_37609579/article/details/99691368

First, the origin of Hoffman (Huffman) is

1. The long-range communication problems in history

The initial distance communication for transmitting text information, the main telegraph.

  • Small A: How will the contents of a text as " BADCADFEED " pass it to others through the network?
  • Little B: These letters using a binary encoding, the encoding and transmission line ah.
  • Small D: Yes, telegraph not to "drop" and "A," Well? !
  • Small A: Then how coding it?
  • Small C: Have not you heard ASCII code?
  • Small A: Oh, then there is no ASCII code?

2. The original solution

For transmission of text "BADCADFEED" because only "ABCDEF" six characters recurring, so you can use the following way Code:

The recipient may be performed once every 3 bit, character encoding methods to restore text information.

3. But there is a serious performance problem with this scheme:

  • Such encoding requires 30 bits to represent 10 bit characters
  • So when the transmission information a 500-character requires 15,000 bit bit

During the war years, this encoding method is very inefficient and error-prone for sending and receiving of information.

4. How to improve the efficiency of the transceiver?

Second, Huffman coding

1. New coding

To improve efficiency and necessity of coding from start to avoid each character occupies the same bit position.

2. efficiency gains

  • Improved encoding requires only 25 bits can represent 10 bit characters
  • With the increase of transmission character, this advantage will be more obvious
  • A 17% improvement in efficiency! ! !

Third, the Huffman tree building steps

Subtlety: any one character prefix code is not encoded in another character!

Suppose there are n weights, the Huffman tree is constructed with n leaf nodes. n weight values ​​are set to w1, w2, ..., construction rules wn, the Huffman tree is:

  1. The w1, w2, ..., wn are n as forest trees (each tree node there is only one);
  2. Selecting the root node in the forest two minimum weight tree merge as a new tree in the left and right sub-tree, and the root of the tree is its new right and left weight right subtree root node the sum;
  3. Delete selected two trees from the forest, and the addition of new forest tree;
  4. Repeat (2), (3) step, until only a forest tree up the tree that is obtained by Huffman tree.

 

IV Summary

  1. Huffman tree is a special kind of binary tree
  2. Tree Huffman encoding and data compression is applied to the art
  3. Huffman tree is the foundation of modern compression algorithms

My micro-channel public number: architecture Scriptures (id: gentoo666), shared Java dry, high concurrency programming, popular technical tutorials, and distributed micro-services technology, architecture, design, block chain technology, artificial intelligence, big data, Java interview questions, as well as cutting-edge information and so popular. Updated daily Oh!

References:

  1. https://blog.csdn.net/lzjsqn/article/details/53141648

Guess you like

Origin www.cnblogs.com/anymk/p/11470518.html