赫夫曼编码压缩解压

package test.赫夫曼压缩;


import java.util.*;

public class demo {
    public static void main(String[] args) {
        String str = "你好我是赵冀超";
        Tree tree = new Tree();
        HashMap<String,String> hashMap = tree.creatTree(str);
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < str.length(); i++) {
            if (hashMap.get(str.charAt(i)+"").contains("null")){
                stringBuffer.append((hashMap.get(str.charAt(i)+"").replace("null","")));
            }
            else{
                stringBuffer.append(hashMap.get(str.charAt(i)+""));
            }
        }

        System.out.println(stringBuffer);
        System.out.println(tree.encode(stringBuffer.toString()));
    }
}
class node{

    String character;
    int weight;
    String code;
    node right;
    node left;
    public node(String character, int weight) {
        this.character = character;
        this.weight = weight;
    }
    public void mid(){
        if (this.left!=null){
            this.left.mid();
        }
        System.out.println(this.character);
        if (this.right!=null){
            this.right.mid();
        }
    }

    public void midGetCode(HashMap<String,String>hashMap) {
        if (this.left!=null){
            this.left.code = this.code+this.left.code;
            this.left.midGetCode(hashMap);
        }
        if (this.left==null&&this.right==null){
            hashMap.put(this.character,this.code);
        }
        if (this.right!=null){
            this.right.code = this.code+this.right.code;
            this.right.midGetCode(hashMap);
        }
    }
}
class Tree{
    node root;
    HashMap<String,String> hashMap;
    public HashMap<String,String> creatTree(String str){
        int length = str.length();
        ArrayList<node> arrayList = new ArrayList<>();
        char[] arr = str.toCharArray();
        Set<String> strings  =new HashSet<>();
        for (char n:arr){
            strings.add(n+"");
        }
        HashMap<String,Integer> hashMap = new HashMap<>();
        for (String s:strings){
            int sum = 0;
            for (int i = 0; i < length; i++) {
                if (s.equals(str.charAt(i)+"")){
                    sum++;
                }
                hashMap.put(str.charAt(i)+"",sum);
            }
        }

        for (int i = 0; i < length; i++) {
            arrayList.add(new node(str.charAt(i)+"",hashMap.get(str.charAt(i)+"")));
        }
        while (arrayList.size()>1){
            arrayList.sort(new Comparator<node>() {
                @Override
                public int compare(node o1, node o2) {
                    return o1.weight-o2.weight;
                }
            });
            node left = arrayList.get(0);
            node right = arrayList.get(1);
            left.code = "0";
            right.code = "1";
            node parent = new node("", left.weight+right.weight);
            parent.left = left;
            parent.right = right;
            arrayList.remove(left);
            arrayList.remove(right);
            arrayList.add(parent);
        }
        root = arrayList.get(0);
        this.hashMap = getCode();
       return this.hashMap;
    }
    public HashMap<String,String> getCode(){
        HashMap<String,String> hashMap = new HashMap<>();
        if (root!=null){
            root.midGetCode(hashMap);
            return hashMap;
        }else{
            return null;
        }
    }
    public String encode(String str){
        HashMap<String,String> hashMap = this.hashMap;
        Set<String> keys = hashMap.keySet();
        for (String s:keys){
            if (hashMap.get(s).contains("null")){
                hashMap.put(s,hashMap.get(s).replace("null",""));
            }
        }
        int right = 0;
        int left = 0;
        String n  = "";
        Collection<String> collections = hashMap.values();
        HashMap<String,String> hashMap1 = new HashMap<>();

        for (String s:keys){
            hashMap1.put(hashMap.get(s),s);
        }
        while (right<=str.length()&&right>=left){
            String s = str.substring(left,right);
            int temp = 0;
            for (String m:collections){
                if (s.equals(m)){
                    n+=hashMap1.get(m);
                    left = right;
                    temp++;
                }
            }
            if (temp!=0){
            }else{
                right++;
            }
        }
        return n;
    }
}

  • 8
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值