欢迎来到天天文库
浏览记录
ID:41534568
大小:59.20 KB
页数:5页
时间:2019-08-27
《信息111,李继华,110111122哈夫曼编码算法》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、实验题目:哈夫曼编码班级:信息111
2、姓名:李继华
3、学号:110111122口期:2014-4-8一、实验目的掌握哈夫曼编码算法,理解贪心算法思想。二、实验要求用自己熟悉的高级程序设计语言编写程序实现哈夫曼编码算法。在编写程序时,至少要设计两个类:第一个是最小堆类。(详见附件)第二个是哈夫曼类。(详见附件)最后编写main函数,用于读入数据。三源程序(带注释)packagecn.jihuali.course;importjava•util.ArrayList;importjava•util•Iterator;importjava•
4、util•List;importjava•util•Scanner;publicclassHuffmanTreeDemo{publicstaticvoidmain(String[]args){while(true){Scannersc=newScanner(System.in);System,out.printIn("请输入哈夫曼码屮字符的总数:”);//了符个数intnums=sc•nextint();char[]arrayc=newchar[nums];double[]weight=newdouble[nums];for(in
5、ti=0;i6、("哈弗曼编码为:”);t4•PrintHuffmanlncoding();}}}classTool4{double[]w;//用来存储每个字符的权重char[]arrayc;//用来存储编码屮所有的字符intlength;//字符的总长度intposition;//存储临时的最小权重节点的位置TreeNodehead;ListInCode=newArrayList();ListIs=newArrayList();//存彳诸树hj节点p7、ublicTool4(){}publicToo14(double[]a,char[]c){this.w=a;this.arrayc=c;length=a.length;}privatevoidmin(){//求出最小权值的卜标doubleresult=1;Iteratorit=Is.iterator();inti=0;while(it.hasNext()){TreeNodetemp=it.next();if(result>temp.weight){result=temp.weight;position=i;}i8、++;}}publicvoidinit(){for(inti=0;i9、newTreeNode();temp3.weight=templ.weight+temp2.weight;temp3.lchild=tempi;temp3.rchild=temp2;Is•add(temp3);}publicvoidCreateHuffmanTree(){//仓U造哈夫曼码树while(Is.size()>1)CreateHu£fmanNode();head=Is•get(0);}publicvoidGetHuffmanlncoding(TreeNodet,StringIncoding){Stringtemplnc10、oding=Incoding;if(t・lchild!=null){templncoding+=0n;GetHuffmanlncoding(t•lchild,templncoding);}if(t.rchild!=null){templncodin
6、("哈弗曼编码为:”);t4•PrintHuffmanlncoding();}}}classTool4{double[]w;//用来存储每个字符的权重char[]arrayc;//用来存储编码屮所有的字符intlength;//字符的总长度intposition;//存储临时的最小权重节点的位置TreeNodehead;ListInCode=newArrayList();ListIs=newArrayList();//存彳诸树hj节点p
7、ublicTool4(){}publicToo14(double[]a,char[]c){this.w=a;this.arrayc=c;length=a.length;}privatevoidmin(){//求出最小权值的卜标doubleresult=1;Iteratorit=Is.iterator();inti=0;while(it.hasNext()){TreeNodetemp=it.next();if(result>temp.weight){result=temp.weight;position=i;}i
8、++;}}publicvoidinit(){for(inti=0;i9、newTreeNode();temp3.weight=templ.weight+temp2.weight;temp3.lchild=tempi;temp3.rchild=temp2;Is•add(temp3);}publicvoidCreateHuffmanTree(){//仓U造哈夫曼码树while(Is.size()>1)CreateHu£fmanNode();head=Is•get(0);}publicvoidGetHuffmanlncoding(TreeNodet,StringIncoding){Stringtemplnc10、oding=Incoding;if(t・lchild!=null){templncoding+=0n;GetHuffmanlncoding(t•lchild,templncoding);}if(t.rchild!=null){templncodin
9、newTreeNode();temp3.weight=templ.weight+temp2.weight;temp3.lchild=tempi;temp3.rchild=temp2;Is•add(temp3);}publicvoidCreateHuffmanTree(){//仓U造哈夫曼码树while(Is.size()>1)CreateHu£fmanNode();head=Is•get(0);}publicvoidGetHuffmanlncoding(TreeNodet,StringIncoding){Stringtemplnc
10、oding=Incoding;if(t・lchild!=null){templncoding+=0n;GetHuffmanlncoding(t•lchild,templncoding);}if(t.rchild!=null){templncodin
此文档下载收益归作者所有