首页 > 代码库 > java实现Haffman编码

java实现Haffman编码

1、先创建一个树节点类(泛型类),为了方便使用集合的排序方法,泛型类要实现泛型接口Comparable,代码如下

技术分享
package com.hjp.huffman;

/**
 * Created by JiaPeng on 2016/12/28.
 */
public class Node<T> implements Comparable<Node<T>> {

    private T data;
    private int weight;
    private Node<T> left;
    private Node<T> right;
    private String codeStr;

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data =http://www.mamicode.com/ data;
    }

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    public Node<T> getLeft() {
        return left;
    }

    public void setLeft(Node<T> left) {
        this.left = left;
    }

    public Node<T> getRight() {
        return right;
    }

    public void setRight(Node<T> right) {
        this.right = right;
    }

    public Node(T data, int weight) {
        this.data =http://www.mamicode.com/ data;
        this.weight = weight;
    }

    public String getCodeStr() {
        return codeStr;
    }

    public void setCodeStr(String codeStr) {
        this.codeStr = codeStr;
    }

    @Override
    public int compareTo(Node<T> o) {
        if (o.weight > this.weight) {
            return 1;
        } else if (o.weight < this.weight) {
            return -1;
        }
        return 0;
    }

    @Override
    public String toString() {
        return "data:" + this.data + ",weight:" + this.weight + "; ";
    }
}
树节点类

2、编写泛型静态方法,构造Haffman树及对树编码,代码如下

技术分享
package com.hjp.huffman;

import java.util.*;

/**
 * Created by JiaPeng on 2016/12/28.
 */
public class HuffmanTree {

    public static <T> Node<T> createTree(List<Node<T>> nodes) {
        while (nodes.size() > 1) {
            int nodesLen = nodes.size();
            Collections.sort(nodes);
            Node<T> left = nodes.get(nodesLen - 1);
            Node<T> right = nodes.get(nodesLen - 2);
            Node<T> parent = new Node<T>(null, left.getWeight() + right.getWeight());
            parent.setLeft(left);
            parent.setRight(right);
            nodes.remove(left);
            nodes.remove(right);
            nodes.add(parent);
        }
        return nodes.get(0);
    }

    public static <T> List<Node<T>> breath(Node<T> root) {
        List<Node<T>> list = new ArrayList<Node<T>>();
        Queue<Node<T>> queue = new LinkedList<Node<T>>();
        queue.add(root);
        StringBuilder stringBuilder = new StringBuilder();
        while (!queue.isEmpty()) {
            Node<T> pNode = queue.poll();
            list.add(pNode);
            Node<T> leftNode = pNode.getLeft();
            String codeStr = pNode.getCodeStr();
            if (leftNode != null) {
                if (codeStr != null && !"".equals(codeStr)) {
                    leftNode.setCodeStr(codeStr + "0");
                } else {
                    leftNode.setCodeStr("0");
                }
                queue.add(leftNode);
            }
            Node<T> rightNode = pNode.getRight();
            if (rightNode != null) {
                if (codeStr != null && !"".equals(codeStr)) {
                    rightNode.setCodeStr(codeStr + "1");
                } else {
                    rightNode.setCodeStr("1");
                }
                queue.add(rightNode);
            }
        }
        return list;
    }

}
构造树方法及编码方法

3、测试方法

技术分享
package com.hjp.huffman;

import com.hjp.data.JSONUtil;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by JiaPeng on 2016/12/28.
 */
public class testMain {

    public static void main(String[] args){
        List<Node<String>> nodes = new ArrayList<Node<String>>();
        nodes.add(new Node<String>("b", 5));
        nodes.add(new Node<String>("a", 7));
        nodes.add(new Node<String>("d", 2));
        nodes.add(new Node<String>("c", 4));
        Node<String> root = HuffmanTree.createTree(nodes);
        List<Node<String>> huffmanList=HuffmanTree.breath(root);
        Map<String,String> huffmanMap=new LinkedHashMap<String, String>();
        for (int i = 0; i < huffmanList.size(); i++) {
            Node<String> curNode=huffmanList.get(i);
            if(curNode.getData()!=null){
                huffmanMap.put(curNode.getData(),curNode.getCodeStr());
            }
        }
        System.out.println(JSONUtil.toJson(huffmanMap));
    }

}
测试方法

 

java实现Haffman编码