这是一段没有写完的哈夫曼树压缩解压缩文件的代码,但是其中的PriorityBlockingQueue数据结构中的comparator接口出问题了。。。
package Controller.Huffman_tree;
import javax.swing.tree.TreeNode;
import java.util.Comparator;
import java.util.Hashtable;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.PriorityBlockingQueue;
public class buildTree {
public static class TreeNode {
int weight;
Byte data;
TreeNode lClild;
TreeNode rChild;
public void setWeight(int weight) {
this.weight = weight;
}
public void setData(Byte data) {
this.data = data;
}
public TreeNode() {
}
public TreeNode(int weight, Byte data) {
this.weight = weight;
this.data = data;
}
@Override
public String toString() {
return "TreeNode{" +
"weight=" + weight +
", data=" + data.toString() +
", lClild=" + lClild +
", rChild=" + rChild +
'}';
}
}
public static Queue<TreeNode> pq;
public static Byte [] bytes;
public static void main(){
}
public static void creatingLeafs(){
Map<Byte,Integer> map=new Hashtable<>();
pq=new PriorityBlockingQueue<TreeNode>(101,
new Comparator<TreeNode>() {
@Override
public int compare(TreeNode o1, TreeNode o2) {
return o1.weight - o2.weight;
}
}
);
for(Byte b:bytes){
if(map.containsKey(b)){
map.put(b,map.get(b)+1);
}
else{
map.put(b,1);
}
for(Map.Entry<Byte,Integer> it:map.entrySet()){
TreeNode node=new TreeNode(it.getValue(),it.getKey());
pq.add(node);
}
}
}
public static TreeNode buildTree(){
while(pq.size()>1){
TreeNode new_node=new TreeNode();
}
}
}