| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 478 人关注过本帖
标题:求解关于哈夫曼树的问题
只看楼主 加入收藏
dolly1822
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2010-11-28
结帖率:0
收藏
已结贴  问题点数:20 回复次数:1 
求解关于哈夫曼树的问题
#define MAXVALUE 32767
#define MAXLEAF 8
#define MAXNODE MAXLEAF*2-1
#include <stdio.h>
typedef struct hnode
{
  int weight;
  int lchild,rchild;
  int parent;
}HTNode;

typedef struct Code
{
  int bits[MAXLEAF];
  int start;
  char ch;
}HCodeType;

void Huffman_tree(HTNode Ht[MAXNODE])
{
  int i,j,m1,m2,p1,p2;
  for(i=0;i<MAXNODE;i++)
  {
    Ht[i].parent=-1;
    Ht[i].lchild=-1;
    Ht[i].rchild=-1;
  }
  for(i=MAXLEAF;i<MAXNODE;i++)
  {
    m1=m2= MAXVALUE;
    p1=p2=0;
    for(j=0;j<i-1;j++)
    {
      if(Ht[j].weight<m1&&Ht[j].weight==-1)
      {
        m2=m1;
        p2=p1;
        m1=Ht[j].weight;
        p2=j;
      }
      else if(Ht[j].weight<m2&&Ht[j].weight==-1)
      {
        m2=Ht[j].weight;
        p2=j;
      }
    }
    Ht[p1].parent=i;
    Ht[p2].parent=i;
    Ht[i].lchild=p1;
    Ht[i].rchild=p2;
    Ht[i].weight=Ht[p1].weight+Ht[p2].weight;
  }
}

void Huffman_code(HTNode Ht[MAXNODE],HCodeType HuffCode[MAXLEAF])
{
  HCodeType cd;
  int c,p,i,j;
  for(i=0;i<MAXLEAF;i++)
  {
    cd.start=MAXLEAF;
    c=i;
    p=Ht[i].parent;
    while(p!=-1)
    {
      cd.start--;
      if(Ht[p].lchild==c)
    cd.bits[cd.start]='0';
      else cd.bits[cd.start]='1';
      c=p;
      p=Ht[p].parent;
    }
    cd.start++;
    for(j=cd.start;j<=MAXLEAF;j++)
        HuffCode[i].bits[j]=cd.bits[j];
    HuffCode[i].start=cd.start;
  }
}

void display(HCodeType HuffCode[MAXLEAF])
{
    int i, k;
    for(i=0;i<MAXLEAF;i++)
    {
        printf("%c:\t",HuffCode[i].ch);
        for(k=HuffCode[i].start;k<=MAXLEAF;k++)
            printf("%c",HuffCode[i].bits[k]);
        printf("\n");
    }
}

void main()
{
  HTNode Ht[MAXNODE];
  HCodeType HuffCode[MAXLEAF];
  int i,j;
  printf("please input 8 leafs' weight:\n");
  for(i=0;i<MAXLEAF;i++)
    scanf("%d",&Ht[i].weight);
  printf("qing shu ru zi fu:\n");
  for(j=0;j<MAXLEAF;j++)
    scanf("%c",&HuffCode[j].ch);
  Huffman_tree(Ht);
  Huffman_code(Ht,HuffCode);
  printf("please output huffman bian ma:\n");
  display(HuffCode);
}



大家帮我看看呀,为什么没有没有输出啊。。。
搜索更多相关主题的帖子: 哈夫曼 求解 
2010-12-12 14:38
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:20 
程序代码:
#define MAXVALUE 32767
#define MAXLEAF 8
#define MAXNODE MAXLEAF*2-1
#include <stdio.h>

typedef struct hnode
{
    int weight;
    int lchild,rchild;
    int parent;
}HTNode;

typedef struct Code
{
    int bits[MAXLEAF];
    int start;
    char ch;
}HCodeType;

void Huffman_tree(HTNode Ht[MAXNODE])
{
    int i,j,m1,m2,p1,p2;
    for(i=0;i<MAXNODE;i++)
    {
        Ht[i].parent=-1;
        Ht[i].lchild=-1;
        Ht[i].rchild=-1;
    }
    for(i=MAXLEAF; i<MAXNODE; i++)
    {
        m1 = m2 = MAXVALUE;
        p1 = p2 = 0;
        for(j=0;j<i;j++)
        {
            //if(Ht[j].weight<m1&&Ht[j].weight==-1)
            if( Ht[j].weight<m1 && Ht[j].parent==-1 )
            {
                m2 = m1;
                p2=p1;
                m1 = Ht[j].weight;
                p1=j;
            }
            //else if(Ht[j].weight<m2&&Ht[j].weight==-1)
            else if( Ht[j].weight<m2 && Ht[j].parent==-1 )
            {
                m2 = Ht[j].weight;
                p2 = j;
            }
        }
        Ht[p1].parent=i;
        Ht[p2].parent=i;
        Ht[i].lchild=p1;
        Ht[i].rchild=p2;
        Ht[i].weight = Ht[p1].weight + Ht[p2].weight;
    }
}

void Huffman_code(HTNode Ht[MAXNODE],HCodeType HuffCode[MAXLEAF])
{
    HCodeType cd;
    int c,p,i,j;
    for(i=0;i<MAXLEAF;i++)
    {
        cd.start=MAXLEAF;
        c=i;
        p=Ht[i].parent;
        while(p!=-1)
        {
            cd.start--;
            if(Ht[p].lchild==c)
                cd.bits[cd.start]=0;
            else
                cd.bits[cd.start]=1;
            c = p;
            p=Ht[p].parent;
        }
        //cd.start++;
        for(j=cd.start; j<MAXLEAF; j++)
            HuffCode[i].bits[j]=cd.bits[j];
        HuffCode[i].start=cd.start;
    }
}

void display(HCodeType HuffCode[MAXLEAF])
{
    int i, k;
    for(i=0;i<MAXLEAF;i++)
    {
        printf("%c:\t",HuffCode[i].ch);
        for(k=HuffCode[i].start;k<MAXLEAF;k++)
            printf("%d",HuffCode[i].bits[k]);
        printf("\n");
    }
}

void main()
{
    HTNode Ht[MAXNODE];
    HCodeType HuffCode[MAXLEAF];
    int i,j;
    printf("please input 8 leafs' weight:\n");
    for(i=0;i<MAXLEAF;i++)
        scanf("%d",&Ht[i].weight);
    getchar();
    printf("qing shu ru zi fu:\n");
    for(j=0;j<MAXLEAF;j++)
        scanf("%c ",&HuffCode[j].ch);
   
    Huffman_tree(Ht);
    Huffman_code(Ht,HuffCode);
    printf("please output huffman bian ma:\n");
    display(HuffCode);
}

/*

大家帮我看看呀,为什么没有没有输出啊。。。
*/
图片附件: 游客没有浏览图片的权限,请 登录注册
2010-12-18 11:47
快速回复:求解关于哈夫曼树的问题
数据加载中...
 
   



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

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