"Algorithm Summary" Haffman encoding

Source: Internet
Author: User

First, Huffman tree1. Huffman Tree also known as the optimal binary tree.

 the weight of the leaf node is a meaningful amount of value given to the leaf node.

A two-fork tree has n weighted leaf nodes, and the sum of the paths from the root node to each leaf node and the corresponding leaf node weights is called a two-fork tree with a weighted path length.

 Given a set of leaf nodes with certain weights, it is possible to construct a different two-fork tree, which is called Huffman tree with the smallest two-tree with the right path length.

2. Basic ideas:
    • initialization : by a given n weights $\left\{\omega_{1},\omega_{2},\cdots, \omega_{n}\right\}$ constructs n a two-fork tree with only one root node, resulting in a binary tree set $f=\ left\{t_{1},t_{2},\cdots,t_{n}\right\}$.
    • Select and merge: two binary trees with the least weight of the root node in the $f$ construct a new two-fork tree as the left and right sub-tree (under normal circumstances, the node with large weights is used as a subtree.) ), the weight of the root node of the new binary tree is the sum of the weights of its left and right sub-root nodes.
    • Delete and join : Remove the two binary trees as left and right subtrees in $f$ and add the newly established two-fork tree to the $f$.
    • Repeat these two steps, when there is only one binary tree left in the set $f$, this binary tree is Huffman tree.

In Huffman tree constructed by Huffman algorithm, the degree of non-leaf node is 2. The Huffman tree with n leaf nodes has a public 2n-1 node, in which there are n-1 non-leaf nodes. They were produced during the merger of N-1 Times.

Second, Huffman code

1. Huffman encoding is a variable word length encoding

If any encoding in a set of encodings is not a prefix for any other encoding, we call this set of codes prefix-coded. Huffman tree can be used to construct the shortest unequal length coding scheme.

2. Algorithmic flow

It is stipulated that the branch of Huffman coding tree represents 0, and the right branch represents 1, then the sequence of 0 and 1 of the path from the root node to each leaf node becomes the encoding of the corresponding character of the leaf node.

  

Decoding is a bitwise discriminant of the encoded string from left to right until a character is determined.

In Huffman coding tree, the meaning of the tree's weighted path length is the sum of the product of the code length and the number of occurrences of each character, so the encoding using Huffman tree structure is a kind of unequal length encoding which can make the length of the encoding of the string shortest.

"Algorithm Summary" Haffman encoding

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.