欢迎来到天天文库
浏览记录
ID:55750978
大小:276.50 KB
页数:11页
时间:2020-03-01
《数据结构实验三哈夫曼树实验报告.doc》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、题目:哈夫曼编/译码器一、题目要求:写一个哈夫曼码的编/译码系统,要求能对要传输的报文进行编码和解码。构造哈夫曼树时,权值小的放左子树,权值大的放右子树,编码时右子树编码为1,左子树编码为0.二、概要设计:数据结构:typedefstruct{intbit[MAXBIT];intstart;}HCodeType;/*编码结构体*/typedefstruct{intweight;intparent;intlchild;intrchild;charvalue;}HNode;/*结点结构体*/函数:voidDEM
2、ONHuffmanTree(HNodeHuffNode[MAXNODE],intn)作用:构造一个哈夫曼树,并循环构建intmain()作用:运用已经构建好的哈弗曼树,进行节点的处理,达到成功解码编译三、详细设计:哈夫曼树的建立:voidDEMONHuffmanTree(HNodeHuffNode[MAXNODE],intn){inti=0,j,m1,m2,x1,x2;charx;/*初始化存放哈夫曼树数组HuffNode[]中的结点*/while(i3、值Word文档HuffNode[i].parent=-1;HuffNode[i].lchild=-1;HuffNode[i].rchild=-1;scanf("%c",&x);scanf("%c",&HuffNode[i].value);//实际值,可根据情况替换为字母i++;}/*输入n个叶子结点的权值*/scanf("%c",&x);for(i=0;i4、ght=0;//权值HuffNode[i].parent=-1;HuffNode[i].lchild=-1;HuffNode[i].rchild=-1;HuffNode[i].value=i;}/*循环构造Huffman树*/for(i=0;i5、1&&HuffNode[j].parent==-1){m2=m1;//m1中是最小x2=x1;m1=HuffNode[j].weight;x1=j;}elseif(HuffNode[j].weight6、n+i].weight=HuffNode[x1].weight+HuffNode[x2].weight;HuffNode[n+i].lchild=x1;HuffNode[n+i].rchild=x2;}}叶子节点的哈夫曼编码的保存:for(j=cd.start+1;j7、XLEAF],cd;inti,j,c,p,n,k=0;charwen[100];charz;scanf("%d",&n);HuffmanTree(HuffNode,n);for(i=0;i8、=HuffNode[c].parent;/*设置下一循环条件*/}/*endwhile*/Word文档for(j=cd.start+1;j
3、值Word文档HuffNode[i].parent=-1;HuffNode[i].lchild=-1;HuffNode[i].rchild=-1;scanf("%c",&x);scanf("%c",&HuffNode[i].value);//实际值,可根据情况替换为字母i++;}/*输入n个叶子结点的权值*/scanf("%c",&x);for(i=0;i4、ght=0;//权值HuffNode[i].parent=-1;HuffNode[i].lchild=-1;HuffNode[i].rchild=-1;HuffNode[i].value=i;}/*循环构造Huffman树*/for(i=0;i5、1&&HuffNode[j].parent==-1){m2=m1;//m1中是最小x2=x1;m1=HuffNode[j].weight;x1=j;}elseif(HuffNode[j].weight6、n+i].weight=HuffNode[x1].weight+HuffNode[x2].weight;HuffNode[n+i].lchild=x1;HuffNode[n+i].rchild=x2;}}叶子节点的哈夫曼编码的保存:for(j=cd.start+1;j7、XLEAF],cd;inti,j,c,p,n,k=0;charwen[100];charz;scanf("%d",&n);HuffmanTree(HuffNode,n);for(i=0;i8、=HuffNode[c].parent;/*设置下一循环条件*/}/*endwhile*/Word文档for(j=cd.start+1;j
4、ght=0;//权值HuffNode[i].parent=-1;HuffNode[i].lchild=-1;HuffNode[i].rchild=-1;HuffNode[i].value=i;}/*循环构造Huffman树*/for(i=0;i5、1&&HuffNode[j].parent==-1){m2=m1;//m1中是最小x2=x1;m1=HuffNode[j].weight;x1=j;}elseif(HuffNode[j].weight6、n+i].weight=HuffNode[x1].weight+HuffNode[x2].weight;HuffNode[n+i].lchild=x1;HuffNode[n+i].rchild=x2;}}叶子节点的哈夫曼编码的保存:for(j=cd.start+1;j7、XLEAF],cd;inti,j,c,p,n,k=0;charwen[100];charz;scanf("%d",&n);HuffmanTree(HuffNode,n);for(i=0;i8、=HuffNode[c].parent;/*设置下一循环条件*/}/*endwhile*/Word文档for(j=cd.start+1;j
5、1&&HuffNode[j].parent==-1){m2=m1;//m1中是最小x2=x1;m1=HuffNode[j].weight;x1=j;}elseif(HuffNode[j].weight6、n+i].weight=HuffNode[x1].weight+HuffNode[x2].weight;HuffNode[n+i].lchild=x1;HuffNode[n+i].rchild=x2;}}叶子节点的哈夫曼编码的保存:for(j=cd.start+1;j7、XLEAF],cd;inti,j,c,p,n,k=0;charwen[100];charz;scanf("%d",&n);HuffmanTree(HuffNode,n);for(i=0;i8、=HuffNode[c].parent;/*设置下一循环条件*/}/*endwhile*/Word文档for(j=cd.start+1;j
6、n+i].weight=HuffNode[x1].weight+HuffNode[x2].weight;HuffNode[n+i].lchild=x1;HuffNode[n+i].rchild=x2;}}叶子节点的哈夫曼编码的保存:for(j=cd.start+1;j7、XLEAF],cd;inti,j,c,p,n,k=0;charwen[100];charz;scanf("%d",&n);HuffmanTree(HuffNode,n);for(i=0;i8、=HuffNode[c].parent;/*设置下一循环条件*/}/*endwhile*/Word文档for(j=cd.start+1;j
7、XLEAF],cd;inti,j,c,p,n,k=0;charwen[100];charz;scanf("%d",&n);HuffmanTree(HuffNode,n);for(i=0;i8、=HuffNode[c].parent;/*设置下一循环条件*/}/*endwhile*/Word文档for(j=cd.start+1;j
8、=HuffNode[c].parent;/*设置下一循环条件*/}/*endwhile*/Word文档for(j=cd.start+1;j
此文档下载收益归作者所有