The Huffman tree of data structure and the optimal binary tree

Source: Internet
Author: User

recently in the stir fry some knowledge about the tree, found a more interesting two-fork tree, Huffman Tree, corresponding to a discrete mathematics called the optimal Binary tree path structure, and the main role of Huffman, finally can be attributed to a code called Huffman encoding method, Using the Huffman encoding method, we can record a string of information with the shortest average length of code, and each information molecule is encoded uniquely and independently. Thus the information corresponding to the final synthesis code is unique and unambiguous.

The Huffman tree is created based on each information molecule has its weight, the greater the weight, the closer to the root, that is, the shorter the path,

Let's take an example of a Huffman tree: simply introduce the Huffman tree:

That is the necessary elements for constructing Huffman encoding, then, through the information molecule and corresponding code, we can write the code parsing results unique and unambiguous code string such as:

001011110100111 CDGFH

000111110010 CHEF

Any combination of Huffman encoding can only correspond to a string of information, there can be no ambiguity. Because in the Huffman tree, each information element is a leaf node ...

The Huffman tree results are:

The numbers in the circle represent weights, we rule to the left 0 to the right 1 , that is, to the Huffman code in the table above, through the tree, we are not difficult to calculate the right of the tree

W (T) is 291651 the minimum value of all the weights in the tree composed of these information elements, of course, the combination may not be the same, but the final right, will only be greater than or equal to him, that is, there is no equal to the weight and the minimum weight of the two non-isomorphic trees.

How to create this Huffman tree (optimal binary tree), this is the key to our today.

First we need to be clear about one thing, based on the number of nodes of the Huffman tree created by the information element, the answer is yes, if the length of the data structure we are creating at the outset is deterministic, then I think we have a great need to select an array.

The length of the array: m = number of information elements n * 2-1; that is, we need n cells to hold the information element node, n-1 the cell to hold the branch point (inner Point and root node).

Data structures of array cells (regardless of information metadata):

since the storage structure of the tree is implemented by an array, the array subscript can be saved directly in the Parent,rchild,lchild.

Everything's all right, that's it. Initialize the tree (take the example above):

(initial state, not yet done):

After the achievement action is completed:

Hey, where did the middle step go??? Don't worry!!!

Listen to me: 1: Look for the cell data in the array with the parent nonzero two array element c1, C2

2: Find their parent node father, Parent: array index increments the first array element in the sequence weight zero (provided that there is no right in the information element that weight is zero).

3: Point the parent node's lchild to the C1 and C2 ordinal (index) in the preceding array cell (that is, lchild = Indexmin (C1,C2). Index), and Rchild equals the other index, The parent of both C1 and C2 points to the found parental node, which is C1/c2.parent = Father.index. At the same time, the C1 and C2 of the rights and the weight (right) assigned to Father

4: Repeat the weight until the left and right of the array cell are data (assigned).

The code is as follows:

voidHuffmancoding (Huffmantree *ht,int*w,intN) {  /*W is an array of weights, and N is the number of information elements*/   intm,i,s1,s2;   Huffmantree p; Char*cd; if(n<=1)     return; M=2*n-1; *ht= (Huffmantree)malloc((m+1)*sizeof(Htnode));/*Unit No. No. 0 is not in use*/    for(p=*ht+1, i=1; i<=n;++i,++p,++w)//parent Lcahid rchild all initialized to 0   {     (*p). weight=*W; (*p). parent=0; (*p). lchild=0; (*p). rchild=0; }    for(; i<=m;++i,++p) (*p). parent=0;  for(i=n+1; i<=m;++i)/*Kenkheffman Tree*/    { /*in ht[1~i-1], select two nodes with a parent of 0 and a minimum of weight, with the sequence numbers s1 and S2 respectively .*/     Select(*ht,i-1,&s1,&S2); (*HT) [S1].parent= (*HT) [s2].parent=i; (*HT) [i].lchild=S1; (*HT) [i].rchild=S2; (*HT) [I].weight= (*HT) [S1].weight+ (*HT)   [S2].weight; }}

So, the final Huffman coding how to achieve, and how we implement anti-coding (from the code to get information), I will be the most in the future of the day to explore (no time), Ariase oh, feel good, remember to praise OH.

The Huffman tree of data structure and the optimal binary 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.