sinat_35066848 2016-06-06 14:01 采纳率: 40%
浏览 1610

C语言 Huffman树编码的问题

我已经读入文本,然后定义了一个指针栈,把所有的结点都按频率存入栈中,不知道现在如何利用现有的数组,来构建一个Huffman树?请大神指点。
部分代码如下,还没写完。

 #include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct tnode{
    char word;
    int count;
    int flag;
    struct tnode *left;
    struct tnode *right;
};
struct tnode *p,*q;
struct tnode str[128];
struct tnode *arr[128];
int curr=0;
int temp;
int main()
{
    FILE *IN,*OUT;
    IN=fopen("input.txt","r");
    char c;
    int i,num,j;
    int temp;
    while((c=fgetc(IN))!=EOF){
        str[(int)c].word=c;
        str[(int)c].count++;
    }
    str[10].count=0;
    /*for(i=0;i<128;i++){
        printf("%d  %c  %d\n",i,str[i].word,str[i].count);
    }*/
    for(i=0;i<128;i++){
        if(str[i].count!=0){
            p=(struct tnode*)malloc(sizeof(struct tnode));
            p->count=str[i].count;
            p->word=str[i].word;
            p->left=p->right=NULL;
            arr[curr++]=p;
        }
    }
    for(i=0;i<curr;i++){
        for(j=i+1;j<curr;j++){
            if((arr[j]->count<arr[i]->count)||((arr[j]->count==arr[i]->count)&&(arr[j]->word<arr[i]->word))){
                q=arr[i];
                arr[i]=arr[j];
                arr[j]=q;
            }
        }
    }
    /*for(i=0;i<curr;i++){
        printf("%2d  %c  %d\n",i,arr[i]->word,arr[i]->count);
    }*/
    fseek(IN,0,SEEK_SET);
    while(c=fgetc(IN)!=EOF){
        if(c=='\n'){
            continue;
        }

    }
    return 0;
}
  • 写回答

1条回答 默认 最新

  • 咖啡不加盐 2016-06-06 14:11
    关注

    没明白你的意思,题目的意思就是为了构建霍夫曼树?

    评论

报告相同问题?

悬赏问题

  • ¥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