【数据结构基础】哈夫曼编码/译码课程设计

#include
#include
#include
#define N 20   
typedef struct {
    char ch;
    int weight;
    int lchild, rchild, parent;
}Hufnode,*THufnode;
typedef struct{
    char *code;
    int length;
}CodeType;
//选择排序法 找1~n+i-1中parent不等于-1,且权值最小的两个结点,只选不排 
void Select(THufnode T, int len, int *s1, int *s2) { 
    int i = 1, j, min1, min2;
    while (i <= len) {//找到*s1 
        if (i <= len && T[i].parent == -1) {
            min1 = i;
            for (j = i; j <= len; j++) {
                if (T[min1].weight >= T[j].weight && T[j].parent == -1) {
                    min1 = j;
                }
            }
            *s1 = min1;
            break;
        }
        else {
            i++;
        }
    }
    i = 1;
    while (i <= len) {//找到*s2 
        if (i <= len && T[i].parent == -1 && i != min1) {
            min2 = i;
            for (j = i; j <= len; j++) {
                if (T[min2].weight >= T[j].weight && T[j].parent == -1 && T[j].weight >T[*s1].weight) {
                    min2 = j;
                }
            }
            *s2 = min2;
            break;
        }
        else {
            i++;
        }
    }
}
/*----------------Create hufmanTree-------------------------------*/
void Create_HufTree(THufnode *T, int n) {
    *T=(THufnode)malloc(sizeof(Hufnode)*(2*n));
    int i, m, sum;
    int c,k,f,j;
    char temp[N];
    m = n*2-1;
    for (i = 1; i <= n; i++) {
        printf("Please input the weight and node name in turn:");
        scanf("%d %c",&((*T)[i].weight),&((*T)[i].ch));
        (*T)[i].parent = -1;
        (*T)[i].lchild = -1;
        (*T)[i].rchild = -1;
    }
    for (i = n + 1; i <= m; i++) {
        (*T)[i].ch=' '; 
        (*T)[i].weight = -1;
        (*T)[i].lchild = -1;
        (*T)[i].rchild = -1;
        (*T)[i].parent = -1;
    }
    //初始化哈夫曼树 
    for (i = 1; i <= m; i++) {
        printf("(*T)[%d].ch=%c\t (*T)[%d].weight=%d\t (*T)[%d].lchild=%d\t (*T)[%d].rchild=%d\n ", i,(*T)[i].ch,i, (*T)[i].weight, i, (*T)[i].lchild, i, (*T)[i].rchild);
    }
    //生成哈夫曼树 
    for (i = 1; i <= n - 1; i++) {
        int s1, s2;
        Select(*T, n + i - 1, &s1, &s2);
        sum = (*T)[s1].weight + (*T)[s2].weight;
        (*T)[s1].parent = n + i;
        (*T)[s2].parent = n + i;
        (*T)[n + i].lchild = s1;
        (*T)[n + i].rchild = s2;
        (*T)[n + i].weight = sum;
    }
    for (i = 1; i <= m; i++) {
        printf("(*T)[%d].weight=%c\t (*T)[%d].weight=%d\t (*T)[%d].lchild=%d\t (*T)[%d].rchild=%d\t (*T)[%d].parent=%d\n",i, (*T)[i].ch, i, (*T)[i].weight, i, (*T)[i].lchild, i, (*T)[i].rchild, i, (*T)[i].parent);
    }
}


/*--------------Hufman encoding-----------------------------*/    
void HufTree_Encoding(THufnode *T,int n){
    int i,j,c,k,f; 
    char temp[50];
    CodeType cd[50];    
    for(i=1;i<=n;i++){
        c=0;
        cd[i].length=0;
        for(k=i,f=(*T)[i].parent;f!=-1;k=f,f=(*T)[f].parent){
            if((*T)[f].lchild==k){
                temp[c]='0';
                c++;
                cd[i].length++;
            }
            else{
                temp[c]='1';
                c++;
                cd[i].length++;
            }
        }
        cd[i].code=(char*)malloc(c+1);
        cd[i].code[c]='\0';
        c--;
        k=0;
        while(c>=0){
            cd[i].code[k++]=temp[c--];
        } 
    }
    for(i=1;i<=n;i++){
        printf("%c: ",(*T)[i].ch);
        for(j=0;jlength;j++){
            printf("%c",cd[i].code[j]);
        }
        printf("\n");           
    } 
}    
/*-----------------Hufman decoding-------------------------------*/
void HufTree_Decoding(THufnode *T,int n){
    char code[50];
    int i,len;
    int f;
    char temp[50];
    printf("Please input message flow:");
    scanf("%s",code);
    len=strlen(code);
    f=2*n-1;
    for(i=0;i<=len;i++){
        if((*T)[f].lchild!=-1 && (*T)[f].rchild!=-1){
            if(code[i]=='0'){
                f=(*T)[f].lchild;
            }else{
                if(code[i]=='1'){
                    f=(*T)[f].rchild;   
                }
            }
        }else{
            printf("%c ",(*T)[f].ch);
            i--;
            f=2*n-1;
        }
    }
}

int main(void) {
    THufnode T=NULL;
    int len,i,option;
    int w[N];
    CodeType cd[2*N];
    while(1){
    printf("\n\t\t****************hufman coding******************\n");
    printf("\t\t*\t     1:Create HuffmanTree             *\n");
    printf("\t\t*\t     2:Huffman Encoding               *\n");
    printf("\t\t*\t     3:Huffman Decoding               *\n");
    printf("\t\t*\t     0:exit                           *\n");
    printf("\t\t***********************************************\n");
    printf("\n请输入序号:");
    scanf("%d", &option);
    switch(option){
        case 1:{
            printf("Please input the number of nodes: ");
            scanf("%d",&len);
            Create_HufTree(&T,len);
            break;
        }
        case 2:{
            if(T==NULL){
                printf("Before,You must create Huffman Tree");
                break;
            }
            HufTree_Encoding(&T,len);
            break;
        }
        case 3:{
            HufTree_Decoding(&T,len);
            break;
        }
        case 0:{
            exit(0);
        }
    }
}
    return 0;
}

你可能感兴趣的:(数据结构和算法,数据结构,哈夫曼编码)