Huffman Tree 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,

The dashed line is the newly generated node, and the second step then puts the newly generated node with the weight of 3 into the remaining set, so the set becomes {5,4,3,3}, and then, based on the second step, takes the minimum two weights to form a new tree,

Then establish Huffman tree, such as:

where each weight value replaces the corresponding character is:

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 * /
struct
{
    int weight;
    int parent;
    int lchild;
    int rchild;
    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].weight = 0;
        Huffnode[i].parent =-1;
        Huffnode[i].lchild =-1;
        Huffnode[i].rchild =-1;
        Huffnode[i].  //actual value, can be replaced by the letter  according to the situation
    /* 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;
            }
            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 (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];
  
while (nump< (&num[strlen (string)))
{tmp=m-1;
  while ((Buf[tmp].lchild!=-1) && (buf[tmp].rchild!=-1))
  {
  
   if (*nump==0)
   {
     Tmp=buf[tmp].lchild;          
   
   else Tmp=buf[tmp].rchild;
   nump++;
        
  
  
  printf ("%d", buf[tmp].  value);                                   
}
  
}
int main (void)
{
    
    Hnodetype Huffnode[maxnode];            / * Define a node structure array * /
    Hcodetype Huffcode[maxleaf],  cd;       / * Define an array of coded structures, and define a temporary variable to hold the information when solving the encoding * /
    int I, J, C, p, N;
    Char pp[100];
    printf ("please input n:\n");
    scanf ("%d", &n);
    Huffmantree (Huffnode, N);
   
    
    for (i=0; i < n; i++)
    {
        Cd.start = n-1;
        c = i;
        p = huffnode[c].parent;
        while (P! =-1)/   * Parent node exists * /
        {
            if (Huffnode[p].lchild = = c)
                Cd.bit[cd.start] = 0;
            Else
                Cd.bit[cd.start] = 1;
            cd.start--;        / * Low one for coding * /
            C=p;                    
            p=huffnode[c].parent;    / * Set Next loop condition * /
        /* End While */
        
        /* Save the first bit of Huffman encoding and encoding for each leaf node that is calculated */
        for (j=cd.start+1; j<n; j + +)
        {Huffcode[i].bit[j] = cd.bit[j];}
        Huffcode[i].start = Cd.start;
    /* End for */
    
    /* Outputs all existing encoded Huffman codes that have been saved */
    for (i=0; i<n; i++)
    {
        printf ("%d ' s Huffman code is:", i);
        for (j=huffcode[i].start+1; j < N; j + +)
        {
            printf ("%d", huffcode[i].bit[j]);
        }
        printf ("start:%d", Huffcode[i].start);
       
        printf ("\ n");
        
    }
/* for    (i=0;i<n;i++) {
    for (j=0;j<n;j++)
        {
             printf ("%d", huffcode[i].bit[j]);           
        }
        printf ("\ n");
        }*/
    printf ("decoding?") Please Enter code:\n ");
    scanf ("%s", &pp);
Decodeing (Pp,huffnode,n);
    Getch ();
    return 0;
}

Huffman Tree Coding

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.