天天看點

使用java語言實作哈夫曼編碼系統

直接上代碼

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Huffmantree
{
    public static void main(final String[] args) 
    {
        final String str;
        Scanner scn=new Scanner(System.in);
        System.out.println("請任意輸入英文字元和機率:");
        str=scn.nextLine();
        scn.close();
        // String str="a0.4b0.1c0.2d0.3";
        final String regx1 = "[a-z]|[A-Z]{1}";
        final String regx2 = "\\d+.\\d+";
        final Pattern pt1 = Pattern.compile(regx1);
        final Pattern pt2 = Pattern.compile(regx2);
        final Matcher mt1 = pt1.matcher(str);
        final Matcher mt2 = pt2.matcher(str);
        int sum=0;
        while(true)
        {
            mt1.find();
            if(mt1.hitEnd())
            {
                mt1.reset();
                break;
            }
            sum++;
        }
        final List<nodes> tree= new ArrayList<nodes>(10);
        final List<String> strs=new ArrayList<>();
        for (int i = 0; i < sum; i++) 
        {
            mt1.find();
            mt2.find();
            tree.add(new nodes(mt1.group(),Double.valueOf(mt2.group())));
            strs.add(mt1.group());
        }
        Collections.sort(tree);
        for(int i=0;i<tree.size();i++)
        {
            
        }
        for(int i=0;i<2*sum-1;i+=2)
        {
            if(i==2*sum-2)
            {
                break;
            }
            tree.add(new nodes(tree.get(i).getCode()+tree.get(i+1).getCode(),tree.get(i).getP()+tree.get(i+1).getP()));
            tree.get(i).setFathernode(tree.get(tree.size()-1));
            tree.get(i).sethuffcode(0);
            tree.get(i+1).setFathernode(tree.get(tree.size()-1));
            tree.get(i+1).sethuffcode(1);
            Collections.sort(tree); 
        }
        for(int j=0;j<strs.size();j++)
        {
            String s="";
            for(int i=0;i<tree.size();i++)
            {
                if(tree.get(i).getCode().contains(strs.get(j)))
                {
                    s+=tree.get(i).getHuffcode();
                }
            }
            System.out.println(strs.get(j)+"的哈夫曼編碼為:"+s); 
        }      
    }
}
class nodes implements Comparable<nodes> 
{
    String code;
    double p;
    int huffcode;
    nodes fathernode=null;
    public String getCode() 
    {
        return code;
    }

    public double getP() 
    {
        return p;
    }
    nodes(final String code, final double p) {
        this.code = code;
        this.p = p;
    }

    public void sethuffcode(final int huffcode) {
        this.huffcode = huffcode;
    }

    public int getHuffcode() {
        return huffcode;
    }

    public void setFathernode(final nodes fathernode) {
        this.fathernode = fathernode;
    }

    public nodes getFathernode() 
    {
        return fathernode;
    }

    @Override
    public int compareTo(nodes o) 
    {
        final nodes node = (nodes) o;
        return(this.p<node.p?-1:(this.p==node.p?0:1));
    }
}