HeJ_1996
2017-10-13 03:55
采纳率: 75%
浏览 1.0k
已采纳

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

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

 # 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
    已采纳
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题