我想实现一个二叉树,但我在传递一个方法时遇到了困难。这是我的代码:有两个单独的类:xyz
和BTree
. 的方法BTree
在xyz
's 方法中使用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() 方法传递节点?