📄 huffman.txt
字号:
TNode.java
public class TNode {
protected int w;
TNode lChild = null;
TNode rChild = null;
String huffCode = null;
public TNode(int w,TNode l,TNode r) {
this.w = w;
lChild = l;
rChild = r;
}
public TNode(int w)
{ this.w = w;
}
public boolean isLeaf()
{
return (rChild==null && lChild == null);
}
public int getWeight()
{ return w;
}
public TNode leftChild()
{ return lChild;
}
public TNode rightChild()
{ return rChild;
}
public void setHuffCode(String str)
{ huffCode = str;
}
public String getHuffCode()
{ return huffCode;
}
}
Tree.java
import java.util.*;
class Tree
{
protected TNode root;
protected List<Integer> leafWList = new ArrayList<Integer>();
protected List<TNode> tmpList = new LinkedList<TNode>();
protected TNode[] leafArr = null;
public void getLeafWeight()
{
Scanner scan = new Scanner(System.in);
System.out.println("请输入各叶子节点的权值,0为结束:");
while(scan.hasNextInt())
{
int i = scan.nextInt();
if(i==0)
break;
leafWList.add(new Integer(i));
}
scan = null;
return ;
}
public TNode min()
{
Iterator<TNode> itr = tmpList.iterator();
TNode minNode = itr.next();
int min = minNode.getWeight();
//找到最小的节点
TNode tmpNode;
while(itr.hasNext())
{
tmpNode = itr.next();
if(tmpNode.getWeight()<min)
{
min = tmpNode.getWeight();
minNode = tmpNode;
}
}
//最小的节点移出临时队列
tmpList.remove(minNode);
//处理垃圾
itr = null;
tmpNode = null;
return minNode;
}
/**
* 根据权值创建叶子节点并加入临时队列
*
*/
public void makeLeafNode()
{
leafArr = new TNode[leafWList.size()];
for(int i=0;i<leafWList.size();i++)
{
TNode node = new TNode(leafWList.get(i).intValue());
leafArr[i] = node;
tmpList.add(node);
}
}
/**
* 根据权值构造最优的二叉树
*
*/
public void makeBestTree()
{
//根据权值创建叶子节点并加入临时队列
makeLeafNode();
TNode minNode1 = null,minNode2 = null;
TNode node = null;
//构造最优树
while(tmpList.size()!=1)
{
minNode1 = min();
minNode2 = min();
node = new TNode(minNode1.getWeight()+minNode2.getWeight(),minNode1,minNode2);
tmpList.add(node);
}
root = node;
}
/**
* 先序遍历的递归调用
*
*/
protected void preT(TNode t)
{
if(t.isLeaf())
{
System.out.print(t.getWeight() + " ");
return ;
}
else
{
System.out.print(t.getWeight() + " ");
preT(t.lChild);
preT(t.rChild);
}
}
/**
* 先序遍历最优二叉树
*
*/
public void preOrderTraverse()
{
preT(root);
}
public static void main(String [] args)
{
Tree t = new Tree();
t.getLeafWeight();
t.makeBestTree();
t.preOrderTraverse();
}
}
HuffmanCode.java
public class HuffmanCode extends Tree
{
public HuffmanCode()
{
init();
}
/**
* 初始化节点值并构造最优二叉树
*
*/
public void init()
{
super.getLeafWeight();
super.makeBestTree();
}
/**
* 生成赫夫曼编码的递归函数
*
* @param t TNode 当前遍历节点
* @param s String 目前遍历至此的赫夫曼编码
*/
protected void hufT(TNode t,String s)
{
if(t.isLeaf())
{
t.setHuffCode(s);
}
else
{
hufT(t.lChild,s+"0");
hufT(t.rChild,s+"1");
}
}
/**
* 生成赫夫曼编码的外部调用函数
*
*/
public void makeHuffCode()
{
hufT(root,"");
}
/**
* 查看所有的赫夫曼编码值
*
*/
public void viewHuffCode()
{String S= new String();
int L=S.length();
float p;
int t,n;
t=0;n=0;
for(int i=0;i<leafArr.length;i++)
{ S=leafArr[i].getHuffCode();
L=S.length();
t=leafArr[i].w*L+t;
n=leafArr[i].w+n;
System.out.println(leafArr[i].w + ": " +S+" 编码长度为: "+ L);
}
p=(float)t/n;
System.out.println("最短编码长度为:"+t+"/"+n+"="+p);
}
public static void main(String [] args)
{
HuffmanCode hc = new HuffmanCode();
hc.makeHuffCode();
hc.viewHuffCode();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -