package com.tree;
import java.util.*;
/**
* yuyong 2012-11-2
*/
public class HuffmanTree {
Node treeRoot=null;
HuffCode huff=new HuffCode();
public HuffmanTree(SortedList values){
while(true){
Node first=values.get(0);
values.remove(0);
Node root=first;
if(values.size()<=0){
treeRoot=root;
break;
}
Node two=values.get(0);
if(two!=null){
values.remove(0);
root=new Node(null,first.weight+two.weight);
root.left=first;
root.right=two;
values.insertItem(root);
}
}
}
public static void main(String args[]) throws Exception{
long start=new Date().getTime();
SortedList list=new SortedList(SortedList.ASC);
for(int i=0;i<5;i++){
int value=(int)(Math.random()*100);
list.insertItem(new Node(value, value));
}
long end=new Date().getTime();
System.out.println("共耗时:"+(end-start));
System.out.println(list);
HuffmanTree ht=new HuffmanTree(list);
System.out.println("霍夫曼编码");
ht.show(ht.treeRoot,0);
}
void show(Node node,int c){
if(node.left!=null){
huff.put(c,0);
show(node.left, c + 1);
}
if(node.value!=null)
System.out.println("权重:"+node.weight+" 霍夫曼编码值:"+huff.toString().substring(0,c));
if(node.right!=null){
huff.put(c,1);
show(node.right, c + 1);
}
}
}
class Node{
Object value;
int weight;
Node left;
Node right;
public Node(Object value,int weight){
this.value=value;
this.weight=weight;
}
public boolean equals(Object obj){
Node node=(Node) obj;
if(node.weight==this.weight&&node.value==this.value)
return true;
return false;
}
}
class SortedList extends LinkedList {
public static String DESC="desc";
public static String ASC="asc";
public String sort;
public SortedList(String sort) throws Exception{
if(sort.equals(DESC))
this.sort=DESC;
else if(sort.equals(ASC))
this.sort=ASC;
else
throw new Exception("no support sort");
}
public void insertItem(T value){
int index=0;
if(this.size()>0){
int start=0;
int end=this.size()-1;
int step=0;
if(start!=end)
while((end-start)!=1){
step=(end-start)/2;
if(this.get(start+step).weight>value.weight){
end=end-step;
}else if(this.get(start+step).weight
start=start+step;
}else{
this.add(start+step,value);
return;
}
}
if(value.weight>=this.get(end).weight){
index=end+1;
}else if(value.weight<=this.get(start).weight){
index=start;
}else
index=end;
}
this.add(index,value);
}
public String toString(){
String str="[";
for(int j=0;j
str+=this.get(j).weight+",";
}
str=str.substring(0, str.length()-1);
str+="]";
return str;
}
}
class HuffCode extends HashMap{
public String toString(){
String str="";
for(int i=0;i
str+=this.get(i);
}
return str;
}
}
共耗时:0
[25,40,45,55,67]
霍夫曼编码
权重:45 霍夫曼编码值:00
权重:55 霍夫曼编码值:01
权重:25 霍夫曼编码值:100
权重:40 霍夫曼编码值:101
权重:67 霍夫曼编码值:11
分享到:
2012-11-08 17:29
浏览 4553
评论