Huffman Tree (i) C language detailed

Source: Internet
Author: User

The introduction of Huffman tree

Huffman trees, Chinese name is Huffman tree or Hoffman tree, it is the best binary tree.

definition : given n weights as n leaf nodes, the construction of a binary tree, if the tree with the right path length to achieve the smallest, then this tree is called Huffman Tree. This definition involves a few unfamiliar concepts, the following is a Huffman tree, we look at the map solution.

(01) Path and path length

definition : The pathway between a child or grandchild node in a tree that can be reached down from a node, called a path. The number of branches in the path is called the route length. If the number of layers of the root node is 1, the path length from the root node to the L-level node is L-1.

Example : The path length of 100 and 80 is 1,50 and 30 the path length is 2,20 and 10 the path length is 3.

(02) The right of the node and the length of the weighted path

definition : If you assign a node in a tree to a value that has a meaning, the value is called the right of the node. The weighted path length of a node is the product of the path length from the root node to the node and the right of the node.

Example : The path length of node 20 is 3, its weighted path length = path length * right = 3 * 20 = 60.

(03) Length of the weighted path of the tree

definition : The weighted path length of a tree is defined as the sum of the weighted path lengths of all leaf nodes, recorded as WPL.

Example: In the example, the tree's wpl= 1* +2*+ 3*+ 3*10 = 100 + 160 + 60 + 30 = 350.

Compare Two trees below

The two trees above are trees with {10, 20, 50, 100} as leaf nodes.

Left tree wpl=2*10 + 2*20 + 2*50 + 2*100 = 360
The tree on the right wpl=350

The left tree WPL > The WPL of the tree on the right. You can also calculate the case in addition to the above two examples, but in fact the tree on the right is {10,20,50,100} corresponding Huffman tree. So far, should heap Huffman tree concept has a certain understanding, below see how to construct a Huffman tree.

The graphic analysis of Huffman tree

Assuming that there are n weights, the Huffman tree is constructed with n nodes. The n weights are set to W1, W2、...、 wN, and the construction rules of Huffman tree are:

1. W1, W2、..., WN as a forest with n trees (each tree has only one node);

2. In the forest, the two trees with the smallest weight value are merged, as the left and right subtrees of a new tree, and the root node weights of the new tree are the sum of their left and right Zishugen node weights;

3. Remove the two selected trees from the forest and add the new tree to the forest;

4. Repeat (02), (03) step, until only one tree is left in the forest, the tree is the Huffman tree is obtained.

Take {5,6,7,8,15} as an example, to construct a Huffman tree.

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.