Java实现Huffman哈夫曼树(数组实现)

Posted Wecccccccc

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java实现Huffman哈夫曼树(数组实现)相关的知识,希望对你有一定的参考价值。

代码如下:

package HuffmanTreeArrays;

import java.util.ArrayList;
import java.util.Scanner;

public class HuffmanTree {
    private class TreeNode
    {
        private int val;
        private int left;
        private int right;
        private int parent;

        public TreeNode()
        {
            val = 0;
            left = -1;
            right = -1;
            parent = -1;
        }

        public TreeNode(int e)
        {
            val = e;
            left = -1;
            right = -1;
            parent = -1;
        }
    }

    private TreeNode data[];
    private int capacity;
    private int size;
    private final int maxData;

    public HuffmanTree(int n)
    {
        maxData = 999999999;
        size = n;
        capacity = 2*n-1;
        data = new TreeNode[2*n];
        TreeNode t = new TreeNode(maxData);
        data[0] = t;
        Scanner sc = new Scanner(System.in);
        for (int i = 1;i<=n;i++)
        {
             t = new TreeNode(sc.nextInt());
            data[i] = t;
        }
    }

    public void buildHuffmanTree()
    {
        int s1,s2;
        for (int i = size+1;i<=capacity;i++)
        {
            s1 = s2 = 0;
            for (int j = 1;j<=i-1;j++)
            {
                if (data[j].parent==-1) {
                    if (data[j].val < data[s1].val) {
                        s2 = s1;
                        s1 = j;
                    } else if (data[j].val < data[s2].val) {
                        s2 = j;
                    }
                }
            }
            data[s1].parent = i;
            data[s2].parent = i;
            TreeNode t = new TreeNode(data[s1].val+data[s2].val);
            t.left = s1;
            t.right = s2;
            data[i] = t;
            printNode(t);
        }
    }

    private void printNode(TreeNode t)
    {
        System.out.println(t.val+" ("+data[t.left].val+","+data[t.right].val+")");
    }

    public void codeHuffmanTree()
    {
        if (size==0)
        {
            System.out.println("The HuffmanTree is empty");
            return ;
        }
        ArrayList<Integer> arrays = new ArrayList<>();
        dfsTree(data[capacity],arrays);

    }

    private void dfsTree(TreeNode t,ArrayList<Integer> arrays)
    {
        if (t.left==-1 && t.right==-1)
        {
            System.out.println(t.val+" "+arrays);
            return ;
        }

        if (t.left!=-1)
        {
            arrays.add(0);
            dfsTree(data[t.left],arrays);
            arrays.remove(arrays.lastIndexOf(0));
        }

        if (t.right!=-1)
        {
            arrays.add(1);
            dfsTree(data[t.right],arrays);
            arrays.remove(arrays.lastIndexOf(1));
        }
    }

}

测试类如下:

package HuffmanTreeArrays;

public class TestHuffmanTree {
    public static void main(String[] args)
    {
        HuffmanTree hf = new HuffmanTree(7);
        hf.buildHuffmanTree();
        hf.codeHuffmanTree();
    }
}

以上是关于Java实现Huffman哈夫曼树(数组实现)的主要内容,如果未能解决你的问题,请参考以下文章

Huffman树及其编码(STL array实现)

学习数据结构笔记(10) --- [赫夫曼树(Huffman Tree)与赫夫曼编码(Huffman coding)]

Java- 哈夫曼(Huffman)压缩算法

构造Huffman以及实现

Huffman树的构造及编码与译码的实现

h5房卡源码C语言实现哈夫曼树编码解码及问题总结