围绕Java引用移动
因此,我通过递归创建了一个二叉树,当我插入一个新节点时,我想跳回到根,重新开始插入。为了做到这一点,我遵循这个惯例围绕Java引用移动,java,Java,因此,我通过递归创建了一个二叉树,当我插入一个新节点时,我想跳回到根,重新开始插入。为了做到这一点,我遵循这个惯例 BinaryTree node = new BinaryTree(); node = this; node = root; // this is where I want to get to 因此,当我将其编码出来并试图将$this变量设置为root时,它没有设置,如图所示。 为什么“this”不设置为root?我怎样才能跳到树顶上 public void addNode(Li
BinaryTree node = new BinaryTree();
node = this;
node = root; // this is where I want to get to
因此,当我将其编码出来并试图将$this变量设置为root时,它没有设置,如图所示。
为什么“this”不设置为root?我怎样才能跳到树顶上
public void addNode(List teams, BinaryTree tree, BinaryTree root){
while(teams.size() > 1){
print(root);
//Half's the list
halfA = teams.subList(0, teams.size() / 2);
halfB = teams.subList(teams.size() / 2, teams.size());
if(parent == null){
left = new BinaryTree(halfA);
left.parent = this;
right = new BinaryTree(halfB);
right.parent = this;
}
if(countChildren(tree.left) >= countChildren(tree.right)) {
if (right == null) {
setRight(new BinaryTree(halfB));
BinaryTree temp = new BinaryTree();
parent = this;
temp = this;
temp = tree;
} else if (right != null)
right.addNode(halfB, tree, root);
}
if(countChildren(tree.right) >= countChildren(tree.left)) {
if (left == null) {
setLeft(new BinaryTree(halfA));
BinaryTree temp = new BinaryTree();
parent = this;
temp = this;
temp = tree;
} else if (left != null)
left.addNode(halfA, tree, root);
}
}
}
你能将你的完整方法复制到问题中而不是使用截图吗?不要截图你的代码。。。只需发布即可。
此
无法重新分配。如果您执行node=this代码>后跟节点=其他内容代码>第一个赋值没有意义。这就像说“让x为一,实际上,让x为二”。