Huffman Tree and Huffman coding

Source: Internet
Author: User
Tags dashed line

In the general data structure of the book, the tree behind that chapter, the author will generally introduce Huffman (HUFFMAN)

Tree and Huffman coding. Huffman coding is an application of Huffman tree. Huffman coding is widely used, such as

A Huffman code is applied to the JPEG. First introduce what is Huffman tree. Huffman tree, also known as the optimal binary tree,

is a two-fork tree with the shortest length of a weighted path. The so-called tree-weighted path length is all the leaf nodes in the tree

The weight is multiplied by the length of the path to the root node (the Joghen node is 0 layers, and the path length of the leaf node to the root node.

Is the number of layers of the leaf node). The tree's weighted path length is recorded as wpl= (W1*L1+W2*L2+W3*L3+...+WN*LN)

, N weights wi (i=1,2,... N) constitute a two-fork tree with n leaf nodes, and the path of the corresponding Leaf junction points

The length is Li (i=1,2,... N). It can be proved that the wpl of Huffman tree is the smallest.

Huffman Coding steps:

First, the given n weights {w1,w2,w3,..., Wi,..., Wn} constitute the initial set of n binary tree f= {t1,t2,t3,..., ti,..., Tn}, where each binary tree ti has only one weight of the root node of Wi, its left and right subtree are empty. (in order to facilitate the implementation of the algorithm on the computer, it is generally required that the weight of ti in the ascending order of WI.) )
Secondly, in F, the tree with the minimum weight of two root nodes is used as the left and right subtree of the new two-fork tree, and the weight of the root node of the new binary tree is the sum of the weights of the root node of its left and right sub-tree.
Remove the two trees from F and add the new two-fork tree in ascending order to the set F.
Repeat two and 32 steps until there is only one binary tree in the set F.

The simple understanding is that if I have a,b,c,d,e five characters, the frequency (that is, the weight) is 5,4,3,2,1, then we first take two minimum weights as the left and right sub-tree structure a new tree, that is, take 1, 2 constitute a new tree, its node is 1+2=3, as shown:

The dashed line is the newly generated node, and the second step is to put the newly generated node with the weight of 3 into the remaining set, so the set becomes {5,4,3,3}, and then the smallest two weights are constructed as a new tree according to the second step, as shown in figure:

Then set up Huffman tree, as shown below:

Each weight value replaces the corresponding character as the following image:

So the corresponding encoding for each character is: a->11,b->10,c->00,d->011,e->010

Huffman coding is an no-prefix code. Decoding is not confusing. It is mainly used in data compression, encryption and decryption and other occasions.

C Language Code implementation:

/*-------------------------------------------------------------------------
* Name:   Huffman coded source code.
* Date:   2011.04.16
* Author:jeffrey Hill+jezze (decoding section)
* Tested under WIN-TC
* Implementation process: First constructs Huffman tree through the Huffmantree () function, then in main function main ()
* Start           from bottom up (that is, start with a node with an array ordinal of zero) and
*           on the left side of the parent node, the code is 0, and if it is on the right, the code is 1. The final output of the generated encoding.
*------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#define MAXBIT      100
#define MAXVALUE  10000
#define MAXLEAF     30
#define Maxnode    maxleaf*2-1
{
    int Bit[maxbit];
    int start;
} Hcodetype;        /* Encode struct */
typedef struct
{
    int weight;
    int parent;
    int lchild;
    int rchild;
    int value;
} Hnodetype;        /* Node Structure */
/* Construct a huffman tree * *
void Huffmantree (Hnodetype huffnode[maxnode],  int n)
    /* I, J: cyclic variables, m1, M2: The weights of two minimum weights in the different process of constructing Huffman tree,
        X1, X2: The ordinal number of two minimum weights nodes in an array in the different process of constructing Huffman tree. */
    int I, J, M1, M2, X1, x2;
    /* Initialize the node in the Huffman Tree array huffnode[] */
    for (i=0; i<2*n-1; i++)
    {
        
        Huffnode[i].parent =-1;
        Huffnode[i].lchild =-1;
        Huffnode[i].rchild =-1;
        Huffnode[i].value=i; Actual values, which can be replaced by letters as appropriate  
    }/* End for */
    /* Enter the weights of n leaf nodes */
    for (i=0; i<n; i++)
    {
        printf ("Please input weight of leaf node%d: \ n", i);
        scanf ("%d", &huffnode[i].weight);
    }/* End for */
    /* Looping constructs Huffman tree */
    for (i=0; i<n-1; i++)
    {
        M1=m2=maxvalue;     /* Two nodes with two non-parent nodes and minimum node weights in M1, M2 */.
        x1=x2=0;
        /* Find the two nodes with the least weight and no parent nodes in all nodes and merge them into a binary tree */.
        for (j=0; j<n+i; j + +)
        {
            if (Huffnode[j].weight < M1 && Huffnode[j].parent==-1)
            {
                
                
                M1=huffnode[j].weight;
                X1=j;
            }
            else if (Huffnode[j].weight < m2 && huffnode[j].parent==-1)
            {
                M2=huffnode[j].weight;
                X2=j;
            }
        }/* End for */
            /* Set the parent node information for the two sub-nodes found X1, x2 */
        Huffnode[x1].parent  = n+i;
        Huffnode[x2].parent  = n+i;
        Huffnode[n+i].weight = Huffnode[x1].weight + huffnode[x2].weight;
        Huffnode[n+i].lchild = x1;
        Huffnode[n+i].rchild = x2;
        printf ("X1.weight and X2.weight in round%d:%d,%d\n", I+1, Huffnode[x1].weight, huffnode[x2].weight);  /* For testing */
        printf ("\ n");
    }/* End for */
  /* for  (i=0;i<n+2;i++)
    {
        printf ("parents:%d,lchild:%d,rchild:%d,value:%d,weight:%d\n", Huffnode[i].parent,huffnode[i].lchild,huffnode[i] . rchild,huffnode[i].value,huffnode[i].weight);
                  
}/* End Huffmantree */
void Decodeing (char string[],hnodetype buf[],int Num)
{
  int i,tmp=0,code[1024];
  int m=2*num-1;
  Char *nump;
  Char num[1024];
  For (I=0;i<strlen (string); i++)
  {
   if (string[i]== ' 0 ')
  num[i]=0;        
  Else
  Num[i]=1;                    
  
  i=0;
  nump=&num[0];

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.