Huffman编码

数组里面的权值对应了空格以及abcd......xyz,然后进行Huffman编码

 

package tree;

import java.util.ArrayList;

public class testHuffmanTree {
	public static void main(String[] args) {
		//the weight of leafNode
		int weight[]={186,64,13,22,32,103,21,15,47,57,
				        1, 5,32,20,57, 63,15, 1,48,51,
				       80,23,8,18,1,16,1};	
		huffmanTree tree =new huffmanTree(weight);
		tree.HuffmanCoding();
		tree.printHuffmanCoding();
		
	}
}
class huffmanTreeNode{
	int weight;
	int parent,lchild,rchild;
	huffmanTreeNode(){}
	huffmanTreeNode(int weight,int parent,int lchild,int rchild){
    	this.weight=weight;	
    	this.parent=parent;
    	this.lchild=lchild;
    	this.rchild=rchild;
	}
}
class huffmanTree{
	private int n;//the number of leafNodes
	private int m;
	private int[] weight;
	/*the number of all Nodes
	 *不能在这里写m=2*n-1;否则m只是会等于1.
	*/
	//ArrayList  huffmanTree store the nodes of the tree
	private ArrayList<huffmanTreeNode>  huffmanTree=new ArrayList<huffmanTreeNode>();
	//Coding stores the coding of the huffmanTree.
	private char[][] Coding;
 	huffmanTree(){}
	huffmanTree(int[] weight){
		this.weight=weight;	
		//如果m,n 不在这里初始化,在其他函数中初始化是会随即销毁的
		n=weight.length;
		m=2*n-1;
		creatHuffman();
	} 
	public ArrayList<huffmanTreeNode> getHuffmanTree() {
		return huffmanTree;
	}
    /*to select  two nodes with the minimun weight 
     * (index ranges from 0 to endOfArrayList),
     * and these two nodes' parent  equals 0
	*/
    int Select(int endOfArrayList){
    	int minimun=1000000000;
    	int current=-1;
    	for(int i=0;i<=endOfArrayList;i++){
    		if(0!=huffmanTree.get(i).parent)
    			continue;
    		if(huffmanTree.get(i).weight<minimun){
    			minimun=huffmanTree.get(i).weight;	
    			current=i;  			
    		}  			
    	}
    	return current;	
    }
    //creatHuffman
	void creatHuffman(){		
		for(int i=0;i<n;i++){
			huffmanTree.add(new huffmanTreeNode(weight[i],0,0,0));			
		}	
		for(int i=n;i<m;i++){
			huffmanTree.add(new huffmanTreeNode(0,0,0,0));
		}
		for(int i=n;i<m;i++ ){
			int s1,s2;
			s1=Select(i-1);			
			huffmanTree.get(s1).parent=i;
			s2=Select(i-1);			
			huffmanTree.get(s2).parent=i;
			// s1 and s2 are the smallest 
			huffmanTree.get(i).lchild=s1;
			huffmanTree.get(i).rchild=s2;// in this case  lchild  is  not bigger than rchild
			huffmanTree.get(i).weight=
					huffmanTree.get(s1).weight+
					huffmanTree.get(s2).weight;	
		}    	
	}
	//getHuffmanCoding
    void  HuffmanCoding(){
    	Coding=new char[n][n]; 	
    	for(int i=0;i<n;i++){
    		int start=n-1;
    		for(int c=i, f=huffmanTree.get(i).parent;  f!=0;  c=f,f=huffmanTree.get(f).parent){
    			if(huffmanTree.get(f).lchild==c)
    				Coding[i][start--]='0';//左分支为‘0’
    			else 
    				Coding[i][start--]='1';	
    		}   		 		
    	}	
    }
    //printHuffmanCoding
    void printHuffmanCoding(){
    	char letter=97;//对应的字母
    	System.out.println("the codes  are as following ******" );
    	for(int i=0;i<n;i++){//第一个是空格,额外考虑
    		if(i!=0){
    			System.out.print(letter+"  : "  );
        		letter++; 			
    		}
    		else 
    			System.out.print("空格"+"  : "  );   		
    		for(int j=0;j<n;j++){
    			if(Coding[i][j]!='#')
    	    		System.out.print(Coding[i][j]+" ");
    		}
    		System.out.println();
    	}	
    }
}
 

 

你可能感兴趣的:(Huffman)