| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 861 人关注过本帖
标题:霍夫曼树问题,求解答
取消只看楼主 加入收藏
Insistanttea
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2013-10-22
结帖率:0
收藏
已结贴  问题点数:20 回复次数:1 
霍夫曼树问题,求解答
#include "stdio.h"
#include "stdlib.h"



typedef struct Node
{   char data;
    int w;
    struct Node *lchild,*rchild,*parent;
}Node;

int n;
Node *p,*q;

int a[1000],top,base=0;

void init()
{top=base=0;
}

int empty( )
{if (top==base)
  return 1;
 else
  return 0;
}

int full()
{ if(top==1000)
    return 1;
  else
   return 0;
 }

 void push(int x)
 { if(!full())
     {a[top]=x;
      top++;
     }
 }

 void pop()
 { if(!empty())
        top--;
 }

 int get()
 { if(!empty())
    return a[top-1];
   else
    return -1;
 }


void input()  //输入N个结点
{int i;
 
 printf("请输入需要编码的结点的个数:");
 scanf("%d",&n);
 fflush(stdin);
 p=(Node*)malloc((2*n)*sizeof(Node)); //p+0不要


for(i=1;i<=n;i++)//构建待编码的N个零散结点
{ printf("请输入第%d个需要编码的结点的字符和权重,以逗号隔开:",i);
    scanf("%c,%d",&(p+i)->data,&(p+i)->w);
    fflush(stdin);
  (p+i)->rchild=(p+i)->lchild=(p+i)->parent=NULL;
  
}           

 for(i=n+1;i<=n*2-1;i++)//构建树需要补充的N-1个结点
 {   
      (p+i)->data=' ';
      (p+i)->w=1000;
      (p+i)->rchild=(p+i)->lchild=(p+i)->parent=NULL;
 }   

}

int minw()//求最小的权的结点
{
    int i,f=0;
    int m=10000;
 
    for(i=1;i<=n*2-1;i++)
 {
     if((p+i)->parent==NULL && (p+i)->w<m)
         
     { m=(p+i)->w;
       f=i;   
     }
 }
 
return f;
}

void createtree()  //创建赫夫曼树
{int k,f1=0,f2=0,j;
 k=1;
 while(k<=n-1)
 {  j=n+k;
    f1=minw();
   (p+f1)->parent=(p+j);
  f2=minw();
  (p+f2)->parent=(p+j);
 
  (p+j)->w=(p+f1)->w+(p+f2)->w;
  (p+j)->lchild=(p+f1);
  (p+j)->rchild=(p+f2);
  
    k++;
  }
}



void output()  //输出编码
{
    int i;
   
   
    for(i=1;i<=n;i++)
    {  
        printf("%c %d:",(p+i)->data,(p+i)->w);
        q=(p+i);

        while(q->parent!=NULL)
        {
            if (q->parent->lchild==q)
                push(0);
            else
                push(1);
            q=q->parent;
        }


        while(!empty())
        {
            printf("%d",get());
            pop();
        }
        printf("\n");
    }
}

main()
{init();
 input();
createtree();
output();

请问这段代码是什么意思:
int a[1000],top,base=0;

void init()
{top=base=0;
}

int empty( )
{if (top==base)
  return 1;
 else
  return 0;
}
还有这句: (p+i)->data=' '中的空格什么意思?;


int minw()//求最小的权的结点
{
    int i,f=0;
    int m=10000;
 
    for(i=1;i<=n*2-1;i++)
 {
     if((p+i)->parent==NULL && (p+i)->w<m)
         
     { m=(p+i)->w;
       f=i;   
     }这没看懂,m为什么要设为10000?拜托大神帮我解答下吧,谢谢
搜索更多相关主题的帖子: include return parent 霍夫曼 
2014-04-22 22:12
Insistanttea
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2013-10-22
收藏
得分:0 
回复 2 楼 鸥翔鱼游
帮帮我吧,大师
2014-05-19 22:09
快速回复:霍夫曼树问题,求解答
数据加载中...
 
   



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

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