java实现哈夫曼编码

哈夫曼算法

众所周知,哈夫曼算法适用于文件压缩领域。由于上机需要,就写了一个Java代码实现。

哈夫曼算法的内容

1.将带有符号的字母存储为叶子节点,其权值为符号出现的次数
2.从这些叶子结点中找到权值最小的两个节点,然后将其合并成一个新节点,即新节点的权值为两个节点的权值之和,然后删除这两个节点。
3.将新节点加入队列中。重复2-3步骤直到队列只有一个节点。

哈夫曼的实现

1.定义TreeNode节点

private static class TreeNode implements Comparable<TreeNode>{
        TreeNode left;
        TreeNode right;
        int weight;
        char ch;
        String code;

        public TreeNode(int weight,TreeNode left,TreeNode right) {
            this.weight = weight;
            this.left = left;
            this.right = right;
            this.code = "";
        }
        @Override
        public int compareTo(TreeNode o) {
            if (this.weight > o.weight) {
                return 1;
            }else if (this.weight < o.weight) {
                return -1;
            }else {
                return 0;
            }   
        }
    }

ch保存相应字符。code保存0或1,方便打印字符编码。

2.实现核心算法
这里用了一个TreeSet的容器装节点,因为它自带排序功能。(前提是对象为Comparable的子类)

public static TreeNode huffman(TreeMap data) {
        TreeSet tNodes = new TreeSet<>();
        Set weights = data.keySet();
        Iterator iterator = weights.iterator();
        while (iterator.hasNext()) {
            int weight = iterator.next();
            TreeNode tmp = new TreeNode(weight, null, null);
            tmp.ch = data.get(weight);
            tNodes.add(tmp);
        }
        while (tNodes.size() > 1) {
            TreeNode leftNode = tNodes.pollFirst();
            leftNode.code = "0";
            TreeNode rightNode = tNodes.pollFirst();
            rightNode.code = "1";
            TreeNode newNode = new TreeNode(leftNode.weight+rightNode.weight,
                    leftNode, rightNode);
            tNodes.add(newNode);
        }
        return tNodes.first();
    }

3.得到字符编码

private static void code(TreeNode t) {
        if (t.left != null) {
            t.left.code = t.code + t.left.code;
            code(t.left);
        }
        if (t.right != null) {
            t.right.code = t.code + t.right.code;
            code(t.right);
        }
    }

4.打印字符编码结果

public static void print(TreeNode root) {
        if (root != null) {
            if (root.left == null && root.right == null) {
                System.out.println(root.ch + " 编码:" + root.code);
            }else {
                print(root.left);
                print(root.right);
            }
        }
    }

结果截图

java实现哈夫曼编码_第1张图片

最后放上完整代码:

import java.util.Iterator;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;

public class Huffman {

    private static class TreeNode implements Comparable{
        TreeNode left;
        TreeNode right;
        int weight;
        char ch;
        String code;

        public TreeNode(int weight,TreeNode left,TreeNode right) {
            this.weight = weight;
            this.left = left;
            this.right = right;
            this.code = "";
        }
        @Override
        public int compareTo(TreeNode o) {
            if (this.weight > o.weight) {
                return 1;
            }else if (this.weight < o.weight) {
                return -1;
            }else {
                return 0;
            }   
        }
    }

    public static TreeNode huffman(TreeMap data) {
        TreeSet tNodes = new TreeSet<>();
        Set weights = data.keySet();
        Iterator iterator = weights.iterator();
        while (iterator.hasNext()) {
            int weight = iterator.next();
            TreeNode tmp = new TreeNode(weight, null, null);
            tmp.ch = data.get(weight);
            tNodes.add(tmp);
        }
        while (tNodes.size() > 1) {
            TreeNode leftNode = tNodes.pollFirst();
            leftNode.code = "0";
            TreeNode rightNode = tNodes.pollFirst();
            rightNode.code = "1";
            TreeNode newNode = new TreeNode(leftNode.weight+rightNode.weight,
                    leftNode, rightNode);
            tNodes.add(newNode);
        }
        return tNodes.first();
    }

    private static void code(TreeNode t) {
        if (t.left != null) {
            t.left.code = t.code + t.left.code;
            code(t.left);
        }
        if (t.right != null) {
            t.right.code = t.code + t.right.code;
            code(t.right);
        }
    }

    public static void print(TreeNode root) {
        if (root != null) {
            if (root.left == null && root.right == null) {
                System.out.println(root.ch + " 编码:" + root.code);
            }else {
                print(root.left);
                print(root.right);
            }
        }
    }

    public static void main(String[] args) {
        TreeMap test = new TreeMap<>();
        test.put(5, 'f');
        test.put(9, 'e');
        test.put(12, 'c');
        test.put(13, 'b');
        test.put(16, 'd');
        test.put(45, 'a');

        TreeNode root = huffman(test);
        code(root);
        print(root);
    }

}

你可能感兴趣的:(算法-java)