| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 691 人关注过本帖
标题:[求助] hfmtree的编码和译码
只看楼主 加入收藏
levononice
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2006-3-21
收藏
 问题点数:0 回复次数:0 
[求助] hfmtree的编码和译码

#include <iostream.h>
#include <iomanip.h>
const int n=4;
const int m=2*n-1;
struct tree
{
float weight;
int parent;
int lch,rch;
};
struct codetype
{
int bits[n+1];
int start;
char ch;

};
tree hftree[m+1];
codetype code[n+1];

void creathuffmantree()
{
int i,j,p1,p2;
float s1,s2;
for(i=1;i<=m;i++)
{
hftree[i].parent=0;
hftree[i].lch=0;
hftree[i].rch=0;
hftree[i].weight=0;
}
cout<<"请输入"<<n<<"个权值"<<endl;
for(i=1;i<n;i++)
cin>>hftree[i].weight;
for(i=n+1;i<=m;i++)
p1=p2=0;
s1=s2=32767;
for(j=1;j<=i-1;j++)
if(hftree[j].parent==0)
if(hftree[j].weight<s1)
{
s2=s1;s1=hftree[j].weight;
p2=p1;p1=j;
}
else
if(hftree[j].weight<s2)
{s2=hftree[j].weight;p2=j;}


//以下为合并

hftree[p1].parent=i;
hftree[p2].parent=i;
hftree[i].lch=p1;
hftree[i].rch=p2;
hftree[i].weight=hftree[p1].weight+hftree[p2].weight;


}

void huffcode ()
{
codetype cd;
int c,p;
for (int i=1;i<=n;i++)
{
cd.start=n+1;
cd.ch=96+i;
c=i;
p=hftree[i].parent;
while(p=0)
{
cd.start--;
if(hftree[p].lch==c)
cd.bits[cd.start]=0;
else
cd.bits[cd.start]=1;
c=p;
p=hftree[p].parent;
}
code [i]=cd;
}

for(i=1;i<=n;i++)
{
cout<<"字符"<<code[i].ch<<"权值为:"<<hftree[i].weight<<setw(15)<<"编码为:";
for( int j=code[i].start;j<=n;j++)
cout<<code[i].bits[j]<<" ";
cout<<endl;

}
}

void trancode()
{
int i=m;char b;
cout<<"请输入一串二近制编码(0,1以外的数结束)";
cin>>b;
while ((b=='0')||(b=='1'))
{
if(b=='0')
i=hftree[i].lch;
else
i=hftree[i].rch;
if (hftree[i].rch==0)
{
cout<<code[i].ch;
i=m;

}
cin>>b;
}
}

void main()
{
creathuffmantree();
huffcode();
trancode();
}
不知道为什么不能显示编码?

搜索更多相关主题的帖子: 译码 hfmtree 编码 
2006-06-09 00:50
快速回复:[求助] hfmtree的编码和译码
数据加载中...
 
   



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

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