霍夫曼 java_霍夫曼树,霍夫曼编码 的java实现

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

分享到:

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

2012-11-08 17:29

浏览 4553

评论

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值