Java实现Huffman哈夫曼树
Posted Wecccccccc
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java实现Huffman哈夫曼树相关的知识,希望对你有一定的参考价值。
代码如下:
package HuffmanTree;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class HuffmanTree {
private class TreeNode
{
private int val;
private TreeNode left;
private TreeNode right;
public TreeNode()
{
val = 0;
left = right = null;
}
public TreeNode(int e)
{
val = e;
left = right = null;
}
}
private TreeNode data[];
private int size;
private TreeNode TreeRoot;
public HuffmanTree(int n)
{
TreeRoot = null;
data = new TreeNode[n+1];
Scanner sc = new Scanner(System.in);
for (int i = 1;i<=n;i++)
{
TreeNode t = new TreeNode(sc.nextInt());
data[i] = t;
size++;
}
for (int i = size/2;i>0;i--)
{
percDown(i);
}
}
private void percDown(int p)
{
TreeNode x = data[p];
int parent,child;
for (parent = p;parent*2 <=size;parent = child)
{
child = 2*parent;
if (child!=size && data[child].val > data[child+1].val)
{
child++;
}
if (x.val <= data[child].val) break;
else data[parent] = data[child];
}
data[parent] = x;
}
private TreeNode deleteMin()
{
TreeNode minData = data[1];
TreeNode x = data[size--];
int parent,child;
for (parent = 1;parent*2<=size;parent = child)
{
child = 2*parent;
if (child!=size && data[child].val > data[child+1].val)
{
child++;
}
if (x.val <= data[child].val) break;
else data[parent] = data[child];
}
data[parent] = x;
return minData;
}
private void insertHeap(TreeNode e)
{
int i = ++size;
for (;i>1 && data[i/2].val > e.val ;i = i/2)
{
data[i] = data[i/2];
}
data[i] = e;
}
public void buildHuffmanTree()
{
TreeNode t;
int cnt = size;
for (int i = 1;i<cnt;i++)
{
t = new TreeNode();
t.left = deleteMin();
t.right = deleteMin();
t.val = t.left.val+t.right.val;
insertHeap(t);
}
TreeRoot = deleteMin();
}
private void printNode(TreeNode e)
{
System.out.print(e.val+" ");
if (e.left!=null) System.out.print("left = "+e.left.val+" ");
else System.out.print("left = null ");
if (e.right!=null) System.out.print("right = "+e.right.val+" ");
else System.out.print("right = null ");
System.out.println();
}
public void levelOrder()
{
Queue<TreeNode> q = new LinkedList<>();
q.add(TreeRoot);
while(!q.isEmpty())
{
TreeNode t = q.poll();
printNode(t);
if (t.left!=null) q.add(t.left);
if (t.right!=null) q.add(t.right);
}
}
public void codeHuffmanTree()
{
ArrayList<Integer> arrays = new ArrayList<>();
if (TreeRoot==null)
{
System.out.println(arrays);
return ;
}
dfsNode(TreeRoot,arrays);
}
private void dfsNode(TreeNode root,ArrayList<Integer> arrays)
{
if (root.left==null && root.right==null)
{
System.out.println(root.val+" = "+arrays);
return ;
}
if (root.left!=null)
{
arrays.add(0);
dfsNode(root.left,arrays);
arrays.remove(arrays.lastIndexOf(0));
}
if (root.right!=null)
{
arrays.add(1);
dfsNode(root.right,arrays);
arrays.remove(arrays.lastIndexOf(1));
}
}
}
测试类如下:
package HuffmanTree;
public class TestHuffmanTree {
public static void main(String[] args)
{
HuffmanTree hf = new HuffmanTree(7);
hf.buildHuffmanTree();
hf.levelOrder();
hf.codeHuffmanTree();
}
}
以上是关于Java实现Huffman哈夫曼树的主要内容,如果未能解决你的问题,请参考以下文章