Dahua data Structure 16: Huffman tree (best binary tree)

Source: Internet
Author: User

1. Intro

Under the background of the current quality education, the academic grades of primary school have been changed into the vague words of excellent, good, pass and fail, and no longer communicate the specific scores.

You can use code to represent the following:

if (a <)  
    System.out.print ("fail");  
else if (a <)  
    System.out.print ("Pass");  
else if (a <)  
    System.out.print ("good");  
else
    System.out.print ("excellent");

A cursory look at this is no problem, but a good test paper most students focus on "good", and the above procedures need to judge two times to "good", input a large amount of time, in the algorithm efficiency is actually a problem. So the following improvements should be made (①->②):

2. Huffman tree Definition and principle

We first simplified the above diagram into a leaf node with a weighted two-tree (note: The number of connections between tree nodes is called the right, Weight).

The path length of the ① node: the number of connections from the root node to the path of the node.

Path length of the ② tree: The sum of the path lengths of each leaf node in the tree.

③ node Weighted path length: The product of node path length and node weight value.

The weighted path length of the ④ tree: WPL (weighted path length) is the sum of the weighted path lengths of all the leaf nodes in the tree.

The smaller the value of the WPL, the better the performance of the two-fork tree, the optimal binary tree is also called Huffman Tree.

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.