




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、哈夫曼树实现统一编码中国石油大学计算机系暑期实习内容JAVA平台,jdk版本1.8,工具Eclispse。实现了输入字符串建立对应的哈夫曼树并进行编码,并能够动态地画出哈夫曼树的建立过程,有socket能够使两台电脑之间能够通信。建立两个包,分别建立客户端和服务器端。第一个包中有12个类 源代码如下一、主函数public class main public static void main(String args) / TODO Auto-generated method stubWindowActionEvent win = new WindowActionEvent();PoliceLis
2、ten police = new PoliceListen();win.setMyCommandListener(police);二、建立哈夫曼树import java.awt.Graphics;import java.util.*;import javax.swing.JFrame;import fb.HuffmanTree.Node;public class HuffmanTree public static class Code<E>String d;String c;Code(String d1,String c1)d = d1;c = c1;public static c
3、lass Node<E>int flag = 0;int x = 0;int y = 0;int w = 1;String data;double weight;Node leftChild;Node rightChild;Node parent;Node(String d,double w)int x = 0;int y = 0;int flag = 0;data = d;weight = w;leftChild = null;rightChild = null;parent = null;Node()public static int xweight;Node root;pub
4、lic static ArrayList<Node> list= new ArrayList<Node>();public static List<Code> list1 = new ArrayList<Code>();public static ArrayList<Node> nodes = new ArrayList<Node>();public HuffmanTree(pinShu p1)int a = new int10;for(int f = 0;f < 10;f +)af = 0;a0 = 2;for(i
5、nt i = 1;i < 10;i +)for(int j = i-1;j >= 0;j -)ai += aj;ai+;/System.out.println(ai); /System.out.println(high+" "+xweight+" "+yweight);for(int i = 0;i < p1.n;i +)nodes.add(new Node(p1.list.get(i),p1.numi);while(nodes.size()>1) sort(nodes); Node left = nodes.get(nodes.
6、size()-1); Node right = nodes.get(nodes.size()-2); Node parent1 = new Node(left.data+right.data,left.weight+right.weight); left.parent = parent1 ; right.parent = parent1 ; parent1 .leftChild = left; parent1 .rightChild = right; nodes.remove(nodes.size()-1); nodes.remove(nodes.size()-1); nodes.add(pa
7、rent1);root= nodes.get(0);int high = this.getHigh(root);/int high = (int)Math.ceil(Math.log(double)p1.list.size()+1)/(double)Math.log(2)+1; xweight =600/(int)(Math.pow(2, high-1)*3-1); int yweight = 400/high; Queue<Node> queue = new ArrayDeque<Node>(); if( root != null) queue.offer(root)
8、; root.x = 400; root.y = 100; while(!queue.isEmpty() list.add(queue.peek(); / System.out.println(queue.peek().data+queue.peek().weight); Node p = queue.poll(); if(p.leftChild != null) p.leftChild.w = p.w+1; p.leftChild.x = p.x-xweight*ahigh-p.leftChild.w; /p.leftChild.x = p.x-100/p.leftChild.w; p.le
9、ftChild.y = p.y + yweight; /p.leftChild.y = p.y +60; queue.offer(p.leftChild); if(p.rightChild != null) p.rightChild.w = p.w +1; p.rightChild.x = p.x+xweight*ahigh - p.rightChild.w; p.rightChild.y = p.y + yweight; /p.rightChild.x = p.x+100/p.rightChild.w+30; /p.rightChild.y = p.y +60; queue.offer(p.
10、rightChild); for(int i = 0;i < list.size();i +) /System.out.println(list.get(i).data+list.get(i).weight); String s; if(list.get(i).leftChild=null&&list.get(i).rightChild=null) Node temp = list.get(i).parent; Node temp1 = list.get(i); if(temp.leftChild.data.equals(temp1.data) s="0&quo
11、t; else s="1" temp = temp.parent; temp1 = temp1.parent; while(temp1!=root) if(temp.leftChild.data.equals(temp1.data) s+="0" else if(temp.rightChild.data.equals(temp1.data) s+="1" else s+="" temp = temp.parent; temp1 = temp1.parent; StringBuffer sb = new String
12、Buffer(); for(int q = 0;q < s.length();q+) sb.append(s.charAt(s.length()-(q+1); list1.add(new Code(list.get(i).data,sb.toString(); public int getHigh(Node subtree)if(subtree=null) return 0;else if(getHigh(subtree.leftChild)>getHigh(subtree.rightChild)return 1+ getHigh(subtree.leftChild);else r
13、eturn 1+ getHigh(subtree.rightChild);public void sort(ArrayList<Node> nodes )for(int i = 0;i < nodes.size()-1;i +)for(int j = i+1;j < nodes.size();j +)if(nodes.get(i).weight<nodes.get(j).weight)Node temp = nodes.get(i);nodes.set(i, nodes.get(j);nodes.set(j,temp); public String print1(
14、) String s = "各编码为"+'n' for(int i = 0;i < list1.size();i +) s +=list1.get(i).d+":"+list1.get(i).c +'n' /System.out.println(list1.get(i).c+" "+list1.get(i).d); return s; public static String trans(String str) int j; String s = "" for(int i =
15、0;i < str.length();i +) for(j = 0;j < list1.size();j +) char te = str.charAt(i); String temp = String.valueOf(te); if(temp.equals(list1.get(j).d) s +=list1.get(j).c; break; if(j=list1.size() char te = str.charAt(i); String temp = String.valueOf(te); s+=" " s+=temp; s+="处开始有错误&qu
16、ot; return s; return s; public static String retrans(String str) String s ="" while(!str.isEmpty() int i; String str1 = str; for( i = 0;i < list1.size();i +) if(str.length()<list1.get(i).c.length() continue; /System.out.println(str.substring(0,list1.get(i).c.length()+" "+li
17、st1.get(i).c); if(str.substring(0,list1.get(i).c.length().equals(list1.get(i).c) s+=list1.get(i).d; /System.out.println(s); str = str.substring(list1.get(i).c.length(); /System.out.println(str); break; if(str.equals(str1) s+=" " s+=str; s+="有错误" break; return s; 三、主界面import java.
18、awt.event.*;import java.awt.*;import javax.swing.*;public class WindowActionEvent extends JFramepublic static JTextField inputText;public static JTextField inputText1;public static JTextField inputText2;public static JTextField inputText3;public static JTextField inputTexti;public static JTextField
19、outputTexto;public static JTextArea textShow;JButton button;JButton button1;JButton button2;JButton button3;JButton button4;JButton button5;MyCommandListener listener;public WindowActionEvent()init();this.setSize(800, 600);setVisible(true);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);void init() s
20、etLayout(new FlowLayout(); add(new JLabel("请输入要编码的字符:"); inputText1 = new JTextField(47); inputText2 = new JTextField(47); inputText2 = new JTextField(47); inputText3 = new JTextField(47); inputText = new JTextField(47); inputTexti = new JTextField(47); outputTexto = new JTextField(47); bu
21、tton = new JButton("确定"); textShow = new JTextArea(10,47); add(inputText); add(button); add(new JLabel("对此字符串 进行处理:"); add(new JScrollPane(textShow);/滚动窗格 button1 = new JButton("作图动态生成哈夫曼树"); add(Box.createHorizontalStrut(1000); /换行 add(button1); add(Box.createHorizonta
22、lStrut(1000); /换行 button1.addActionListener(new print(); add(new JLabel("请输入要转换的编码"); add(inputTexti); add(Box.createHorizontalStrut(1000); /换行 button4 = new JButton("进行哈夫曼编码"); button4.addActionListener(new Transform(); button5 = new JButton("哈夫曼编码转原文"); add(button4);
23、add(button5); button5.addActionListener(new retransform(); add(Box.createHorizontalStrut(1000); /换行 add(new JLabel("转换结果为:"); add(outputTexto); add(Box.createHorizontalStrut(1000); /换行 add(new JLabel("请输入所要连接的IP:"); add(inputText1); add(Box.createHorizontalStrut(1000); /换行 add(ne
24、w JLabel("请输入端口号:"); add(inputText2); add(Box.createHorizontalStrut(1000); /换行 button2 = new JButton("建立联系"); button2.addActionListener(new ClientListen(); add(button2); button3 = new JButton("发送数据"); add(button3); button3.addActionListener(new postListen(); add(Box.cre
25、ateHorizontalStrut(1000); /换行 /add(inputText3); void setMyCommandListener(MyCommandListener listener)this.listener = listener;listener.setJTextField(inputText);listener.setJTextArea(textShow);inputText.addActionListener(listener);button.addActionListener(listener);四、import java.awt.event.*;import ja
26、va.awt.*;import javax.swing.*;public interface MyCommandListener extends ActionListenerpublic void setJTextField(JTextField text);public void setJTextArea(JTextArea area);五、画树算法import java.util.ArrayList;import java.awt.*;import javax.swing.JFrame;import fb.HuffmanTree.Node;public class picture exte
27、nds JFramepublic ArrayList<Node> list= new ArrayList<Node>();int cr = 32;public picture()/String s = WindowActionEvent.inputText.getText();/pinShu pin1 = new pinShu(s); /HuffmanTree huff1 = new HuffmanTree(pin1); list = HuffmanTree.list;this.setBounds(00,100, 800, 600);this.setName("
28、;哈夫曼树");this.getContentPane().setBackground(Color.WHITE);this.setVisible(true); public void sort(ArrayList<Node> nodes )for(int i = 0;i < nodes.size()-1;i +)for(int j = i+1;j < nodes.size();j +)if(nodes.get(i).weight<nodes.get(j).weight)Node temp = nodes.get(i);nodes.set(i, nodes
29、.get(j);nodes.set(j,temp);public void paint(Graphics g) super.paint(g); Font font=new Font("",Font.ITALIC|Font.BOLD,23); g.setFont(font); while(list.size()>1) sort(list); Node temp1 = list.get(list.size()-1); Node temp2 = new Node(); if(temp1.data.equals(temp1.parent.leftChild.data) tem
30、p2 = temp1.parent.rightChild; else temp2 = temp1.parent.leftChild; if(temp1.flag = 0) g.drawOval(temp1.x, temp1.y, cr, cr); if(temp1.leftChild=null&&temp1.rightChild=null) g.drawString(temp1.data,temp1.x+cr/4,temp1.y+cr*2/3); if(temp2.flag=0) g.drawOval(temp2.x, temp2.y, cr, cr); if(temp2.le
31、ftChild=null&&temp2.rightChild=null) g.drawString(temp2.data,temp2.x+cr/4,temp2.y+cr*2/3); temp1.parent.flag=1; int x1 = temp1.x+cr/2,y1 = temp1.y; int xlong = (temp1.parent.x- temp1.x)/10; int ylong = (temp1.parent.y +cr- temp1.y)/10; int x2 = x1+xlong,y2 = y1+ylong; / g.drawLine(temp1.x+cr
32、/2, temp1.y, temp1.parent.x+cr/2,temp1.parent.y+cr); for(int i=0; i<10; i+) g.setColor(Color.BLACK); g.drawLine(x1, y1, x2, y2); tryThread.sleep(100);catch(Exception eex) x2=x2+xlong; y2=y2+ylong; g.setColor(Color.WHITE); g.drawLine(x1, y1, x2, y2); g.setColor(Color.BLACK); g.drawLine(temp1.x+cr/
33、2, temp1.y, temp1.parent.x+cr/2,temp1.parent.y+cr); int x3 = temp2.x+cr/2,y3 = temp2.y; int xlong1 = (temp2.parent.x - temp2.x)/10; int ylong1 = (temp2.parent.y+cr- temp2.y)/10; int x4 = x3+xlong1,y4 = y3+ylong1; /g.drawLine(temp2.x+cr/2, temp2.y, temp2.parent.x+cr/2,temp2.parent.y+cr); for(int i=0;
34、 i<10; i+) g.setColor(Color.BLACK); g.drawLine(x3, y3, x4, y4); tryThread.sleep(100);catch(Exception eex) / x3=x4; /y3=y4; x4=x4+xlong1; y4=y4+ylong1; g.setColor(Color.WHITE); g.drawLine(x3, y3, x4, y4); g.setColor(Color.BLACK); g.drawLine(temp2.x+cr/2, temp2.y, temp2.parent.x+cr/2,temp2.parent.y
35、+cr); g.drawOval(temp1.parent.x, temp1.parent.y, cr, cr); tryThread.sleep(500);catch(Exception eex) list.remove(list.size()-1); for(int i = 0;i < list.size();i +) if(list.get(i).data.equals(temp2.data) list.remove(i); break; 六、监听画树的按钮import java.awt.*;import java.awt.event.*;import java.awt.geom.
36、*;public class print implements ActionListener public void actionPerformed(ActionEvent e) new picture();七、import java.awt.event.*;import java.awt.*;import javax.swing.*;public class PoliceListen implements MyCommandListener JTextField textInput;JTextArea textShow;public String str;public void setJTe
37、xtField(JTextField text)textInput = text; public void setJTextArea(JTextArea area) textShow = area;public void actionPerformed(ActionEvent e)textShow.setText(null);str = textInput.getText();if(str=null) System.exit(0); pinShu pin = new pinShu(str);textShow.append(pin.print();HuffmanTree huff = new H
38、uffmanTree(pin);textShow.append(huff.print1();String s = huff.trans(str);textShow.append("转换后的编码是"+'n');textShow.append(s);huff.retrans(s);textShow.append('n'+"将编码转换回原文"+'n');textShow.append(huff.retrans(s);八、import java.util.ArrayList;public class pinShu
39、ArrayList<String> list = new ArrayList<String>(); int num;int n = 0;int l;pinShu(String str)num = new intstr.length();l = str.length();for(int i = 0; i<str.length();i+) numi = 0;char te = str.charAt(i);String temp = String.valueOf(te);if(n=0)numn+;list.add(temp);else int j;for(j = n -
40、 1;j >=0;j -)if(temp.equals(list.get(j) numj+;break;if(j < 0)numn+; list.add(temp);public String print()String temp = "各字符出现的频率是"+'n'for(int i = 0;i < list.size();i +)String double_str = String.format("%.2f", (double)numi/l);temp +=list.get(i)+":"+doubl
41、e_str+" "temp+='n'return temp;九、import java.awt.event.*; class postListen implements ActionListenerpublic void actionPerformed(ActionEvent e)tryClientListen.out.writeUTF(WindowActionEvent.outputTexto.getText();/System.out.println(WindowActionEvent.inputText.getText();catch(Exceptio
42、n e1)十、import java.awt.event.ActionEvent;import java.awt.event.ActionListener;public class retransform implements ActionListenerpublic void actionPerformed(ActionEvent e)tryString s=HuffmanTree.retrans(WindowActionEvent.inputTexti.getText();WindowActionEvent.outputTexto.setText(s);catch(Exception e3
43、)十一、import java.awt.event.ActionEvent;import java.awt.event.ActionListener;public class Transform implements ActionListenerpublic void actionPerformed(ActionEvent e)tryString s=HuffmanTree.trans(WindowActionEvent.inputTexti.getText();WindowActionEvent.outputTexto.setText(s);catch(Exception e3)十二、建立客
44、户端import java.awt.event.*;import java.awt.event.ActionEvent;import java.io.DataInputStream;import java.io.DataOutputStream;import .InetAddress;import .InetSocketAddress;import .Socket;import java.io.*;import javax.swing.JTextField;public class ClientListen implements ActionListener/JTextField inputT
45、ext1;/JTextField inputText2;/JTextField inputText;/public void set(JTextField text)/inputText= text;/public void set1(JTextField text)/inputText1 = text;/public void set2(JTextField text)/inputText2 = text;/ Socket mysocket = null;public static DataInputStream in ; public static DataOutputStream out
46、 ;public void actionPerformed(ActionEvent e) / Thread readData; / Read read = null; try mysocket = new Socket(); / read = new Read(); / readData = new Thread (read); String IP = WindowActionEvent.inputText1.getText(); InetAddress address = InetAddress.getByName(IP); int port; port = Integer.parseInt(WindowActionEvent.inputText2.getText(); InetSocketAddress socketAddress = new InetSocketAddress("localhost",port); if(mysocket.isConnected() else mysocket.connect(socketAddre
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年心理学概论考试试卷及答案
- 2025年心理学专业硕士研究生入学考试试卷及答案
- 2025年物理学基础知识验收考试题及答案
- 2025年食品安全监督相关考试试题及答案
- Adezmapimod-hydrochloride-Standard-SB-203580-hydrochloride-Standard-生命科学试剂-MCE
- 2025年社会工作者职业资格认证考试试题及答案
- 2025年农学与生态学研究生入学考试试题及答案
- 2025年电子商务技术考试试卷及答案
- 2025年儿童发展心理学考试试题及答案
- 2025年护理硕士研究生入学考试试卷及答案
- 《中华人民共和国药典》修订内容(2025年)解读课件
- GB 10769-2025食品安全国家标准婴幼儿谷类辅助食品
- 审核技巧培训
- 延迟退休人员协议书
- 井下作业施工方案
- 2025年房地产开发经营服务项目投资风险评估报告
- EPC项目全流程咨询管理的核心要点与优化策略
- 铁路施工高空作业安全教育
- 2025年管道工(技师)职业技能鉴定理论考试题库(含答案)
- 一体化污水处理设备采购安装及运维 投标方案(技术方案)
- 晋升品质主管述职报告
评论
0/150
提交评论