史天亮123 2022-11-15 11:09 采纳率: 75%
浏览 22
已结题

哈夫曼编码有些问题,循环和输出编码有问题

问题遇到的现象和发生背景

哈夫曼编码函数有些问题,输出哈夫曼编码有些问题

用代码块功能插入代码,请勿粘贴截图
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
typedef struct{
    int weight;
    int parent,lchild,rchild;
    int flag;
}HTNode,*HuffmanTree;
typedef char** HuffmanCode;


void Select(HuffmanTree HT,int end,int *s1,int *s2){
    int min=1,i;
    for(i=1;i<=end;i++){
        if(HT[i].flag!=0){
            continue;
        }
        while(HT[min].flag!=0){
            min++;
        }
        if(HT[i].parent==0&&HT[min].weight>HT[i].weight){
                min=i;
        }    
    }
    *s1=min;
    HT[min].flag=1;
    min=1;
    for(i=1;i<=end;i++){
        if(HT[i].flag!=0){
            continue;
        }
        while(HT[min].flag!=0){
            min++;
        }
        if(HT[i].parent==0&&HT[min].weight>HT[i].weight){
                min=i;
        }
    }
    *s2=min;
    HT[min].flag=1;
}

void CreateHuffmanTree(HuffmanTree *HT,int n){
    if(n<=1) return;
    int i,m=2*n-1;
    *HT=(HuffmanTree)malloc((m+1)*sizeof(HTNode));
    HuffmanTree p=*HT;
    for(i=1;i<=m;i++){
        p[i].parent=0;
        p[i].lchild=0;
        p[i].rchild=0;
        p[i].flag=0;
    }
    for(i=1;i<=n;i++){
        puts("输入权值:");
        scanf("%d",&(p[i].weight));
    }
    int s1,s2;
    for(i=n+1;i<=m;i++){
        Select(*HT,i-1,&s1,&s2);
        p[s1].parent=p[s2].parent=i;
        p[i].lchild=s1;
        p[i].rchild=s2;
        p[i].weight=p[s1].weight+p[s2].weight;
    }    
}

void CreateHuffmanCode(HuffmanTree HT,HuffmanCode *HC,int n){
    int i;
    (*HC)=(HuffmanCode)malloc((n+1)*sizeof(char *));
     
    if(!(*HC)) printf("kong");
    char *cd=(char *)malloc(n*sizeof(char));
    if(!cd) printf("kong");
    cd[n-1]='\0';
    
    for(i=1;i<=n;i++){
        int start=n-1;
        int c=i;
        int f=HT[i].parent;
        
        while(f!=0){
            --start;
            if(HT[f].lchild==c){
                cd[start]='0';
            }else{
                cd[start]='1';
            }
            c=f;
            f=HT[f].parent;
        }
        //printf("%s",cd);
        (*HC)[i]=(char *)malloc((n-start)*sizeof(char));
        if(!(*HC)[i]) printf("kong");
        strcpy((*HC)[i],cd+start);
        printf("%s",HC[i]);
    }
    puts("--");
    free(cd);
}





void printHuffmanTree(HuffmanTree HT,int n){
    puts("节点\tweight\tparent\tlchild\trchild");
    int i;
    for(i=1;i<=n;i++){
        printf("%d  \t%d  \t%d  \t%d  \t%d\n",i,HT[i].weight,HT[i].parent,HT[i].lchild,HT[i].rchild);
    }
    
    
}


int main(int argc, char *argv[]) {
    HuffmanTree HT;
    HuffmanCode HC;
    CreateHuffmanTree(&HT,8);
    printHuffmanTree(HT,15);
    puts("哈夫曼树");
    
    CreateHuffmanCode(HT,&HC,8);
    
    int i;
     for(i=0;i<=8;i++){
         printf("%s",HC[i]);
     }
    return 0;
}

运行结果及报错内容

img

img

img

@qzjhjxj

  • 写回答

