用Java实现的huffman编码树(仅对26个字符)。
1.使用的是26字符的频率作为各字符的权值
2.使用到最小堆,用java.util.PriorityQueue类实现。
3.各huffman node需要实现java.lang.Comparable接口
主函数
1.使用的是26字符的频率作为各字符的权值
2.使用到最小堆,用java.util.PriorityQueue类实现。
3.各huffman node需要实现java.lang.Comparable接口
/**
* HuffmanTreeNode.java
*/
package Huffman;
/**
* @function :
* @author :hjh
* @company :fdu
* @date :2009-10-23
*/
public class HuffmanTreeNode implements Comparable<HuffmanTreeNode>{
private char ch; //if node is leaf, this value is valid
private boolean leaf;
private float weight; //frequency
private HuffmanTreeNode leftChild;
private HuffmanTreeNode rightChild;
private String huffmanCode;
/**
* @param c
* @param weight
*/
public HuffmanTreeNode( char c, float weight) {
// constructor for leaf node
this.ch = c;
this.weight = weight;
this.leaf = true;
this.leftChild = null;
this.rightChild = null;
this.huffmanCode = "";
}
public HuffmanTreeNode( float weight,HuffmanTreeNode leftChild,HuffmanTreeNode rightChild) {
// constructor for none-leaf node
this.ch = '#';
this.weight = weight;
this.leaf = false;
this.leftChild = leftChild;
this.rightChild = rightChild;
this.huffmanCode = "";
}
public char getCh() {
return ch;
}
public void setCh( char ch) {
this.ch = ch;
}
public boolean isLeaf() {
return leaf;
}
public void setLeaf( boolean leaf) {
this.leaf = leaf;
}
public float getWeight() {
return weight;
}
public void setWeight( float weight) {
this.weight = weight;
}
public HuffmanTreeNode getLeftChild() {
return leftChild;
}
public void setLeftChild(HuffmanTreeNode leftChild) {
this.leftChild = leftChild;
}
public HuffmanTreeNode getRightChild() {
return rightChild;
}
public void setRightChild(HuffmanTreeNode rightChild) {
this.rightChild = rightChild;
}
public String getHuffmanCode() {
return huffmanCode;
}
public void setHuffmanCode(String huffmanCode) {
this.huffmanCode = huffmanCode;
}
/* (non-Javadoc)
* @see java.lang.Comparable#compareTo(java.lang.Object)
*/
@Override
public int compareTo(HuffmanTreeNode o) {
return weight==o.weight?0:(weight<o.weight?-1:1);
}
public String toString(){
return weight+ "@"+huffmanCode+ "@"+ch;
}
}
* HuffmanTreeNode.java
*/
package Huffman;
/**
* @function :
* @author :hjh
* @company :fdu
* @date :2009-10-23
*/
public class HuffmanTreeNode implements Comparable<HuffmanTreeNode>{
private char ch; //if node is leaf, this value is valid
private boolean leaf;
private float weight; //frequency
private HuffmanTreeNode leftChild;
private HuffmanTreeNode rightChild;
private String huffmanCode;
/**
* @param c
* @param weight
*/
public HuffmanTreeNode( char c, float weight) {
// constructor for leaf node
this.ch = c;
this.weight = weight;
this.leaf = true;
this.leftChild = null;
this.rightChild = null;
this.huffmanCode = "";
}
public HuffmanTreeNode( float weight,HuffmanTreeNode leftChild,HuffmanTreeNode rightChild) {
// constructor for none-leaf node
this.ch = '#';
this.weight = weight;
this.leaf = false;
this.leftChild = leftChild;
this.rightChild = rightChild;
this.huffmanCode = "";
}
public char getCh() {
return ch;
}
public void setCh( char ch) {
this.ch = ch;
}
public boolean isLeaf() {
return leaf;
}
public void setLeaf( boolean leaf) {
this.leaf = leaf;
}
public float getWeight() {
return weight;
}
public void setWeight( float weight) {
this.weight = weight;
}
public HuffmanTreeNode getLeftChild() {
return leftChild;
}
public void setLeftChild(HuffmanTreeNode leftChild) {
this.leftChild = leftChild;
}
public HuffmanTreeNode getRightChild() {
return rightChild;
}
public void setRightChild(HuffmanTreeNode rightChild) {
this.rightChild = rightChild;
}
public String getHuffmanCode() {
return huffmanCode;
}
public void setHuffmanCode(String huffmanCode) {
this.huffmanCode = huffmanCode;
}
/* (non-Javadoc)
* @see java.lang.Comparable#compareTo(java.lang.Object)
*/
@Override
public int compareTo(HuffmanTreeNode o) {
return weight==o.weight?0:(weight<o.weight?-1:1);
}
public String toString(){
return weight+ "@"+huffmanCode+ "@"+ch;
}
}
主函数
/**
* HuffmanCoding.java
*/
package Huffman;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.PriorityQueue;
/**
* @function :
* @author :hjh
* @company :fdu
* @date :2009-10-23
*/
public class HuffmanCoding {
//the symbol table of 26 characters
static HuffmanTreeNode[] nodes= new HuffmanTreeNode[26];
static void initial() {
BufferedReader br = null;
try {
br = new BufferedReader( new FileReader( "bin/resource/abcfreqs.txt"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String line = null;
try {
line = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
float[] freqs = new float[26];
char[] chars= new char[26];
String[] strs = line.split( " ");
for ( int i = 0,j=0; i<chars.length && j < strs.length; i++,j+=2) {
chars[i]= strs[j].charAt(0);
freqs[i] = Float.valueOf(strs[j+1]) * 0.01f;
nodes[i]= new HuffmanTreeNode(chars[i],freqs[i]);
}
}
public static void main(String args[]){
initial();
//create a minimum heap
PriorityQueue<HuffmanTreeNode> minHeap = new PriorityQueue<HuffmanTreeNode>();
for (HuffmanTreeNode node : nodes) {
minHeap.offer(node);
}
HuffmanTreeNode root=genHuffmanTree(minHeap);
dfsEncoding(root);
enCoding();
}
private static void enCoding() {
try {
BufferedReader br = new BufferedReader( new FileReader( "bin/resource/input.txt"));
BufferedWriter bw = new BufferedWriter( new FileWriter( "bin/resource/output.txt", false));
String line;
while ((line = br.readLine()) != null) {
StringBuffer buf = new StringBuffer();
for ( int i = 0; i < line.length(); i++) {
char c = line.charAt(i);
String huffcode=nodes[c-'A'].getHuffmanCode();
buf.append(huffcode);
}
bw.write(buf.toString());
bw.newLine();
}
bw.flush();
bw.close();
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void dfsEncoding(HuffmanTreeNode root) {
if(root== null)
return;
if(root.getLeftChild()== null && root.getRightChild()== null){
//System.out.println(root);
return;
}
if(root.getLeftChild()!= null){
root.getLeftChild().setHuffmanCode(root.getHuffmanCode()+ "0");
dfsEncoding(root.getLeftChild());
}
if(root.getRightChild()!= null){
root.getRightChild().setHuffmanCode(root.getHuffmanCode()+ "1");
dfsEncoding(root.getRightChild());
}
}
/**
*
* @param minHeap
* @return
*/
private static HuffmanTreeNode genHuffmanTree(PriorityQueue<HuffmanTreeNode> minHeap) {
while (!minHeap.isEmpty()) {
HuffmanTreeNode n1 = minHeap.poll();
if(minHeap.isEmpty())
return n1;
HuffmanTreeNode n2 = minHeap.poll();
float weight=n1.getWeight()+n2.getWeight();
HuffmanTreeNode s = new HuffmanTreeNode(weight,n1,n2);
minHeap.offer(s);
}
return null;
}
}
* HuffmanCoding.java
*/
package Huffman;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.PriorityQueue;
/**
* @function :
* @author :hjh
* @company :fdu
* @date :2009-10-23
*/
public class HuffmanCoding {
//the symbol table of 26 characters
static HuffmanTreeNode[] nodes= new HuffmanTreeNode[26];
static void initial() {
BufferedReader br = null;
try {
br = new BufferedReader( new FileReader( "bin/resource/abcfreqs.txt"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String line = null;
try {
line = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
float[] freqs = new float[26];
char[] chars= new char[26];
String[] strs = line.split( " ");
for ( int i = 0,j=0; i<chars.length && j < strs.length; i++,j+=2) {
chars[i]= strs[j].charAt(0);
freqs[i] = Float.valueOf(strs[j+1]) * 0.01f;
nodes[i]= new HuffmanTreeNode(chars[i],freqs[i]);
}
}
public static void main(String args[]){
initial();
//create a minimum heap
PriorityQueue<HuffmanTreeNode> minHeap = new PriorityQueue<HuffmanTreeNode>();
for (HuffmanTreeNode node : nodes) {
minHeap.offer(node);
}
HuffmanTreeNode root=genHuffmanTree(minHeap);
dfsEncoding(root);
enCoding();
}
private static void enCoding() {
try {
BufferedReader br = new BufferedReader( new FileReader( "bin/resource/input.txt"));
BufferedWriter bw = new BufferedWriter( new FileWriter( "bin/resource/output.txt", false));
String line;
while ((line = br.readLine()) != null) {
StringBuffer buf = new StringBuffer();
for ( int i = 0; i < line.length(); i++) {
char c = line.charAt(i);
String huffcode=nodes[c-'A'].getHuffmanCode();
buf.append(huffcode);
}
bw.write(buf.toString());
bw.newLine();
}
bw.flush();
bw.close();
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void dfsEncoding(HuffmanTreeNode root) {
if(root== null)
return;
if(root.getLeftChild()== null && root.getRightChild()== null){
//System.out.println(root);
return;
}
if(root.getLeftChild()!= null){
root.getLeftChild().setHuffmanCode(root.getHuffmanCode()+ "0");
dfsEncoding(root.getLeftChild());
}
if(root.getRightChild()!= null){
root.getRightChild().setHuffmanCode(root.getHuffmanCode()+ "1");
dfsEncoding(root.getRightChild());
}
}
/**
*
* @param minHeap
* @return
*/
private static HuffmanTreeNode genHuffmanTree(PriorityQueue<HuffmanTreeNode> minHeap) {
while (!minHeap.isEmpty()) {
HuffmanTreeNode n1 = minHeap.poll();
if(minHeap.isEmpty())
return n1;
HuffmanTreeNode n2 = minHeap.poll();
float weight=n1.getWeight()+n2.getWeight();
HuffmanTreeNode s = new HuffmanTreeNode(weight,n1,n2);
minHeap.offer(s);
}
return null;
}
}
转载于:https://blog.51cto.com/dongdong1314/221221