20182309 哈夫曼树

哈夫曼树的实现

1.找到一个哈夫曼树的例子

  • 这个例子需要自己输入字符个数、字符集、频率分布
import java.util.Scanner;
public class HuffmanCode{
    //建立数的节点类
    static class Node{
        int weight;//频数
        int parent;
        int leftChild;
        int rightChild;

        public Node(int weight,int parent,int leftChild,int rightChild){
            this.weight=weight;
            this.parent=parent;
            this.leftChild=leftChild;
            this.rightChild=rightChild;
        }

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

        void setParent(int parent){
            this.parent=parent;
        }

        void setLeftChild(int leftChild){
            this.leftChild=leftChild;
        }

        void setRightChild(int rightChild){
            this.rightChild=rightChild;
        }

        int getWeight(){
            return weight;
        }

        int getParent(){
            return parent;
        }

        int getLeftChild(){
            return leftChild;
        }

        int getRightChild(){
            return rightChild;
        }
    }

    //新建哈夫曼编码
    static class NodeCode{
        String character;
        String code;
        NodeCode(String character,String code){
            this.character=character;
            this.code=code;
        }
        NodeCode(String code){
            this.code= code;
        }

        void setCharacter(String character){
            this.character=character;
        }

        void setCode(String code){
            this.code=code;
        }

        String getCharacter(){
            return character;
        }

        String getCode(){
            return code;
        }
    }

    //初始化一个huffuman树
    public static void initHuffmanTree(Node[] huffmanTree,int m){
        for(int i=0;i=0 )
            {
                start--;
                code[start]=((huffmanTree[parent].getLeftChild()==c)?'0':'1');
                c=parent;

            }
            for(;start

改造成我需要的哈夫曼树并添加所需功能

  • 重写节点的比较方法
    public int compareTo(HuffmanNode node) {
        if (this.count >= node.count){
            return 1;
        }
        else {
            return -1;
        }
    }
  • 构建哈夫曼树
    public HuffmanNode createTree(List nodes) {
        while (nodes.size() > 1) {
            Collections.sort(nodes);

            HuffmanNode left = nodes.get(nodes.size() - 2);
            left.setCode("0");
            HuffmanNode right = nodes.get(nodes.size() - 1);
            right.setCode("1");
            HuffmanNode parent = new HuffmanNode(null, left.getLength() + right.getLength());
            parent.setLeft(left);
            parent.setRight(right);
            nodes.remove(left);
            nodes.remove(right);
            nodes.add(parent);
        }
        return nodes.get(0);
    }
  • 给出字符的哈夫曼编码(利用List)
    public List breadth(HuffmanNode root) {
        List list = new ArrayList();
        Queue queue = new ArrayDeque();

        if (root != null) {
            queue.offer(root);
            root.getLeft().setCode(root.getCode() + "0");
            root.getRight().setCode(root.getCode() + "1");
        }

        while (!queue.isEmpty()) {
            list.add(queue.peek());
            HuffmanNode node = queue.poll();
            if (node.getLeft() != null)
                node.getLeft().setCode(node.getCode() + "0");
            if (node.getRight() != null)
                node.getRight().setCode(node.getCode() + "1");

            if (node.getLeft() != null) {
                queue.offer(node.getLeft());
            }

            if (node.getRight() != null) {
                queue.offer(node.getRight());
            }
        }
        return list;
    }
  • 主函数
    • 读取文件
        File file = new File("编码文件.txt");
        if (!file.exists()) {
            file.createNewFile();
        }
        BufferedReader br = new BufferedReader(new FileReader(file));
        String s;
        String message = "";
        while((s = br.readLine()) != null){
            message += s;
        }
    • 按字符拆分字符串
    String[] result = message.split("");
    • 统计频率
    for (int n = 0;n < result.length; n++){
            for (int i = 0; i < 27; i++){
                if (result[n].equals(list.get(i))){
                    number[i] += 1;
                }
            }
        }
    • 构建哈夫曼树
        HuffmanTree huffmanTree = new HuffmanTree();
        HuffmanNode node = huffmanTree.createTree(nodeList);
        List inlist = new ArrayList();
        inlist = huffmanTree.breadth(node);
    • 加密
        String res = "";
        for(int f = 0; f < sum; f++){
            for(int j = 0;j
    • 解密
        String string2 = "";
        for(int h = putlist.size(); h > 0; h--){
            string1 = string1 + putlist.get(0);
            putlist.remove(0);
            for(int i=0;i

    运行结果

    总代码连接

你可能感兴趣的:(20182309 哈夫曼树)