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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog