Construct the optimal binary tree-Huffman tree algorithm

Source: Internet
Author: User

I. Basic Concepts

1. The Huffman tree, also known as the optimal binary tree or the optimal search tree, is a binary tree with the shortest length of the weight path. In many applications, a meaningful real number is often assigned to a node in the tree, which is called the right of the node. The product between the path length from the root node to the node and the permission on the node is called the weighted path length (WPL) of the node ), the sum of the weighted path lengths of all leaf nodes in the tree is called the weighted path length of the tree, which is usually recorded:

 

2. path between two nodes: the node sequence from one node to another; Path Length: number of branches from the root node to the corresponding node path; tree path length: the sum of the path lengths from the root node to each node.

3. A binary tree with a depth of K and A knots of N. if and only when the numbers of each node are one-to-one with a full Binary Tree of the same depth, it is called a Complete Binary Tree. In a binary tree with the same number of nodes, the full binary tree is the binary tree with the shortest path length.

4. The smallest Binary Tree in WPL is the optimal binary tree (Huffman tree ).

5. Features of the Huffman tree

① When the weights on the leaves are the same, the full binary tree must be the optimal binary tree. Otherwise, the full binary tree is not necessarily the optimal binary tree.

② In the optimal binary tree, the leaves with larger weights are closer to the root.

③ The form of the optimal binary tree is not unique, but the WPL is the smallest.


For example, only (d) is the Heman tree. Here, the value of the circle represents.

Ii. algorithm ideas

(1) the weights are W1, W2... the N nodes of WN constitute n Binary Trees T1, T2 ,... TN and forest F = {T1, T2 ,... tn}, where each binary tree TI has only one root node with the weight of Wi;

(2) In F, select the tree with the smallest root node weight as the left and right subtree to construct a new binary tree, in addition, the root node weight of the new binary tree is the sum of the root node weights of the left and right Subtrees (the root node weights = the sum of left and right child weights, and the leaf node weights = WI)

(3) Delete the two binary trees from F and add the new binary trees to F;

(4) Repeat (2) and (3) until F contains only one binary tree, which is the Huffman tree.

Address: http://blog.163.com/zhoumhan_0351/blog/static/3995422720098275836215/

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.