7-2 Huffman coding and decoding (25 points) (c language)

Posted by SouThPaw09 on Fri, 04 Mar 2022 23:56:37 +0100


@Sparks
2022-03-05
Data structure experiment of gxust

subject

Write a Huffman coding and decoding program.

Each word frequency is coded according to the order of Huffman's word frequency, and each word frequency is given from Huffman's word frequency to Huffman's word frequency.

In order to ensure the uniqueness of the constructed Huffman tree, this topic is limited as follows:

(1) When selecting the two binary trees with the smallest weight of the root node, the one with the smaller weight is selected as the left subtree.

(2) If the root node weights of multiple binary trees are equal, they are divided into left and right in order. The first one appears as the left subtree and the second one appears as the right subtree.

When generating Huffman code, the left branch of Huffman tree is marked as 0 and the right branch is marked as 1.

Input format:
Enter the number of characters n in the first line;

Input the corresponding characters and their word frequency from the second line to the nth line (which can be integers and decimals);

The last line inputs the string to be decoded.

Output format:
First, the codes of all characters are output in the order of the tree, and each code occupies one line;

The last line outputs the original text to be decoded, plus the word original:.

There are no spaces in the output

Input sample:

3
m1
n1
c2
10110

Output example:

c:0
m:10
n:11
original:mnc

analysis

The key is the construction and decoding of Huffman coding, but there is a difficulty here: separating characters and numbers, and numbers, not only integers, but also floating-point numbers

code

For some reasons, I can't provide complete code. The specific implementation still depends on your students' thinking and debugging.
There are few comments because I wrote them all the way. There are few comments. The code can only be used for reference. After all, it took me a lot of time to write them

#include<stdio.h>
#include<stdlib.h>
#define MAXVALUE 32767
typedef struct hf{//Huffman tree 
    int weight;
    int parent;
    int lchild;
    int rchild;
}Htree;

typedef struct hc{//Huffman coding
    int* bit;//Huffman code for storing the current node
    int start;//bit[start]-bit[n] store Huffman code
    char ch;
}Hcode;




Htree* InitHtree(int* w,int leght){
    int i;
    Htree* HT = (Htree*)malloc(sizeof(Htree)*(2*leght-1));
    for(i = 0; i<leght; i++){
        HT[i].weight = w[i];
        HT[i].parent = -1;
        HT[i].lchild = -1;
        HT[i].rchild = -1;
        }
        return HT;
}

Hcode* InitHcode(int leght,char* ch){
    int i;
    Hcode* HC = (Hcode*)malloc(sizeof(Hcode)*leght);

    for(i = 0; i<leght; i++){
        HC[i].bit = (int*)malloc(sizeof(int)*leght);
        HC[i].ch = ch[i];
    }
    
    return HC;
}


void CreateHuffTree(Htree* HT,int leght){			//Constructing Huffman tree
    int i,j,a,b,min1,min2;
    int l = 2*leght-1;
    for(i = leght;i<l;i++){
        a = MAXVALUE;
        b = MAXVALUE;
        min1 = 0;
        min2 = 0;
            for(j = 0; j < leght+i; j++){ //Select the smallest weight
                if(HT[j].parent == -1 && HT[j].weight < a){
                    a = HT[j].weight;
                    min1 = j;
                }
            }
        for(j = 0; j < leght+i; j++){ //Select the next smallest weight
                 if(HT[j].parent == -1 && HT[j].weight < b && j!= min1){
                    b = HT[j].weight;
                    min2 = j;
                }
            }

        HT[i].weight = HT[min1].weight+HT[min2].weight;
        HT[i].parent = -1;
        HT[i].lchild = min1;
        HT[i].rchild = min2;
        HT[min1].parent = i;
        HT[min2].parent = i;
       
        leght++;//It's not necessary.

    }

}
void PrintHuffTree(Htree* HT,Hcode* HC,int index,int leght){//Output Huffman tree
 

     int i;
     printf("\n Various data of Huffman tree are shown in the table below:\n");
	 printf("        node i weight parent    lchid    rchild\n");
	 for(i=0;i<index;i++){
        printf("\t%d\t%d\t%d\t%d\t%d\n",i,HT[i].weight,HT[i].parent,HT[i].lchild,HT[i].rchild);

     }




}		
void CreateHuffCode(Hcode* HC,Htree* HT,int leght){//Constructive Huffman coding
    
    int i,j,c,p;
    
    for(i = 0; i<leght;i++){
        Hcode* cd = (Hcode*)malloc(sizeof(Hcode));
        cd->bit = (int*)malloc(sizeof(int)*leght);
        cd->start = leght-1;
        c = i;
        p = HT[c].parent;

        while(p!=-1){

            if(HT[p].lchild == c){
                cd->bit[cd->start] = 0;
            }else{
                cd->bit[cd->start] = 1;
            }
            cd->start--;
            c = p;
            p = HT[c].parent;

        }
        for(j = cd->start+1; j < leght; j++){
            HC[i].bit[j] = cd->bit[j];

        }
 
        
        HC[i].start = cd->start;
        
        free(cd);
    }
}			
void PrintHuffcode(Hcode* HC,int leght){ //Output Huffman code of each leaf node

	int i,j;
	printf("\n The Huffman code of each leaf node is:\n");
	for(i = 0;i < leght;i++){	
        for(j = HC[i].start+1; j < leght; j++){
			printf("%d",HC[i].bit[j]);
        }
        printf("------------>>>%d",i);
        printf("\n");
	}

}	

void Decode(Htree* HT,Hcode* HC,int lenght,char* mch){//decoding
    printf("original:");
    int index;
    int i;
    int j = 0;
    index = 2*lenght-2;
    while(mch[j]!='\0'){
        if(mch[j] == '0'){
            index = HT[index].lchild;
        }else{
            index = HT[index].rchild;
        }
        if(HT[index].lchild == -1 && HT[index].rchild == -1){          
                printf("%c",HC[index].ch);
                index = 2*lenght-2;          
        }
        j++;
    }
    printf("\n");

}

int main(void){
    int w[3] = {1,1,2};
    char ch[3] = {'m','n','c'};
    char mch[MAXVALUE] = {"011100111001110011100111001110"};


    int leght = 3;
    int i;
    Htree * HT = InitHtree(w,leght);
    Hcode * HC = InitHcode(leght,ch);

    CreateHuffTree(HT,leght);

    CreateHuffCode(HC,HT,leght);

    PrintHuffTree(HT,HC,2*leght-2,leght);
    PrintHuffcode(HC,leght);
    Decode(HT,HC,leght,mch);



    return 0;
}

I also stayed up late to write. My head is bald

I'm a rookie. Big guys spray gently

Topics: C Programming data structure