node_array[real_sum].frequency=combine;
real_sum++;
6.输出码符号序列的递归程序
public void encode(HuffmanNode hn){
if(hn.next!=null&&hn.next.code!=null){
if(hn.next.flag!=false){
encode(hn.next);
hn.code=hn.next.code+hn.code;
hn.flag=false;
}
else
hn.code= hn.next.code+ hn.code;
//System.out.println(hn.code);
}
}
7.译码程序
class ButtonListener2 implements ActionListener{
public void actionPerformed(ActionEvent e){
String s = e.getActionCommand();
if("开始译码" == s){
ta.append("译码结果为:"+"\n");
String get_yima;
get_yima=tf3.getText();
char yima_array[];
yima_array=get_yima.toCharArray();
String str=String.valueOf(yima_array[0]);
for(int i=0;i
}
}
}
六、源程序
import javax.swing.*;
import java.awt.Color;
import java.awt.event.*;
class HuffmanNode { //建立Huffman编码结点
public int frequency;
public boolean flag;
public char name;
public String code;
public HuffmanNode next;
HuffmanNode(char name,int frequency,boolean flag){
this.name=name;
this.frequency=frequency;
this.flag=flag;
this.code="";
this.next=null;
}
}
class N_Huffman_Jframe extends JFrame{
static public int n;
static public int sum;
static public int real_sum;
static public char sign[]=new char[50];//存放输入的符号
static public int times[]=new int[50];
ButtonListener bl =new ButtonListener();
ButtonListener2 b2 =new ButtonListener2();
JTextField tf1=new JTextField();
JTextField tf2=new JTextField();
JTextField tf3=new JTextField();
JTextArea ta=new JTextArea();
/////////////////////////////////////////////////////////////
N_Huffman_Jframe(String title){
super(title);
this.setSize(500,360);
this.setLocation(400, 200);
addWindowListener( // 窗口监听器
new WindowAdapter() {
public void windowClosing(WindowEvent e) { // 关闭所
有窗体
System.exit(0);
}
}
);
JPanel p=new JPanel();
p.setLayout(null);
JLabel l1=new JLabel("请输入符号序列");
l1.setFont(new java.awt.Font("微软雅黑",4,15));
l1.setBounds(10, 13, 120,20);
tf1.setBounds(125, 10,350, 30);
JLabel l2=new JLabel("元数");
l2.setBounds(355, 58, 50,20);
l2.setFont(new java.awt.Font("微软雅黑",4,15));
tf2.setBounds(395,55,80, 30);
JButton ok=new JButton("确认");
ok.setBounds(355, 100, 120, 35);
ok.setBackground(Color.white);
ok.setFont(new java.awt.Font("微软雅黑",4,15));
ok.addActionListener(bl);
JLabel l4=new JLabel("需要译码序列");
l4.setFont(new java.awt.Font("微软雅黑",4,15));
l4.setBounds(10, 250, 90, 20);
tf3.setBounds(110, 245, 365, 30);
JButton yima=new JButton("开始译码");
yima.setBounds(170, 282, 200, 30);
yima.setBackground(Color.white);
yima.setFont(new java.awt.Font("微软雅黑",4,15));
yima.addActionListener(b2);
ta.setBounds(15, 50, 330, 180);
ta.setLineWrap(true);
JScrollPane jsp=new JScrollPane(ta);
jsp.setBounds(15, 50, 330, 180);
this.add(p);