Huffman's c language implementation code

  • 2020-04-02 01:22:16
  • OfStack

We set up a structure array called HuffNode to hold information about each node in the Huffman tree. According to the properties of binary trees, the Huffman tree with n leaf nodes has a total of 2n-1 nodes, so the size of the array HuffNode is set to 2n-1. The HuffNode structure has the weight, lchild, rchild, and parent domains. Where, the weight field stores the weight value of the node, and lchild and rchild store the order number of the node's left and right children in the HuffNode array respectively, so as to establish the relationship between the nodes. To determine whether a node has been added to the Huffman tree to be established, the value of the parent field is used. Parent starts at -1. When a node is added to the tree, the parent value of the node is the number of its parent in the HuffNode array, instead of -1.

Figure out the code of leaf node:
This process is essentially starting from the leaf node in the established Huffman tree, and going back to the root node along the parent chain domain of the node. Since the Huffman code of a character is a sequence of 0 and 1 formed by each branch on the path from the root node to the corresponding leaf node, the branch code obtained first is the lower part of the desired code, and the branch code obtained later is the higher part of the desired code. We can set up an array of structures called HuffCode to hold the Huffman encoding information for each character. The structure of the array elements has two fields: bit and start. Where, the domain bit is a one-dimensional array used to store the Huffman encoding of characters, and start represents the starting position of the encoding in the array bit. So, for the i-th character, its Huffman code is stored in the bits from HuffCode[I].start to n in HuffCode[I].bit.



#include <stdio.h>
#define MAXBIT      100
#define MAXVALUE  10000
#define MAXLEAF     30
#define MAXNODE    MAXLEAF*2 -1
typedef struct 
{
    int bit[MAXBIT];
    int start;
} HCodeType;        
typedef struct
{
    int weight;
    int parent;
    int lchild;
    int rchild;
} HNodeType;        

void HuffmanTree (HNodeType HuffNode[MAXNODE],  int n)
{ 
    
    int i, j, m1, m2, x1, x2;
    
    for (i=0; i<2*n-1; i++)
    {
        HuffNode[i].weight = 0;
        HuffNode[i].parent =-1;
        HuffNode[i].lchild =-1;
        HuffNode[i].lchild =-1;
    } 
    
    for (i=0; i<n; i++)
    {
        printf ("Please input weight of leaf node %d: n", i);
        scanf ("%d", &HuffNode[i].weight);
    } 
    
    for (i=0; i<n-1; i++)
    {
        m1=m2=MAXVALUE;     
        x1=x2=0;
        
        for (j=0; j<n+i; j++)
        {
            if (HuffNode[j].weight < m1 && HuffNode[j].parent==-1)
            {
                m2=m1; 
                x2=x1; 
                m1=HuffNode[j].weight;
                x1=j;
            }
            else if (HuffNode[j].weight < m2 && HuffNode[j].parent==-1)
            {
                m2=HuffNode[j].weight;
                x2=j;
            }
        } 
            
        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, %dn", i+1, HuffNode[x1].weight, HuffNode[x2].weight);  
        printf ("n");
    } 
} 
int main(void)
{
    HNodeType HuffNode[MAXNODE];            
    HCodeType HuffCode[MAXLEAF],  cd;       
    int i, j, c, p, n;
    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)   
        {
            if (HuffNode[p].lchild == c)
                cd.bit[cd.start] = 0;
            else
                cd.bit[cd.start] = 1;
            cd.start--;        
            c=p;                    
            p=HuffNode[c].parent;    
        } 

        
        for (j=cd.start+1; j<n; j++)
        { HuffCode[i].bit[j] = cd.bit[j];}
        HuffCode[i].start = cd.start;
    } 

    
    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 ("n");
    }
    getch();
    return 0;
}


Related articles: