| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 709 人关注过本帖
标题:赫夫曼树编码的实现
只看楼主 加入收藏
中国电信
Rank: 1
等 级:新手上路
帖 子:16
专家分:9
注 册:2013-10-20
结帖率:100%
收藏
已结贴  问题点数:10 回复次数:3 
赫夫曼树编码的实现
自己看书(赫夫曼树),觉得懂了便自己动手敲一个书上的题目,可是实现不了,真的不知道哪错了。求搞手指教。下面是我的代码:

#include<stdio.h>
#include<string.h>
typedef struct
{
    unsigned int weight;
    unsigned int parent,lchild,rchild;
}HTNODE,*huffmantree;
typedef char * *huffmancode;
void select(huffmantree HT,int n,int *s1,int *s2);
void huffmancoding(huffmantree HT,huffmancode HC,int *w,int n);
int main()
{
    huffmantree HT;
    huffmancode HC;
    int w[8];
    int i,m,n;
    for(i=0;i<8;i++)
    {
        printf("请输入第%d个编码数\n",i+1);
        scanf("%d",&w[i]);
    }
    for(i=0;i<8;i++)
    {
        printf("%d\n",w[i]);
    }
    printf("niah\n");
    huffmancoding(HT,HC,w,8);
    printf("输出的编码是\n");
    for(n=0;n<9;n++)
    {
        printf("%s\n",HC[n]);
    }
}
void select(huffmantree HT,int n,int *s1,int *s2)
{
    int i,i1,i2,i3;
    int min1,min2;
    min1=min2=32767;
    for(i=1;i<=n;i++)
    {
        if(HT[i].parent==0)
        {
            if(HT[i].weight<min1)
            {
                min1=HT[i].weight;
                *s1=i;
            }
            

        }
        else
        {
            continue;
        }
    }
    for(i=1;i<=n;i++)
    {
        if(HT[i].parent==0)
        {
            if(min1<HT[i].weight<min2)
            {
                min2=HT[i].weight;
                *s2=i;
            }
            

        }
        else
        {
            continue;
        }
    }
}

void huffmancoding(huffmantree HT,huffmancode HC,int *w,int n)
{
    int m,i,x,start,c,f;
    huffmantree p;
    int *s1,*s2;
    char *cd;
    x=0;
    s1=s2=&x;
    if(n<=1)
    {
        return;
    }
    printf("dazhele\n");
    m=2*n-1;
    HT=(huffmantree)malloc((m+1)*sizeof(HTNODE));
    for(p=HT,i=1;i<=n;++i,++p,++w)
    {
        p[i].weight=w[i-1];
        printf("dazhele1\n");
    }
    for(;i<m;++i)
    {
        p[i].weight=0;
        printf("dazhele2\n");
    }
    for(i=n+1;i<=m;++i)
    {
        printf("dazhele3\n");
        select(HT,i-1,s1,s2);
        printf("dazhele4\n");
        HT[*s1].parent=i;
        HT[*s2].parent=i;
        HT[i].lchild=s1;
        HT[i].rchild=s2;
        HT[i].weight=HT[*s1].weight+HT[*s2].weight;
    }
    printf("dazhele5\n");
    HC=(huffmancode)malloc((n+1)*sizeof(char *));
    cd=(char *)malloc(n*sizeof(char));
    cd[n-1]="\0";
    printf("dazhele6\n");
    for(i=1;i<=n;++i)
    {
        printf("dazhele7\n");
        start=n-1;
        for(c=i,f=HT[i].parent;f!=0;c=f,f=HT[f].parent)
        {
            printf("dazhele8\n");
            if(HT[f].lchild==c)
            {
                printf("dazhele9\n");
                cd[--start]="0";
                printf("dazhele11\n");
            }
            else
            {
                printf("dazhele12\n");
                cd[--start]="1";
                printf("dazhele13\n");
            }
            

        }
        printf("dazhele9\n");
        HC[i]=(char *)malloc((n-start)*sizeof(char));
        strcpy(HC[i],&cd[start]);
    }
    free(cd);

}
谢谢!!!!
搜索更多相关主题的帖子: include parent 
2013-11-09 00:40
yuccn
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:何方
等 级:版主
威 望:167
帖 子:6815
专家分:42393
注 册:2010-12-16
收藏
得分:10 
http://blog.
很详细的一个文章

我行我乐
公众号:逻辑客栈
我的博客:
https://blog.yuccn. net
2013-11-09 08:30
中国电信
Rank: 1
等 级:新手上路
帖 子:16
专家分:9
注 册:2013-10-20
收藏
得分:0 
以下是引用yuccn在2013-11-9 08:30:12的发言:

http://blog.
很详细的一个文章

谢谢!!
2013-11-09 15:16
中国电信
Rank: 1
等 级:新手上路
帖 子:16
专家分:9
注 册:2013-10-20
收藏
得分:0 
完成了,可还是有一些地方没搞懂,只能说一句c语言中的太高深了。下面是代码:

#include<stdio.h>
#include<string.h>
typedef struct
{
    unsigned int weight;
    unsigned int parent,lchild,rchild;
}HTNODE,*huffmantree;
typedef char * *huffmancode;
void select(huffmantree HT,int n,int *s1,int *s2);
void huffmancoding(huffmantree HT,huffmancode HC,int w[],int n);
int main()
{
    huffmantree HT;
    huffmancode HC;
    int w[8];
    int i,m,n;
    for(i=0;i<8;i++)
    {
        printf("请输入第%d个编码数\n",i+1);
        scanf("%d",&w[i]);
    }
    huffmancoding(HT,HC,w,8);

}
void select(huffmantree HT,int n,int *s1,int *s2)
{
    int i,i1,i2,i3,m;
    int min1,min2;
    min1=min2=32767;
    for(i=1;i<=n;i++)
    {
        if(HT[i].parent==0)
        {
            if(HT[i].weight<min1)
            {
                min1=HT[i].weight;
                *s1=i;
            }
            else
            {

            }
            

        }
        else
        {
            continue;
        }
    }
    for(i=1;i<=n;i++)
    {
        if(HT[i].parent==0)
        {
            if(min1<=HT[i].weight&&HT[i].weight<min2&&i!=*s1)
            {
                min2=HT[i].weight;
                *s2=i;
            }
            else
            {

            }
            
            

        }
        else
        {
            continue;
        }
    }
}

void huffmancoding(huffmantree HT,huffmancode HC,int w[],int n)
{
    int m,i,x1,x2,start,c,f;
    huffmantree p;
    int *s1,*s2;
    char *cd;
    x1=x2=0;   
    s1=&x1;
    s2=&x2;
    if(n<=1)
    {
        return;
    }
    m=2*n-1;
    HT=(huffmantree)malloc((m+1)*sizeof(HTNODE));
    for(p=HT,i=1;i<=n;i++)
    {
        p[i].weight=w[i-1];
        p[i].parent=0;
    }
    for(i=n+1;i<=m;i++)
    {
        p[i].weight=0;
        p[i].parent=0;
    }
    for(i=n+1;i<=m;++i)
    {
        select(HT,i-1,s1,s2);
        HT[*s1].parent=i;
        HT[*s2].parent=i;
        HT[i].lchild=*s1;
        HT[i].rchild=*s2;
        HT[i].weight=HT[*s1].weight+HT[*s2].weight;
    }
    HC=(huffmancode)malloc((n+1)*sizeof(char *));
    cd=(char *)malloc(n*sizeof(char));
    cd[n-1]='\0';
    for(i=1;i<=n;++i)
    {
        start=n-2;
        for(c=i,f=HT[i].parent;f!=0;c=f,f=HT[f].parent)
        {
            if(HT[f].lchild==c)
            {
                cd[start]='0';
                start--;
            }
            else
            {
                cd[start]='1';
                start--;
            }
            

        }
        HC[i]=(char *)malloc((n-start)*sizeof(char));
        strcpy(HC[i],&cd[++start]);
        printf("i=%d\n",i);
        printf("i=%d\n",i);
    }
    free(cd);
    for(i=8;i>0;i--)
    {
        printf("%d的编码是:\n",HT[i]);
        printf("%s\n",HC[i]);
    }
    system("pause");

}
2013-11-09 21:36
快速回复:赫夫曼树编码的实现
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017685 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved