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
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型