




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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 CodeString d;String c;Code(String d1,String c1)d = d1;c = c1;public static class Node
3、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;public static ArrayLi
4、st list= new ArrayList();public static List list1 = new ArrayList();public static ArrayList nodes = new ArrayList();public HuffmanTree(pinShu p1)int a = new int10;for(int f = 0;f 10;f +)af = 0;a0 = 2;for(int i = 1;i = 0;j -)ai += aj;ai+;/System.out.println(ai); /System.out.println(high+ +xweight+ +y
5、weight);for(int i = 0;i 1) sort(nodes); Node left = nodes.get(nodes.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; node
6、s.remove(nodes.size()-1); nodes.remove(nodes.size()-1); nodes.add(parent1);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 queue = new Array
7、Deque(); if( root != null) queue.offer(root); 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;
8、/p.leftChild.x = p.x-100/p.leftChild.w; p.leftChild.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+3
9、0; /p.rightChild.y = p.y +60; queue.offer(p.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.eq
10、uals(temp1.data) s=0; 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 StringBuffer(); for(int q = 0;q
11、 getHigh(subtree.rightChild)return 1+ getHigh(subtree.leftChild);else return 1+ getHigh(subtree.rightChild);public void sort(ArrayList nodes )for(int i = 0;i nodes.size()-1;i +)for(int j = i+1;j nodes.size();j +)if(nodes.get(i).weightnodes.get(j).weight)Node temp = nodes.get(i);nodes.set(i, nodes.ge
12、t(j);nodes.set(j,temp); public String print1() 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 = 0;i str.length();i +) for
13、(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+=处开始有错误; return s; return s; public static String retrans(Stri
14、ng 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()+ +list1.get(i).c); if(str.substring(0,list1.get(i).c.length().equals(list1.get(i).c) s+=list1
15、.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.awt.event.*;import java.awt.*;import javax.swing.*;public class WindowActionEvent extends JFramepublic static J
16、TextField inputText;public static JTextField inputText1;public static JTextField inputText2;public static JTextField inputText3;public static JTextField inputTexti;public static JTextField outputTexto;public static JTextArea textShow;JButton button;JButton button1;JButton button2;JButton button3;JBu
17、tton button4;JButton button5;MyCommandListener listener;public WindowActionEvent()init();this.setSize(800, 600);setVisible(true);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);void init() setLayout(new FlowLayout(); add(new JLabel(请输入要编码的字符:); inputText1 = new JTextField(47); inputText2 = new JTextF
18、ield(47); inputText2 = new JTextField(47); inputText3 = new JTextField(47); inputText = new JTextField(47); inputTexti = new JTextField(47); outputTexto = new JTextField(47); button = new JButton(确定); textShow = new JTextArea(10,47); add(inputText); add(button); add(new JLabel(对此字符串 进行处理:); add(new
19、JScrollPane(textShow);/滚动窗格 button1 = new JButton(作图动态生成哈夫曼树); add(Box.createHorizontalStrut(1000); /换行 add(button1); add(Box.createHorizontalStrut(1000); /换行 button1.addActionListener(new print(); add(new JLabel(请输入要转换的编码); add(inputTexti); add(Box.createHorizontalStrut(1000); /换行 button4 = new JBu
20、tton(进行哈夫曼编码); button4.addActionListener(new Transform(); button5 = new JButton(哈夫曼编码转原文); add(button4); add(button5); button5.addActionListener(new retransform(); add(Box.createHorizontalStrut(1000); /换行 add(new JLabel(转换结果为:); add(outputTexto); add(Box.createHorizontalStrut(1000); /换行 add(new JLab
21、el(请输入所要连接的IP:); add(inputText1); add(Box.createHorizontalStrut(1000); /换行 add(new JLabel(请输入端口号:); add(inputText2); add(Box.createHorizontalStrut(1000); /换行 button2 = new JButton(建立联系); button2.addActionListener(new ClientListen(); add(button2); button3 = new JButton(发送数据); add(button3); button3.ad
22、dActionListener(new postListen(); add(Box.createHorizontalStrut(1000); /换行 /add(inputText3); void setMyCommandListener(MyCommandListener listener)this.listener = listener;listener.setJTextField(inputText);listener.setJTextArea(textShow);inputText.addActionListener(listener);button.addActionListener(
23、listener);四、import java.awt.event.*;import java.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
24、fb.HuffmanTree.Node;public class picture extends JFramepublic ArrayList list= new ArrayList();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, 60
25、0);this.setName(哈夫曼树);this.getContentPane().setBackground(Color.WHITE);this.setVisible(true); public void sort(ArrayList nodes )for(int i = 0;i nodes.size()-1;i +)for(int j = i+1;j nodes.size();j +)if(nodes.get(i).weight1) sort(list); Node temp1 = list.get(list.size()-1); Node temp2 = new Node(); if
26、(temp1.data.equals(temp1.parent.leftChild.data) temp2 = 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.drawO
27、val(temp2.x, temp2.y, cr, cr); if(temp2.leftChild=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 =
28、y1+ylong; / g.drawLine(temp1.x+cr/2, temp1.y, temp1.parent.x+cr/2,temp1.parent.y+cr); for(int i=0; i10; 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.B
29、LACK); g.drawLine(temp1.x+cr/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,tem
30、p2.parent.y+cr); for(int i=0; i10; 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.par
31、ent.x+cr/2,temp2.parent.y+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.*;i
32、mport java.awt.geom.*;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 st
33、r;public void setJTextField(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();Huff
34、manTree huff = new HuffmanTree(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 ArrayList list = new ArrayList();
35、int num;int n = 0;int l;pinShu(String str)num = new intstr.length();l = str.length();for(int i = 0; i=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, (doub
36、le)numi/l);temp +=list.get(i)+:+double_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
37、();catch(Exception 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);c
38、atch(Exception e3)十一、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(Ex
39、ception e3)十二、建立客户端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/
40、JTextField inputText1;/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 Dat
41、aOutputStream out ;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(socketAddress); System.out.println(连接成功); in = new DataInputStream(mysocket.getInputStream(); out = new Data
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 电影产业电影发行与放映管理手册
- 网络游戏安全管理与保障措施
- 制造行业智能制造标准制定与实施方案
- 滑雪场安全管理制度预案
- 连州市电梯安全管理人员校验试题库与答案
- 体育赛事管理系统开发合同
- 湖北省随州市部分高中2024-2025学年高三下学期2月月考历史试题(原卷版+解析版)
- 物流行业绿色物流智能化运营方案
- 安全生产责任制确认表
- 健康医疗行业远程医疗平台搭建方案
- 《怪老头儿》名著导读
- 外研社一年级起点英语-四年级上册各单元知识点
- NB-T 47013.1-2015 承压设备无损检测 第1部分-通用要求
- 中国液冷数据中心市场深度研究报告 2023
- 5G智能工厂建设规划
- 一例化脓性链球菌感染的下肢静脉溃疡伤口循证护理
- 2018年襄阳市四中、五中自主招生考试数学试卷
- 储能系统介绍-电化学能-储能电站
- 道路环卫保洁投标方案
- (完整版)小学生心理健康教育课件
- 中国海洋大学辅导员考试试题2023
评论
0/150
提交评论