移动Java引用

时间:2016-04-11 15:46:04

标签: java

所以我通过递归创建了一个二叉树,当我插入一个新节点时,我想跳回到root再次从头开始插入。要做到这一点,我遵循这个惯例。

BinaryTree node = new BinaryTree();
node = this;
node = root; // this is where I want to get to

因此,当我将其编码并试图将$ this变量设置为root时,它没有设置,如图所示。 enter image description here 为什么'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);
            }
        }
    }

0 个答案:

没有答案