Huffman Tree and Huffman coding

Source: Internet
Author: User

Huffman Tree Introduction

Huffman Tree (Huffman Tree), also known as the optimal binary tree, is a kind of tree with the shortest length of weighted path. Suppose there are n weights {w1,w2,..., wn}, if you construct a two-fork tree with N-leaf nodes, and the weights of the N-leaf nodes are {w1,w2,..., wn}, the two-fork tree with the least length of the right path constructed is called Huffman Tree.

This adds the concept of the weighted path length of the tree. The weighted path length of a tree refers to the sum of the product of the path length of all leaf nodes to the root node in the tree and the weight value of the leaf node. If there are n leaf nodes in a binary tree, the weight of the first leaf node is expressed by WI, and Li represents the path length of the first leaf node to the root node, then the weighted path length of the two-fork tree WPL=W1*L1 + w2*l2 + ... Wn*ln.

According to the number of nodes and the weight of the difference, Huffman tree shape is also different, Huffman tree has the following characteristics:

For the same set of weights, can get the Huffman tree is not necessarily unique.

Huffman Tree of the left and right subtree can be interchangeable, because this does not affect the length of the tree's weighted path.

Nodes with weights are leaf nodes, and nodes without weights are the root nodes of a Shang binary tree.

The greater the weight of the node closer to the root node Huffman tree, the smaller the weight of the node farther away from Huffman tree root node.

Huffman tree only leaves node and degree of 2 nodes, there is no degree of 1 nodes.

A tree has n leaf node of Huffman tree A total of 2n-1 nodes.

The construction of Huffman tree

Huffman tree Construction steps are as follows:

1, the given n weight values as n only the root node (no children) two fork tree, composed of a set of HT, each tree's weight is the weight of the node.

2, from the set of HT selected 2 of the smallest weight two fork tree, a new two-tree, the weight of these 2 binary tree weight of the sum of the value.

3. Delete the 2 binary tree selected in step 2 from the Set HT and add the new two fork tree in step 2 to the set HT.

4, repeat steps 2 and 3, until the collection HT contains only one tree, this tree is Huffman tree.

If given the following 5 weight values:

In accordance with the above steps, you can construct the following surface of the Huffman tree, of course, it may also be constructed as shown in the following picture of Huffman tree, this is not the only.

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.