Huffman tree and optimal binary tree continuation

Source: Internet
Author: User

OK, yesterday we made a few brief introductions to the basics of Huffman and the creation of Huffman trees, http://www.cnblogs.com/Frank-C/p/5017430.html

Keep Talking today:

How do we get Huffman code after the Huffman tree is created?

Figure 12.4_1 Huffman tree-shaped structure

Figure 12.4_2 Huffman storage structure (array)

First, taking the tree above as an example, we must understand several points:

1: From where to start to visit this tree: root node, index 2n-1 = 15

2: Access rule: 0 to the left 1 to the right

3: What kind of access is there in a sequential way?

Say

(1): Ask if there is a left child, go straight to the left, until the left child is zero, if the right child is also zero, proof has found a leaf node (information element). (Of course, encode records while accessing)

(2): The original road back to one layer (coding at the same time minus one), ask if there is a right child, there, into the right child, no, and then return to the previous layer

Repeat (1) and (2) steps until the final fallback to a more node, in the parent that returns to the root node, that is, the iterator (index) is zero at this time

Of course, I just said a little bit about the steps, the specific code implementation, still have to be fine and thin, look down!!!!!! There are wood there

*hc= (Huffmancode)malloc((n+1)*sizeof(Char*)); /*Assigning N-character-encoded head-pointer vectors ([0] without)*/CD=(Char*)malloc(nsizeof(Char));/*allocate a workspace that is encoded (temp) space*/C=m;//pointing directly to the top non-leaf node .cdlen=0;  for(i=1; i<=m;++i)/*Use as Node status flag when traversing Heffman to set all nodes to zero at this point, the tree has been built, so the weight is not much use.*/     (*HT) [i].weight=0;  while(c) {if((*HT) [c].weight==0)//responsible for left{/*left*/       (*HT) [c].weight=1;/* ************* */       if((*HT) [c].lchild!=0)/*if the right-to-zero left child node is not the first element (subscript 0)*/{C=(*HT)         [C].lchild; Cd[cdlen++]='0'; }       /*See if the left child has a right child, if the right child is 0 (that is, there is no right child), then a leaf node*/       Else if((*HT) [c].rchild==0)          { /*code for registering the character of a leaf node*/         (*HC) [C]= (Char*)malloc((cdlen+1)*sizeof(Char)); Cd[cdlen]=' /'; strcpy ((*HC) [C],CD);/*copy Encoding (string)*/       }     }             /*Conditions Change*/     Else if((*HT) [c].weight==1)/*responsible for going back to the right will not be judged whether the left child is the leaf node or the middle inside point*/     { /*Right*/       (*HT) [c].weight=2;/////************                  if((*HT) [c].rchild!=0)//decide if you will be cleared 0{C=(*HT)         [C].rchild; Cd[cdlen++]='1'; }     }     Else  //responsible for fallback{/*ht[c].weight==2, return*/       (*HT) [c].weight=0;/////**************C= (*HT)       [C].parent; --cdlen;/*Retreat to parent node, encoding length minus 1 iterations: Take advantage of the road that you have traveled before*/     }   }    Free(CD);

The code above passes through a weight (which is zeroed out before the while statement is executed), to determine the current node (except for the case of the Leaf node): (only as a process description, not as a condition judge)

is not accessed or has been accessed and is useless: 0

accessing left tree and "right tree not yet accessible" (of course this is nonsense): 1

Accessing the right tree and "left tree is already visited" (this is nonsense, of course): 2

When to fallback, the left node does not have or has been visited, and the right node does not have or has access to play, how to guarantee these conditions before. Simple, if Else branch statement of the role of the expression, the back of the judgment statement in the last, the front to determine the left child wood has ha, there is a right child wood, no, prove to find a leaf node (information element), record information, at this time, this leaf node weight has been set to 1, Then the top is set to 2 and there is no right node, which goes to the last Else branch, which is zero and fallback.

When the branch node is 1 (at which point the left node has been accessed), it is first set to 2, asking if there is a right node, there is access, that is, note that the current node if it is the parent node of the right child, the word at this time the parent node weight is placed in order to two, when this node is a leaf node and Fallback to parent node, parent node has 2, continue fallback.

Summary: The Code branch statement is divided into three main branch blocks weight 0 (determine if the node has not been accessed) weight 1 (to determine if the left tree has been accessed) weight 2, the node weight need to clear 0 and fallback

For leaf nodes, the first branch is processed, and the second and three branch blocks are assisted to judge and fallback.

How to go through the code to decompile into information or information elements, I think it is simple, at least more than the creation of the code is easy, directly according to the tree with certain rules to find (for example, 0: Left child, 1: Right child), given the independence of each information element and the information element of the non-inclusion, both suitable for anti-coding

Ok,that ' s all!!!

Huffman tree and optimal binary tree continuation

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.