-1
我想實現二叉樹,但是我在傳遞方法時遇到困難。這是我的代碼: 有兩個單獨的類:xyz
和BTree
。 BTree
的方法正在使用xyz
的方法test
。在不使用預定義函數的情況下實現二叉樹
class xyz{ //main class
public static void main (String args[]){
xyz obj = new xyz();
obj.test(n); //takes input (int) from user
}
public void test(int n){
BTree p = new BTree();
int d1 = p.depth( //I want ot pass a node here);
//My question: How to pass an argument here as "Node" to be received properly by the method??
....
....
}
}
class BT Tree{ //another different class
private Node root;
private Node node;
private int size;
public static class Node {
Node left;
Node right;
Node back;
int data;
int index;
Node(int newindex) {
left = null;
right = null;
back= null;
data = 0;
index = newindex;
}
}
public void BTree() { //constructor
root = null;
}
public int depth(Node node){ //Node pass will be correctly executed here
if (node.index==root.index)
return 0;
else
return 1+depth(parent(node));
}
}
我的問題是:如何通過depth()方法傳遞一個節點?
爲什麼不'depth'和'parent'方法*裏面*'Node'?他們爲什麼應該在'BTree'? (PS在二叉樹和[B-tree](http://en.wikipedia.org/wiki/B-tree)之間有區別) – oldrinb
我相信你需要一個公共的無參數'depth()'方法這將調用私有'深度(節點節點)'方法以根節點爲參數。如前所述,每個節點都應該跟蹤它的深度。 –