Data Structure--tree (bottom)--Huffman Tree and Huffman coding

Source: Internet
Author: User

What is Huffman tree?

Encode this question in binary form, equal length code. High frequency of non-debut coding, efficiency can be improved.

Convert percentile's exam results to a five-point score.

Decision Tree

The same thing, we use a decision tree, we can draw different efficiency

So ..... How do I search for trees more efficiently based on a node-less lookup frequency structure? This is the problem that Haffmanshu to solve.

The definition of Huffman tree:

  weighted path Length (WPL): set two fork tree has n leaf nodes, each leaf node with weight wk, from the root node to the length of the leaf node is LK, then the sum of the length of each leaf node with the weighted path is

Huffman Tree is also called the optimal binary tree. Huffman tree is to let WPL value least.

The structure of Huffman tree

Huffman Tree thinking: is the weight from small to large to sort, each time the minimum weight of two lessons two fork tree merge, forming a new two fork tree,

Then the two nodes with the smallest weights are then together, that is, 3 3 4 5 together. The first 3 is the number of weights of 1 and 2. Finally formed a

Huffman Tree algorithm:

Idea, how to find the smallest two elements? is actually a heap problem, build a minimal heap and find the smallest two elements inside.

huffmantree Huffman (minheap H) {inti;    Huffmantree T;     Buildminheap (H);  for(i =0; I < h->size; i++) {T= malloc (sizeof(structTreeNode));//Create a new node.T->left = Deletemin (H);//deletes a node as the left child node of the smallest heap. T->right = Deletemin (H);//deletes a node as the right child node of the smallest heap. T->weight = T->left->weight + t->right->Weight;        Insert (H, T); //inserts a new t into the minimum heap} T=deletemin (H); returnT;}

The greatest time complexity is nlog2n

Several features of Huffman Tree:

    1. There is no node with a degree of 1. Because Huffman tree originally found two of the smallest to merge.
    2. The Huffman tree with n leaf nodes has a total of 2n-1.
      1. There are n N0.
      2. N1 Huffman tree is a node with no degree of 1.
      3. N2 N0 = n2 + 1;
    3. The left and right subtree of any non-leaf node of Huffman tree is still Huffman tree after exchange.
    4. For the same set of weights {W1,W2,W3}, is there a different structure of the two Huffman trees? Yes, there may be.

Huffman code

How to avoid the ambiguity?

Prefix code: The encoding of people and characters is not a prefix of another character encoding

    • There can be no two-meaning encoding.

Use a binary tree for coding

  

How to construct such a non-debut code with Haffman

Data Structure--tree (bottom)--Huffman Tree and Huffman coding

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.