2013-07-06 22 views
1

我試圖遞歸調用搜索或者使用搜索與根節點(這是BSTN),它給我的錯誤與子樹說錯誤的類型或調用getNode(錯誤說未定義類型BST)根節點這給了我一個錯誤以及指出我的錯誤類型調用從不同的類中的方法

package sizebst; 



public class sizeBST { 
sizeBSTN rootNode; 

public SizeBST(BSTN root){ 
    rootNode = root; 
} 



public boolean search(int target){ 
    //isn't complete but I want to recrusively search the tree calling methods from the other class 
      getNode(rootNode.LSubtree, target); 

} 

這是我想從調用getNode方法。

package sizebst; 


public class SizeBSTN { 
SizeBSTN LSubtree; 
SizeBSTN RSubtree; 
int data; 
int size; 


public SizeBSTN(int data){ 
    LSubtree = null; 
    RSubtree = null; 
    this.data = data; 
    size = 1; 
} 




public static SizeBSTN getNode(SizeBSTN node, int target){ 
// isn't working yet but it finds a node and returns it. 




    } 



} 

回答

0

由於getNodeSizeBSTNstatic方法嘗試

SizeBSTN.getNode(rootNode.LSubtree, target); 

代替

getNode(rootNode.LSubtree, target); 

另一種方式是靜態導入這種方法與

import static sizebst.SizeBSTN.getNode; 

現在你可以調用它沒有類的引用。

+0

我已經嘗試過,但現在,它的作品,當它沒有之前。你能解釋爲什麼> –

+0

@PaulthePirate它的工作原理是因爲所有靜態方法都是通過它的類調用的,如果你從它的類之外調用它們的話。我不知道爲什麼它以前不適合你。也許你沒有編譯SizeBSTN源代碼?也許你拼錯了一些東西。很難說。 – Pshemo

+0

@PaulthePirate你真的需要開始使用像Eclipse或Netbeans這樣的IDE,並自動縮進你的代碼。看看這裏http://pastebin.com/Hz7qhzUK。你會看到最後一個'else'沒有連接任何'if'。你也可以'''在'if'之後,這可能不是你想要的。 – Pshemo