?? 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 + -