2条回答 默认 最新

  • qzjhjxj 2022-11-15 13:36
    关注

    两处小错误,见修改注释,供参考:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    /* run this program using the console pauser or add your own getch, system("pause") or input loop */
    typedef struct {
        int weight;
        int parent, lchild, rchild;
        int flag;
    }HTNode, * HuffmanTree;
    typedef char** HuffmanCode;
    void Select(HuffmanTree HT, int end, int* s1, int* s2) {
        int min = 1, i;
        for (i = 1; i <= end; i++) {
            if (HT[i].flag != 0) {
                continue;
            }
            while (HT[min].flag != 0) {
                min++;
            }
            if (HT[i].parent == 0 && HT[min].weight > HT[i].weight) {
                min = i;
            }
        }
        *s1 = min;
        HT[min].flag = 1;
        min = 1;
        for (i = 1; i <= end; i++) {
            if (HT[i].flag != 0) {
                continue;
            }
            while (HT[min].flag != 0) {
                min++;
            }
            if (HT[i].parent == 0 && HT[min].weight > HT[i].weight) {
                min = i;
            }
        }
        *s2 = min;
        HT[min].flag = 1;
    }
    void CreateHuffmanTree(HuffmanTree* HT, int n) {
        if (n <= 1) return;
        int i, m = 2 * n - 1;
        *HT = (HuffmanTree)malloc((m + 1) * sizeof(HTNode));
        HuffmanTree p = *HT;
        for (i = 1; i <= m; i++) {
            p[i].parent = 0;
            p[i].lchild = 0;
            p[i].rchild = 0;
            p[i].flag = 0;
        }
        for (i = 1; i <= n; i++) {
            puts("输入权值:");
            scanf("%d", &(p[i].weight));
        }
        int s1, s2;
        for (i = n + 1; i <= m; i++) {
            Select(*HT, i - 1, &s1, &s2);
            p[s1].parent = p[s2].parent = i;
            p[i].lchild = s1;
            p[i].rchild = s2;
            p[i].weight = p[s1].weight + p[s2].weight;
        }
    }
    void CreateHuffmanCode(HuffmanTree HT, HuffmanCode* HC, int n) {
        int i;
        (*HC) = (HuffmanCode)malloc((n + 1) * sizeof(char*));
        if (!(*HC))   printf("kong");
        char* cd = (char*)malloc(n * sizeof(char));
        if (!cd)   printf("kong");
        cd[n - 1] = '\0';
        for (i = 1; i <= n; i++) {
            int start = n - 1;
            int c = i;
            int f = HT[i].parent;
            while (f != 0) {
                --start;
                if (HT[f].lchild == c) {
                    cd[start] = '0';
                }
                else {
                    cd[start] = '1';
                }
                c = f;
                f = HT[f].parent;
            }
            //printf("%s",cd);
            (*HC)[i] = (char*)malloc((n - start) * sizeof(char));
            if (!(*HC)[i]) printf("kong");
            strcpy((*HC)[i], cd + start);
            printf("%s", (*HC)[i]);//printf("%s",HC[i]); 修改
        }
        puts("--");
        free(cd);
    }
    void printHuffmanTree(HuffmanTree HT, int n) {
        puts("节点\tweight\tparent\tlchild\trchild");
        int i;
        for (i = 1; i <= n; i++) {
            printf("%d  \t%d  \t%d  \t%d  \t%d\n", i, 
                HT[i].weight, HT[i].parent, HT[i].lchild, HT[i].rchild);
        }
    }
    int main(int argc, char* argv[]) {
        HuffmanTree HT;
        HuffmanCode HC;
        CreateHuffmanTree(&HT, 8);
        printHuffmanTree(HT, 15);
        puts("哈夫曼树");
        CreateHuffmanCode(HT, &HC, 8);
        int i;
        for (i = 1; i <= 8; i++) { // for(i=0;i<=8;i++) 修改
            printf("%s", HC[i]);
        }
        return 0;
    }
    
    

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 11月24日
  • 已采纳回答 11月16日
  • 创建了问题 11月15日

悬赏问题

  • ¥50 有偿求qftp工具。能连接,下载文件,发送代码,windows环境,最好qt6 要qt creator写的
  • ¥70 刚刚看到一个人的网站居然是通过cname访问的
  • ¥15 Attributeerror:super object has no attribute '__sklearn_tags__'_'
  • ¥15 逆置单链表输出不完整
  • ¥15 宇视vms-B200-A16@R启动不了,如下图所示,在软件工具搜不到,如何解决?(操作系统-linux)
  • ¥500 寻找一名电子工程师完成pcb主板设计(拒绝AI生成式答案)
  • ¥15 关于#mysql#的问题:UNION ALL(相关搜索:sql语句)
  • ¥15 matlab二位可视化能否针对不同数值范围分开分级?
  • ¥15 已经创建了模拟器但是不能用来运行app 怎么办😭自己搞两天了
  • ¥15 关于#极限编程#的问题,请各位专家解答!