0

I'm currently solving this binary tree(not binary search tree) problem for my data structure class. However, when I tried to print the tree from root, debugging shows that root it still null even through I initialized my tree

public class Node {

    int integerValue = 0;
    public Node leftNode = null;
    public Node rightNode = null;

    public Node (int inputInt){
        this.integerValue = inputInt;
    }
}

insert the array element in the tree knowing there will be no delete or add

public class BinaryTree {
    public void initializeTree(int[]string, int length, int currentPosition, Node currentNode){
        if(currentPosition < length){
            Node newNode = new Node(string[currentPosition]);
            currentNode = newNode;
            initializeTree(string,length, 2*currentPosition +1, currentNode.leftNode);
            initializeTree(string,length, 2*currentPosition +2, currentNode.rightNode);
        }
    }

    public void printTree(Node root){
        if(root != null){
            System.out.print(root.integerValue + " ");
            printTree(root.leftNode);
            printTree(root.rightNode);
        }
    }
}

public class Main {
    public static void main(String[] args) {
        int [] array = {0,1,2};
        ArrayTree tree = new ArrayTree();
        BinaryTree bTree = new BinaryTree();
        Node root = null;
        Node currentNode = root;

        bTree.initializeTree(array, 3, 0, currentNode);
        bTree.printTree(root);
    }
}

1 Answer 1

0

When you pass the currentNode argument to the initializer you're passing a reference (pointer) of the object, in this case null. When in the method you reassign the variable:

currentNode = newNode;

Now currentNode references a new Node instance, but your currentNode variable on the Main class isn't updated, so it will remain null.

I recommend you to use a constructor for the BinaryTree class instead of an "initializer" method. Also the length parameter is not necessary (string.length does the same thing).

Finally you could unify the Node and the BinaryTree in one class.

public class BinaryTree {
    Integer integerValue;
    BinaryTree left, right;

    public BinaryTree(int[] string, int currentPosition) {
        if (currentPosition < string.length){
            this.integerValue = string[currentPosition];
            this.left = new BinaryTree(string, 2 * currentPosition + 1);
            this.right = new BinaryTree(string, 2 * currentPosition + 2);
        }
    }

    public void printTree() {
        if (this.integerValue != null){
            System.out.print(this.integerValue + " ");
            this.left.printTree();
            this.right.printTree();
        }
    }
}

And the Main class:

public class Main {
    public static void main(String[] args) {
        int [] array = {0, 1, 2};
        BinaryTree root = new BinaryTree(array, 0);
        root.printTree();
    }
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.