已阅读5页,还剩1页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
首先是一个简单的排序import java.util.*;public class Heap ArrayList list = new ArrayList();public Heap()public Heap(E objects)for(int i = 0;i 0)if(list.get(currentIndex).compareTo(list.get(currentIndex - 1) 0)E temp = list.get(currentIndex);list.set(currentIndex, list.get(currentIndex - 1);list.set(currentIndex - 1, temp);else break;currentIndex -;public E remove()/删除并返回最小的一个数据if(list.size() = 0)return null;E returnObject = list.get(0);for(int i = 0; i list.size() - 1; i +)list.set(i,list.get(i + 1);list.remove(list.size() - 1);return returnObject;int getSize()return list.size();主要的程序import java.util.*;public class HuffmanCode Scanner input = new Scanner(System.in);int counts = new int256;/各个字符的权重String transferMsg = new String();/编码后的字符串String decipherMsg = new String();/解码后的字符串String codes;public HuffmanCode()/System.out.println(选择何种权重值?n.); useDefaultHuffmanTree();Tree tree = getHuffmanTree(counts);codes = getCodes(tree.root);System.out.print(输出从a开始的字符的各个编码:n);for(int i = 0;i codes.length;i +)if(countsi != 0)System.out.print(codesi + );if(i % 5 = 0)System.out.print(n);System.out.print(n);System.out.println(请输入要编码的字符串);String text = input.nextLine();/待编码的字符串System.out.print(输出输入字符串的huffman编码n);for(int i = 0;i text.length();i +)System.out.print(codes(int)text.charAt(i);transferMsg += codes(int)text.charAt(i);decipherTMsg();System.out.print(n解码后的文本: + decipherMsg);private void decipherTMsg() /解码/ TODO Auto-generated method stubint j = 0;for(int i = 0;i transferMsg.length();i += j)for(j = 1;j 26;j +)if(decipher(transferMsg.substring(i,i+j)break;private boolean decipher(String substring) / TODO Auto-generated method stubfor(int i = 0;i 256;i +)if(substring.equals(codesi)decipherMsg += (char)i;return true;return false;private void useDefaultHuffmanTree()/各个字符的权重countsa = 1859;countsb = 642;countsc = 218;countsd = 317;countse = 1031;countsf = 208;countsg = 152;countsh = 467;countsi = 575;countsj = 8;countsk = 49;countsl = 321;countsm = 198;countsn = 574;countso = 632;countsp = 152;countsq = 8;countsr = 484;countss = 514;countst = 796;countsu = 228;countsv = 83;countsw = 175;countsx = 13;countsy = 164;countsz = 5;private String getCodes(Tree.Node root) /从huffman树获得各个字符的编码/ TODO Auto-generated method stubif(root = null)return null;String codes = new String256;assignCode(root,codes);return codes;private void assignCode(Tree.Node root, String codes) /为各个字符编码/ TODO Auto-generated method stubif(root.left != null)root.left.code = root.code + 0;assignCode(root.left,codes);root.right.code = root.code + 1;assignCode(root.right,codes);elsecodes(int)root.element = root.code;private Tree getHuffmanTree(int counts) /构造霍夫曼树/ TODO Auto-generated method stubHeap heap = new Heap();for(int i = 0;i 1)Tree t1 = heap.remove();Tree t2 = heap.remove();heap.add(new Tree(t1,t2);return heap.remove();public static class Tree implements Comparable/树节点Node root;public Tree(Tree t1,Tree t2)/构建中间节点root = new Node();root.left = t1.root;root.right = t2.root;root.weight = t1.root.weight + t2.root.weight;public Tree(int weight,char element)root = new Node(weight,element);Overridepublic int compareTo(Tree o) / TODO Auto-generated method stubif(root.weight o.root.weight)return 1;if(root.weight o.root.weight)return -1;return 0;public class Node/内部类,存储树节点的相关信息char element;int weight;Node left;Node right;String code = ;public Node()public Node(int weight,char element)this.weight = weight;this.element = el
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 可远程控制的医疗器械设备行业经营分析报告
- 虚拟现实看房行业相关项目现状分析及对策
- 微生物除臭剂行业市场机遇分析
- 分销物流行业竞争格局及投资价值分析报告
- 在线心理教育行业相关项目现状分析及对策
- 影视版权交易平台行业市场特点分析
- 《 我国无固定期限劳动合同制度完善研究》
- 2025届浙江省杭州市余杭区部分学校高三一诊考试物理试卷含解析
- 2023年中国石油国际事业有限公司招聘笔试真题
- 2023年江西省航道工程局招聘考试试题及答案
- 2023年中级审计师考试审计理论与实务真题及答案
- 2024-2030年中国全氟己酮行业发展规划与未来投资战略研究研究报告
- 人力资源管理专业中高职衔接(3+2)专业人才培养方案
- 2024年中考道德与法治(湖北卷)真题评析 课件
- 《化学反应工程》课程思政教学案例(一等奖)
- 山东省济南市槐荫区2023-2024学年七年级下学期6月期末语文试题
- 2024年内蒙古包头市中考英语模拟试卷
- QBT 4483-2024《木聚糖酶制剂》
- 妊娠合并风湿免疫性疾病诊疗及管理常规
- 《唐诗五首》专题探究:律诗的起承转合
- 全文学习2024年安全生产治本攻坚三年行动课件
评论
0/150
提交评论