Step-by-step write algorithm (Huffman tree)

Source: Internet
Author: User

Text: Step by step write algorithm (Huffman tree)

"Disclaimer: Copyright, welcome reprint, please do not use for commercial purposes. Contact mailbox: feixiaoxing @163.com "


In the process of data transmission, we always want to use as little bandwidth as possible to transmit more data, Huffman is one of the less bandwidth transmission method. The basic idea of Huffman is not complicated, that is, for the data with high frequency is expressed in short byte, the data with low frequency is expressed in long byte.

For example, there are now 4 data that need to be transferred, a, B, C, D, so generally speaking, if the probability of the occurrence of four data is not considered at this time, then we can completely allocate, the average length is 2,

/**  A-00         b-01*  C-10         d-11*/
However, now that the conditions have changed, the frequency of four data is not the same, respectively, 0.1/0.2/0.3/0.4. So how to allocate the length at this time, actually also simple. All we have to do is put all the data in the same frequency from low to high, merge the first two bits into a new node each time, and then put the new node in the queue and reorder it. The new node's left node is set to 1 by default, and the right node is set to 0 by default. Then repeat the process until all the nodes are merged into one node. If applied to the actual example, the process of merging should be like this,

The first step is to merge A and B first, because A and B are the least likely

/**  *           total_1 (0.3)           C (0.3)   D (0.4) *          /         *        A (0.1)      B (0.2) */
Step two, then merge Total_1 and C,

/**                 total_2 (0.6) *               /          \     *           total_1 (0.3)    C (0.3)    D (0.4) *          /         *        A (0.1)      B (0.2) */
The final step, merging Total_2 and D,

/**            Final (1.0) *          /       \          *       D (0.4)    total_2 (0.6)    *                /          \     *           Total _1 (0.3)    C (0.3)   *          /         *        A (0.1)      B (0.2) * *
So according to the spanning tree above, the number of data should be so arranged,

/**   A-011       b-010*   C-00        d-1*/
It looks like the length of A and B has increased, but the length of D has decreased. So does the average length of the entire data decrease? We can calculate it. 3 * 0.1 + 3 * 0.2 + 2 * 0.3 + 0.4 = 1.9 < 2. We found that the average length of the adjusted data was less than the original (2-1.9)/2 * 100% = 10, which is a huge discovery.

To complete the creation of the Huffman tree, we also need to define a data structure:

typedef struct _HUFFMAN_NODE{CHAR str;double frequence;int symbol;struct _huffman_node* left;struct _HUFFMAN_NODE* Right;struct _huffman_node* Parent;} Huffman_node;
Where STR record characters, frequency record characters appear frequency, symbol records the assigned data, the left subtree is 1, the right subtree is 0,left for the left dial hand tree, and right is the parent node. Next, we start by creating the Huffman node.

huffman_node* Create_new_node (char str, double Frq) {huffman_node* Pnode = (huffman_node*) malloc (sizeof (Huffman_node)) ; assert (NULL! = pnode);p node->str = str;pnode->frequence = Frq;pnode->symbol = -1;pnode->left = NULL;pNode-& Gt;right = Null;pnode->parent = Null;return pnode;}


"Not finished, to be Continued"



Step-by-step write algorithm (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.