云题海 - 专业文章范例文档资料分享平台

当前位置:首页 > 数据结构实习报告12345

数据结构实习报告12345

  • 62 次阅读
  • 3 次下载
  • 2025/6/4 13:48:46

5.2 功能模块图

5.3 程序:

#include \#include #include #include #include #include

typedef struct{ //赫夫曼树的结构体 char ch; int weight; //权值 int parent,lchild,rchild; }htnode,*hfmtree;

typedef char **hfmcode;

void Select(hfmtree &HT,int a,int *p1,int *p2) //Select函数,选出HT树到a为止,权值最小且parent为0的2个节点 { int i,j,x,y; for(j=1;j<=a;++j){ if(HT[j].parent==0){ x=j; break; } } for(i=j+1;i<=a;++i){ if(HT[i].weight

24

} } for(i=j+1;i<=a;++i) { if(HT[i].weighty){ *p1=y; *p2=x; } else { *p1=x; *p2=y; } }

void hfmcoding(hfmtree &HT,hfmcode &HC,int n) //构建赫夫曼树HT,并求出n个字符的赫夫曼编码HC { int i,start,c,f,m,w; int p1,p2; char *cd,z; if(n<=1){ return; } m=2*n-1; HT=(hfmtree)malloc((m+1)*sizeof(htnode));

for(i=1;i<=n;++i) //初始化n个叶子结点 { }

printf(\请输入第%d字符信息和权值:\scanf(\while(getchar()!='\\n') { continue; }

HT[i].ch=z;

HT[i].weight=w; HT[i].parent=0; HT[i].lchild=0; HT[i].rchild=0;

25

for(;i<=m;++i) //初始化其余的结点 { HT[i].ch='0'; HT[i].weight=0; HT[i].parent=0; HT[i].lchild=0; HT[i].rchild=0; }

for(i=n+1;i<=m;++i) //建立赫夫曼树 { Select(HT,i-1,&p1,&p2); 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; } HC=(hfmcode)malloc((n+1)*sizeof(char *)); cd=(char *)malloc(n*sizeof(char)); cd[n-1]='\\0'; for(i=1;i<=n;++i) //给n个字符编码 { start=n-1; for(c=i,f=HT[i].parent;f!=0;c=f,f=HT[f].parent) { if(HT[f].lchild==c) { cd[--start]='0'; } else { cd[--start]='1'; } } HC[i]=(char*)malloc((n-start)*sizeof(char)); strcpy(HC[i],&cd[start]); } free(cd); }

int main(){

26

char code[100],h[100],hl[100]; int n,i,j,k,l;

ifstream input_file; //文件输入输出流 ofstream output_file; char choice,str[100]; hfmtree HT; hfmcode HC; cout<<\

cout<<\ \ \ \ \ \ \\n\

while(choice!='Q'&&choice!='q') //当choice的值不为q且不为Q时循环 { cout<<\ \赫夫曼编码/译码器*************************\\n\ cout<<\ \ \ \ \

cout<<\请输入您要操作的步骤:\ cin>>choice;

if(choice=='I'||choice=='i') //初始化赫夫曼树 {

cout<<\请输入字符个数:\ cin>>n; hfmcoding(HT,HC,n); for(i=1;i<=n;++i) { cout<

27

搜索更多关于: 数据结构实习报告12345 的文档
  • 收藏
  • 违规举报
  • 版权认领
下载文档10.00 元 加入VIP免费下载
推荐下载
本文作者:...

共分享92篇相关文档

文档简介:

5.2 功能模块图 5.3 程序: #include \#include #include #include #include #include typedef struct{ //赫夫曼树的结构体 char ch; int weight; //权值 int parent,lchild,rchild; }htnode,*hfmtree; typedef char **hfmcode; void Select(hfmtree &HT,int a,int *p1,int *p2) //Select函数,选出HT树到a为止,权值最小且parent为0的2

× 游客快捷下载通道(下载后可以自由复制和排版)
单篇付费下载
限时特价:10 元/份 原价:20元
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219
Copyright © 云题海 All Rights Reserved. 苏ICP备16052595号-3 网站地图 客服QQ:370150219 邮箱:370150219@qq.com