哈弗曼编码加密程序Decoder

package 哈弗曼编码;
import java.util.*;

public class Decoder {
	private String binCode;
	private BinaryNode root;
	private HashMap newDic=new HashMap();//解码时要根据二进制码的值得到字符,因此新的字典要以String作为Key
	private StringBuffer result=new StringBuffer();
	
	public Decoder(){
		
	}
	
	public Decoder(String binCode, BinaryNode root){
		this.binCode=binCode;
		
		decode(binCode,root);
	}
	
	public Decoder(String binCode, HashMap dic){
		this.binCode=binCode;
		decode(binCode,dic);
	}
	
	public void decode(String binCode, BinaryNode root){
		this.binCode=binCode;
		
		//构建编码字典,以HashMap结构存储。
		if(root.left==null&&root.right==null){
			newDic.put("0",root.data.getStr().charAt(0));
		}
		else{
			generateBinCode(root,"");
		}
		
		decode();
	}
	
	public void decode(String binCode, HashMap dic){
		this.binCode=binCode;
		//得到dic的keySet,遍历其中的key,然后构造newDic。
		for(Character key:dic.keySet()){
			newDic.put(dic.get(key), key);
		}
		
		for(String bc:newDic.keySet()){
			System.out.print(bc+": "+newDic.get(bc));
			System.out.println();
		}
		
		decode();
		
	}
	
	private void decode(){
		if(binCode==null||newDic==null){
			System.out.println("Please specify the binary code and dictionary or huffman tree root");
			return;
		}
		else if(validate()==false){
			System.out.println("The binCode is invalidate!");
		}
		else{
//			System.out.println("here");
			StringBuffer search=new StringBuffer();
			
			for(int i=0;i

你可能感兴趣的:(java)