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

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

 # 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个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问