HeJ_1996 2017-10-13 03:55 采纳率: 100%
浏览 1065
已采纳

构建一个哈弗曼树,并计算其带权路径长度

不知道是哪里出现了问题,运行结果不对,并且一点调试就直接结束了,麻烦大神们帮我看一下

 # include <stdio.h>
static int h = 0;
typedef struct tree
{
    char data;
    int quan;
    bool min;
    struct tree* zuo;
    struct tree* you;
}tree;

struct hafmman
{
    char data;
    char lujing[11];
}hafu[100];

void gettree(tree* a,int shij,int youx)
{
    int c = 0;
    tree temp;

    if (youx*2-1 != shij)
    {
        printf("请输入合法参数");
        return;
    }
    while (youx!=shij)
    {
        for (int i=c;i<shij;++i)
        {
            if (a[c].quan>a[i].quan)
            {
                temp = a[i];
                a[i] = a[c];
                a[c] = temp;
            }
            if (a[c+1].quan>a[i].quan)
            {
                temp = a[i];
                a[i] = a[c+1];
                a[c+1] = temp;
            }
        }
            a[youx].quan = a[c].quan + a[c+1].quan;
            a[youx].zuo = &a[c];
            a[youx].you = &a[c+1];
            youx++;
            c=c+2;
    }
}

int bianltree(tree * tr,char* ch,int deep)
{
    static int i = 0;
    int j = 0;
    int sum = 0;
    if (!tr->min)
    {   
        ch[i++] = '0';
        sum += bianltree(tr->zuo,ch,deep+1);
        ch[i++] = '1';
        sum += bianltree(tr->you,ch,deep+1);
    }
    if (tr->min)
    {
        ch[i] = '\0';
        printf("%c : %s",tr->data,ch);
        hafu[h].data = tr->data;
        while (i!=j || i>10)
        {
            hafu[h].lujing[j] = ch[j];
            j++;
        }
        h++;
        i--;
        sum = tr->quan * deep;
    }
    return sum;
}

int main ()
{
    tree a[9] = {{'a',5,true},{'b',4,true},{'c',2,true},{'d',6,true},{'e',2,true}};
    gettree(a,9,5);
    char ch[100];
    int sum = bianltree(&a[8],ch,0);
    printf("%d",sum);
    return 0;
}
  • 写回答

1条回答

  • threenewbee 2017-10-13 15:47
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料