m0_67949079 2022-04-17 09:46 采纳率: 40%
浏览 11

请问各位,下面的这个代码有啥问题啊 我想实现h哈夫曼树的建立,实现占存储空间最小的编码,但是不知道哪一步错了,出不了结果,卡了一个多小时了 c语言

问题遇到的现象和发生背景 可以输入初始数据,但是不出结果
问题相关代码,请勿粘贴截图

[](

```c

#include<stdio.h>
#include<stdlib.h>
typedef struct max
{
int a[8];
int size;
int sum;
}qq,qw;
typedef struct TreeNode HuffmanTree;
struct TreeNode{
int Weight;
HuffmanTree Left, Right;
};
int main()
{
qq h,u;
int ii,b,i;
int d[8],ll[8];
int we,ww;
char ss[58];
void make(qq z);
void num(int s[],char c[]);
int mindel(qq j);
qq minmake(qq j);
HuffmanTree Huffman(qq H);
void bianma(HuffmanTree t,int a[]);
HuffmanTree gh;
printf("请输入58个字符(字符限制为:a,s,d,f,g,h,j,k),以#结束");
for(i=0;i<=10;i++)
{scanf("%c",&ss[i]);
}
for(i=0;i<=10;i++)
printf("%c",ss[i]);
make(u);
num(u->a,ss);
for(ii=1;ii<=7;ii++)
{
d[ii]=u->a[ii];
ll[ii]=u->a[ii];
printf("%d",u->a[ii]);
}
minmake(u);
gh=Huffman(u);
bianma(gh,d);
return 0;
}
void make(qq z)
{
z->size=7;
z->sum=20;
z=(qq)malloc(sizeof(qw));
z->a[0]=59;
}
int mindel(qq j)
{
int parent,child,i;
int minItem, temp;
if (j==NULL) {
printf("最小堆已为空");
return;
}
minItem =j->a[1];
temp=j->a[j->size--];
for(parent=1;parent
2<=j->size;parent=child)
{child=parent
2;
if( (child!= j->size) && (j->a[child] > j->a[child+1]) )
child++;
if( temp <= j->a[child] ) break;
else
j->a[parent] = j->a[child];
}
j->a[parent] = temp;
return minItem;
}
qq minmake(qq j)
{
int parent,child,i;
int temp;
for(i=j->size/2;i>0;i--)
{temp=j->a[i];
for(parent=i;parent2<=j->size;parent=child)
{child=parent
2;
if( (child!= j->size) && (j->a[child] > j->a[child+1]) )
child++;
if( temp <= j->a[child] ) break;
else
j->a[parent] = j->a[child];
}
j->a[parent] = temp;
}
return j;
}
void num(int s[],char c[])
{int i;
int b=0;
s[0]=59;
for(i=0;i<=57;i++)
{if(c[i]=='a')
b++;
}
s[1]=b;
printf("a的个数:%d\n",b);
b=0;
for(i=0;i<=57;i++)
{if(c[i]=='s')
b++;
}
s[2]=b;
printf("s的个数:%d\n",b);
b=0;
for(i=0;i<=57;i++)
{if(c[i]=='d')
b++;
}
s[3]=b;
printf("d的个数:%d\n",b);
b=0;
for(i=0;i<=57;i++)
{if(c[i]=='f')
b++;
}
s[4]=b;
printf("f的个数:%d\n",b);
b=0;
for(i=0;i<=57;i++)
{if(c[i]=='g')
b++;
}
s[5]=b;
printf("g的个数:%d\n",b);
b=0;
for(i=0;i<=57;i++)
{if(c[i]=='h')
b++;
}
s[6]=b;
printf("h的个数:%d\n",b);
b=0;
for(i=0;i<=57;i++)
{if(c[i]=='j')
b++;
}
s[7]=b;
printf("j的个数:%d\n",b);
return s;
}

HuffmanTree Huffman(qq H)
{ int mindel(qq j);
void Insert(qq x,int y);
int i;
HuffmanTree T;
for (i = 1; i < H->size; i++) {
T = (HuffmanTree)malloc( sizeof( struct TreeNode) );
T->Left->Weight=mindel(H);
T->Right->Weight=mindel(H);
T->Weight = T->Left->Weight+T->Right->Weight;
Insert(H,T->Weight);
}
T->Weight= mindel(H);
return T;}
void Insert(qq x,int y)
{int i;
if(x->sum==x->size)
{
printf("最小堆已满");
return;}
i = ++x->size;
for ( ; x->a[i/2]>y; i/=2 )
x->a[i] = x->a[i/2];
x->a[i] = y;
}
void bianma(HuffmanTree t,int a[])
{int i,j;
for(i=1;i<=8;i++)
{
printf("%c的编码为:",a[i]);
if(t->Weight==a[i])
break;
if(t==NULL) break;
printf("1");
bianma(t->Left,a);
printf("0");
bianma(t->Right,a);
}
}

```)

运行结果及报错内容
我的解答思路和尝试过的方法

我试过一步步调试,他出结果,但是一整体运行,就啥也没有了

我想要达到的结果
  • 写回答

