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