A summary of Huffman coding algorithm thought

Source: Internet
Author: User

1 Build Huffman tree.

(using the data structure of the queue, continuously get the smallest two, exit the queue and make it the left and right subtree of the new node.) Then insert the new node into the queue. So loop ... )

Counts the occurrences of each character, based on the string entered by the user. Set weights. Build the queue. The node in the queue is a two-tree node (with a left pointer, and a right pointer).

Build a new node, and the left and right pointers point to the last two of the queue (the two exit queues). Then insert the queue. This cycle, you can build Huffman tree.

2 encodes for each character.

The left side of the node is 0 and the right is 1.

Code (*node) {      //leaf nodeif (node->left==NULL&& node->right==NULL{Insert the code into the table encoded list} if (node->left!=NULL) {nodeEncoding of the->left = node encoding +'0'; Fills the left subtree into the table encoded list} if (node->right!=NULL) {nodeEncoding of the->right = node encoding +'1'; //Recursive right sub-treeCode (Node-Right ); } }

3 with a coded list, you can convert a user-entered string into an encoding.

or convert the encoding into a string.

function is simple, do not repeat.

A summary of Huffman coding algorithm thought

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.