
导入
import java.io.*; import java.util.Scanner;
huffman类的描述
类的成员变量
int n; int m; int s1, s2; String textString; String codeString;
内部类:作节点
class HuffmanTreeNode {
char data;
int weight;
HuffmanTreeNode parent, lchild, rchild;
HuffmanTreeNode() {
data = '0';
weight = 0;
parent = lchild = rchild = null;
}
HuffmanTreeNode(char a, int b, HuffmanTreeNode c, HuffmanTreeNode d, HuffmanTreeNode e) {
data = a;
weight = b;
parent = c;
lchild = d;
rchild = e;
}
}
class HuffmanCodeNode {
char data;
String code;
HuffmanCodeNode() {
data = '0';
code = "0";
}
HuffmanCodeNode(char a, String b) {
data = a;
code = b;
}
public String toString() {
return "< "+data+" "+code+" >";
}
}
class LeafNode {
char data;
int weight;
LeafNode() {
data = '0';
weight = 0;
}
LeafNode(char a, int b) {
data = a;
weight = b;
}
public String toString() {
return "< "+data+" "+weight+" >";
}
}
初始
LeafNode[] w = new LeafNode[128]; HuffmanTreeNode[] HTNode = new HuffmanTreeNode[255]; HuffmanCodeNode[] HCNode = new HuffmanCodeNode[255];
基本方法
public void setN(int num) {
n = num;
}
public void setM(int num) {
m = num;
}
public void setS1(int num) {
s1 = num;
}
public void setS2(int num) {
s2 = num;
}
注意1:writeToText()
注意2:Main()
写入文件
public void writeToText(String filename) {
System.out.println("请输入文本:");
Scanner sc = new Scanner(System.in);
try {
BufferedWriter out = new BufferedWriter(new FileWriter(filename));
out.write(sc.nextLine());
out.close();
sc.close();
System.out.println(" *** 作成功!");
}catch(IOException e) {
e.printStackTrace(); // 显示错误信息,和位置
}
}
统计字符次数
public void readCount(String filename) {
BufferedReader reader = null;
StringBuffer strBuffer = null;
try {
reader = new BufferedReader(new FileReader(filename));
strBuffer = new StringBuffer();
String lines;
while((lines=reader.readLine()) != null) {
strBuffer.append(lines);
}
} catch (IOException e) {
// 以StringBuffer为 *** 作
e.printStackTrace();
} finally {
if(reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} // end try...
String text = new String(strBuffer);
System.out.println("一、读取文本为:");
System.out.println(text);
textString = text;
// 去重
String newText = "";
for(int i=0; i ) :");
for(int i=0; i
select
public void selectMin(HuffmanTreeNode[] HT, int n) {
int i;
for(i=1; i<=n; i++) {
if(HT[i].parent == null) {
setS1(i);
break;
}
}
for(i=1; i<=n; i++) {
if(HT[i].parent==null && HT[s1].weight>HT[i].weight) {
setS1(i);
}
}
for(i=1; i<=n; i++) {
if(HT[i].parent==null && i!=s1) {
setS2(i);
break;
}
}
for(i=1; i<=n; i++) {
if(HT[i].parent==null && HT[s2].weight>HT[i].weight && i!=s1) {
setS2(i);
}
}
} // end select
构建赫夫曼树
public void creatHuffmanTree() {
int i;
// init
for(i=1; i<=n; i++) {
HTNode[i] = new HuffmanTreeNode(w[i].data, w[i].weight, null, null, null);
}
for(i=n+1; i<=m; i++) {
HTNode[i] = new HuffmanTreeNode();
}
// 建叶子以上的节点,链接
for(i=n+1; i<=m; i++) {
selectMin(HTNode, i-1);
HTNode[s1].parent = HTNode[i]; HTNode[s2].parent = HTNode[i];
HTNode[i].lchild = HTNode[s1]; HTNode[i].rchild = HTNode[s2];
HTNode[i].weight = HTNode[s1].weight+HTNode[s2].weight;
}
} // end creatHuffmanTree
写入code文件
public void writeCodeToFile(String filename2) {
String tempString = "";
for(int i=0; i
编码
public void encoding() {
int i;
// encoding
for(i=1; i<=n; i++) {
// init
HCNode[i] = new HuffmanCodeNode();
// data
HCNode[i].data = HTNode[i].data;
// code
HuffmanTreeNode tempNode;
tempNode = HTNode[i];
String tempString = "";
while(tempNode.parent != null) {
if(tempNode == tempNode.parent.lchild) tempString = "0" + tempString;
else tempString = "1" + tempString;
tempNode = tempNode.parent;
}
HCNode[i].code = tempString;
}
// 打印
System.out.println("三、每个字符对应的编码为:( < 字符 赫夫曼编码 > )");
for(i=1; i<=n; i++) {
System.out.println(HCNode[i].toString());
}
} // end encoding
译码
public void decoding() {
int i;
HuffmanTreeNode tempNode;
String tempString = "";
tempNode = HTNode[2*n-1];
for(i=0; i
方法集成
public void Main(String filename1, String filename2) {
writeToText(filename1);
readCount(filename1);
creatHuffmanTree();
encoding();
writeCodeToFile(filename2);
decoding();
}
测试类
public class Test_Huffman {
public static void main(String[] args) {
final String filepath1 = "D:\Java Space\Java 0~100\src\Huffman\text.txt";
final String filepath2 = "D:\Java Space\Java 0~100\src\Huffman\code.txt";
Huffman huffman = new Huffman();
huffman.Main(filepath1, filepath2);
}
}
效果图
源码:
拷贝即用,只需要在D盘下面新建:
text.txt和code.txt
import java.io.*;
import java.util.Scanner;
class Huffman {
int n;
int m;
int s1, s2;
String textString;
String codeString;
class HuffmanTreeNode {
char data;
int weight;
HuffmanTreeNode parent, lchild, rchild;
HuffmanTreeNode() {
data = '0';
weight = 0;
parent = lchild = rchild = null;
}
HuffmanTreeNode(char a, int b, HuffmanTreeNode c, HuffmanTreeNode d, HuffmanTreeNode e) {
data = a;
weight = b;
parent = c;
lchild = d;
rchild = e;
}
}
class HuffmanCodeNode {
char data;
String code;
HuffmanCodeNode() {
data = '0';
code = "0";
}
HuffmanCodeNode(char a, String b) {
data = a;
code = b;
}
public String toString() {
return "< "+data+" "+code+" >";
}
}
class LeafNode {
char data;
int weight;
LeafNode() {
data = '0';
weight = 0;
}
LeafNode(char a, int b) {
data = a;
weight = b;
}
public String toString() {
return "< "+data+" "+weight+" >";
}
}
LeafNode[] w = new LeafNode[128];
HuffmanTreeNode[] HTNode = new HuffmanTreeNode[255];
HuffmanCodeNode[] HCNode = new HuffmanCodeNode[255];
public void setN(int num) {
n = num;
}
public void setM(int num) {
m = num;
}
public void setS1(int num) {
s1 = num;
}
public void setS2(int num) {
s2 = num;
}
public void writeToText(String filename) {
System.out.println("请输入文本:");
Scanner sc = new Scanner(System.in);
try {
BufferedWriter out = new BufferedWriter(new FileWriter(filename));
out.write(sc.nextLine());
out.close();
sc.close();
System.out.println(" *** 作成功!");
}catch(IOException e) {
e.printStackTrace(); // 显示错误信息,和位置
}
}
public void readCount(String filename) {
BufferedReader reader = null;
StringBuffer strBuffer = null;
try {
reader = new BufferedReader(new FileReader(filename));
strBuffer = new StringBuffer();
String lines;
while((lines=reader.readLine()) != null) {
strBuffer.append(lines);
}
} catch (IOException e) {
// 以StringBuffer为 *** 作
e.printStackTrace();
} finally {
if(reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} // end try...
String text = new String(strBuffer);
System.out.println("一、读取文本为:");
System.out.println(text);
textString = text;
// 去重
String newText = "";
for(int i=0; i ) :");
for(int i=0; iHT[i].weight) {
setS1(i);
}
}
for(i=1; i<=n; i++) {
if(HT[i].parent==null && i!=s1) {
setS2(i);
break;
}
}
for(i=1; i<=n; i++) {
if(HT[i].parent==null && HT[s2].weight>HT[i].weight && i!=s1) {
setS2(i);
}
}
} // end select
public void creatHuffmanTree() {
int i;
// init
for(i=1; i<=n; i++) {
HTNode[i] = new HuffmanTreeNode(w[i].data, w[i].weight, null, null, null);
}
for(i=n+1; i<=m; i++) {
HTNode[i] = new HuffmanTreeNode();
}
// 建叶子以上的节点,链接
for(i=n+1; i<=m; i++) {
selectMin(HTNode, i-1);
HTNode[s1].parent = HTNode[i]; HTNode[s2].parent = HTNode[i];
HTNode[i].lchild = HTNode[s1]; HTNode[i].rchild = HTNode[s2];
HTNode[i].weight = HTNode[s1].weight+HTNode[s2].weight;
}
} // end creatHuffmanTree
public void writeCodeToFile(String filename2) {
String tempString = "";
for(int i=0; i )");
for(i=1; i<=n; i++) {
System.out.println(HCNode[i].toString());
}
} // end encoding
public void decoding() {
int i;
HuffmanTreeNode tempNode;
String tempString = "";
tempNode = HTNode[2*n-1];
for(i=0; i欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)