二叉树小组项目。为什么我的根仍然是空的? [不重复]

时间:2016-07-07 18:18:43

标签: java null binary-tree

public void addNodes(int[] keys){
        //TODO
            add(keys, root);
    }

    private void add(int[] keys, Node auxRoot){
        if(auxRoot == null){
            System.out.println("Root " + keys[0]);
            auxRoot = new Node(keys[0]);
            int[] newKeys = new int[keys.length-1];
            for(int i =  0; i < keys.length-1; i++){
                newKeys[i] = keys[i+1];
            }
            add(newKeys, auxRoot);
        } else if(auxRoot != null && keys[0] < auxRoot.key){
            System.out.println("Left " + keys[0]);
            if(auxRoot.left == null){
                auxRoot.left = new Node(keys[0]);
                if (keys.length > 1) {
                    int[] newKeys = new int[keys.length - 1];
                    for (int i = 0; i < keys.length - 1; i++) {
                        newKeys[i] = keys[i + 1];
                    }
                    add(newKeys, auxRoot);
                }
            } else if(auxRoot.left != null){
                add(keys, auxRoot.left);
            }

        } else if(auxRoot != null && keys[0] > auxRoot.key){
            System.out.println("Right " + keys[0]);
            if(auxRoot.right == null){
                auxRoot.right = new Node(keys[0]);
                if (keys.length > 1) {
                    int[] newKeys = new int[keys.length - 1];
                    for (int i = 0; i < keys.length - 1; i++) {
                        newKeys[i] = keys[i + 1];
                    }
                    add(newKeys, auxRoot);
                }
            } else if(auxRoot.right != null){
                System.out.println("keep going");
                add(keys, auxRoot.right);
            }
        }
    }

我为addNode添加了辅助方法,出于某种原因,当我打印出root的键时,它仍为null。为什么会这样?我将根传递给add辅助方法。我理解的是传值。如何更改此方法以使root具有值。

0 个答案:

没有答案