2016-04-06 57 views
-1

我創建了一個huffman樹,現在我需要遍歷huffman樹來解碼消息。我正在編寫遍歷huffman樹的方法,並且我無法訪問我的樹的當前節點,即使我將它傳遞給了我的樹。任何幫助將不勝感激 - 粗魯的評論不是。無法從樹訪問根

 //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

該樹是通過分析二進制字符的頻率而創建的。我不相信顯示所有能夠找到頻率的代碼並創建樹等是相關的。遍歷樹的方法是在代碼底部的findLetter()方法中。 –

回答

1
Node.current = root; 

沒有在Node類名爲current成員。即使它存在current = root是分配current的代碼。