Java 无法从目录树访问根目录
我已经创建了一个哈夫曼树,现在需要遍历哈夫曼树来解码消息。我正在编写遍历哈夫曼树的方法,并且我无法访问树的当前节点,即使我将其传递给了我的树。任何帮助都将不胜感激——粗鲁的评论是不可取的Java 无法从目录树访问根目录,java,tree,nodes,traversal,huffman-code,Java,Tree,Nodes,Traversal,Huffman Code,我已经创建了一个哈夫曼树,现在需要遍历哈夫曼树来解码消息。我正在编写遍历哈夫曼树的方法,并且我无法访问树的当前节点,即使我将其传递给了我的树。任何帮助都将不胜感激——粗鲁的评论是不可取的 //Code that creates huffman tree not shown //method to find traverse at very bottom public static class Tree implements Comparable<Tree>{
//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);
}
//未显示创建哈夫曼树的代码
//在最底部查找导线的方法
公共静态类树实现了可比较的{
节根;
公共树(树t1,树t2){
根=新节点();
root.left=t1.root;
root.right=t2.root;
root.weight=t1.root.weight+t2.root.weight;
}
公共树(整型权重,字符元素){
根=新节点(权重、元素);
}
@凌驾
公共整数比较(树t){
if(根重量
节点类中没有名为
current
的成员。即使存在current=root
也是分配current
的代码,树是通过分析二进制字符的频率创建的。我不相信显示所有查找频率的代码并创建树是相关的,树的遍历在代码底部的findLetter()方法中。
Node.current = root;