huffman java_Huffman 压缩和解压缩java实现

for(int i=0;i

//w1 : the first min weight w2: the second min weight

//i1 : the first min weight index, i2: the second min weight index

int w1 = MAX_VALUE, w2=MAX_VALUE;

int i1 = 0, i2 = 0;

// find the two node with the minimum weight

for(int j=0;j

HuffmanNode node = tree.get(j);

if(node.getWeight()< w1 && node.getParent()==-1){

w2 = w1;

w1 = node.getWeight();

i2 = i1;

i1 = j;

}

else if(node.getWeight()

w2 = node.getWeight();

i2 = j;

}

}

//set the two node to be the children of a new node, and add the new node to the tree

HuffmanNode pNode = new HuffmanNode(w1+w2);

pNode.setlChild(i1);

pNode.setrChild(i2);

tree.add(pNode);

tree.get(i1).setParent(tree.indexOf(pNode));

tree.get(i2).setParent(tree.indexOf(pNode));}

3.2   依据Huffman 树获得Huffman编码

从叶子节点開始网上遍历Huffman树。直到到达根节点。依据当前节点为其父节点的左儿子还是右儿子确定这一位值是0还是1。

最后将依次获得的0,1字符串反转获得Huffman编码。

代码:

for(int i=0;i

HuffmanNode node = tree.get(i);

HuffmanNode pNode = tree.get(node.getParent());

String code ="";

while(true){

if(pNode.getlChild()==tree.indexOf(node)){

code = "0"+code;

}

else if(pN

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值