Flask接口签名sign原理与实例代码浅析
201
2023-07-10
java哈夫曼树实例代码
本文实例为大家分享了哈夫曼树java代码,供大家参考,具体内容如下
package boom;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Queue;
class Node
private T data;
private int weight;
private Node
private Node
public Node (T data,int weight){
this.data = data;
this.weight = weight;
}
public int compareTo(Node
if(this.weight > other.getWeight()){
return -1;
}if(this.weight < other.getWeight()){
return 1;
}
return 0;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public Node
return left;
}
public void setLeft(Node
this.left = left;
}
public Node
return right;
}
public void setRight(Node
this.right = right;
}
public String toString(){
return "data:"+this.data+";weight:"+this.weight;
}
}
public class huffuman
static
while(nodes.size()>1){
Collections.sort(nodes);
Node
Node
Node
parent.setRight(right);
parent.setLeft(left);
nodes.remove(left);
nodes.remove(right);
nodes.add(parent);
}
return nodes.get(0);
}
static
List
Queue<Node
queue.offer(root);
while(queue.size()>0){
Node
list.add(out);
if(out.getLeft()!=null){
queue.offer(out.getLeft());
}
if(out.getRight()!=null){
queue.offer(out.getRight());
}
}
return list;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
List
list.add(new Node
list.add(new Node
list.add(new Node
list.add(new Node
Node
System.out.println(huffuman.breadth(root));
// System.out.println(list);
}
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~