1条回答 默认 最新

  • 赵4老师 2022-04-17 12:02
    关注

    仅供参考:

    #include <iostream>
    #include <string>
    using namespace std;
    struct huffTree {
        int parent;
        int lchild;
        int rchild;
        int weight;
        string flag;
    };
    struct Lowest_node {
        char ch;
        int ch_num;
    };
    void coding(int length,huffTree *tree,int n,int &a,int &b) {
        int i;
        int r,s;
    
        r=s=length;
        for (i=0;i<n;i++) {
            if (tree[i].weight<r
             && tree[i].parent==-1) {
                r=tree[i].weight;
                a=i;
            }
        }
        for (i=0;i<n;i++) {
            if (tree[i].weight<s
             && i!=a
             && tree[i].parent==-1) {
                s=tree[i].weight;
                b=i;
            }
        }
    }
    void frequency(string str) {
        int i,j;
        int length=str.length();
        Lowest_node *node=new Lowest_node[length];
    
        for (i=0;i<length;i++) node[i].ch_num=0;
    
        int char_type_num=0;
        for (i=0;i<length;i++) {
            for (j=0;j<char_type_num;j++)
                if (str[i]==node[j].ch
                || ('a'<=node[j].ch && node[j].ch<='z'
                    && str[i]+32==node[j].ch))
                    break;//
            if (j<char_type_num) node[j].ch_num++;
            else {
                if ('A'<=str[i] && str[i] <= 'Z') node[j].ch=str[i]+32;
                else node[j].ch=str[i];
                node[j].ch_num++;
                char_type_num++;
            }
        }
        for (i=0;i<char_type_num;i++) {
            for (j=i;j<char_type_num;j++) {
                if (node[j].ch_num<node[j+1].ch_num) {
                    int temp;
                    char ch_temp;
                    temp=node[j].ch_num;
                    ch_temp=node[j].ch;
                    node[j].ch_num=node[j+1].ch_num;
                    node[j].ch=node[j+1].ch;
                    node[j+1].ch_num=temp;
                    node[j+1].ch=ch_temp;
                }
            }
        }
        for (i=0;i<char_type_num;i++)
            cout<<"字符"<<node[i].ch<<"出现了"<<node[i].ch_num<<"次"<<endl;
        huffTree *huff=new huffTree[2*char_type_num-1];
        huffTree temp;
        string *code=new string[2*char_type_num-1];
    
        for (i=0;i<2*char_type_num-1;i++) {
            huff[i].lchild=-1;
            huff[i].parent=-1;
            huff[i].rchild=-1;
            huff[i].flag=-1;
        }
        for (j=0;j<char_type_num;j++) huff[j].weight=node[j].ch_num;
        int min1,min2;
        for (int k=char_type_num;k<2*char_type_num-1;k++) {
            coding(length,huff,k,min1,min2);
            huff[min1].parent=k;
            huff[min2].parent=k;
            huff[min1].flag="0";
            huff[min2].flag="1";
            huff[k].lchild=min1;
            huff[k].rchild=min2;
            huff[k].weight=huff[min1].weight+huff[min2].weight;
        }
        for (i=0;i<char_type_num;i++) {
            temp=huff[i];
            while (1) {
                code[i]=temp.flag+code[i];
                temp=huff[temp.parent];
                if (temp.parent==-1) break;//
            }
        }
        cout<<"字符串的每个字符huffman编码为:"<<endl;
        for (i=0;i<char_type_num;i++) cout<<node[i].ch<<"  "<<code[i]<<endl;
        cout<<"整个字符串的huffman编码为:"<<endl;
        for (i=0;i<length;i++) {                                                                                     //S?
            for (j=0;j<char_type_num;j++) {
                if (str[i]==node[j].ch)
                    cout<<code[j];
            }
        }
        delete[] node;
        node=NULL;
        delete[] huff;
        huff=NULL;
        delete[] code;
        code=NULL;
    }
    int main() {
        int length=0;
        string str;
        cout<<"请输入一个字符串:";
        cin>>str;
        frequency(str);
        return 0;
    }
    //请输入一个字符串:2333abcde
    //字符3出现了3次
    //字符2出现了1次
    //字符a出现了1次
    //字符b出现了1次
    //字符c出现了1次
    //字符d出现了1次
    //字符e出现了1次
    //字符串的每个字符huffman编码为:
    //3  11
    //2  000
    //a  001
    //b  010
    //c  011
    //d  100
    //e  101
    //整个字符串的huffman编码为:
    //000111111001010011100101
    
    
    
    评论

报告相同问题?

问题事件

  • 修改了问题 5月4日
  • 创建了问题 4月17日

悬赏问题

  • ¥15 matlab无法读取mat文件,如何解决?
  • ¥15 51单片机读写24C02
  • ¥50 grlb复位怎么能够不回调?也不卡在home状态?
  • ¥15 win系统下做一个开机自动最大化运行某应用程序的执行文件
  • ¥15 grlb复位,设置设置返回行程为0,卡在home状态,怎么解决?
  • ¥100 CubeIDE更换芯片以及调整代码
  • ¥50 有没有可以远程指导问题
  • ¥15 origin柱状图,分组如何分
  • ¥15 两个不同IP互通的配置命令
  • ¥15 office弹窗激活问题