我创建了一个霍夫曼树,现在我需要遍历霍夫曼树来解码一条消息。我正在编写遍历霍夫曼树的方法,我无法访问我树的当前节点,即使我将它传递给了我的树。任何帮助将不胜感激 - 粗鲁的评论不是。
//Code that creates huffman tree not shown
//method to find traverse at very bottom
public static class Tree implements Comparable<Tree>{
Node root;
public Tree(Tree t1, Tree t2){
root = new Node();
root.left = t1.root;
root.right = t2.root;
root.weight = t1.root.weight + t2.root.weight;
}
public Tree(int weight, char element){
root = new Node(weight, element);
}
@Override
public int compareTo(Tree t){
if(root.weight < t.root.weight){
return 1;
}else if(root.weight == t.root.weight){
return 0;
}else
return -1;
}
public class Node{
char element;
int weight;
Node left;
Node right;
String code = "";
public Node(){
}
public Node(int weight, char element){
this.weight = weight;
this.element = element;
}
public void findLetter(Tree tree){
char letter;
Node.current = root; //Red lines involving everything with Node or current from here on
if(code[i] == 0){
if(current.left == null){
letter = current.element;
}else{
current = Node.left;
}
}else if(code[i] == 1){
if(current.right == null){
letter = current.element;
}else{
current = Node.right;
}
}
System.out.printf(""+ letter);
}
答案 0 :(得分:1)
Node.current = root;
Node类中没有名为current
的成员。即使它存在,current = root
也是分配current
的代码。