0

I'm trying to implement a binary tree, but its just not taking the root. Any ideas? It looks like the root should be inserting fine, but I'm just getting a null when I print it. Am I trying to add only temporary nodes to the tree that don't "stick"?

public class tree {
    public static void main(String args[]){
        Treeb tree = new Treeb();
        tree.add(10);
        tree.add(20);
        tree.add(2);
        tree.add(6);
        tree.printTree();
    }
}
class Node{
    int data;
    Node left;
    Node right;
    public Node(int data){
        this.data = data;
        left = null;
        right = null;
    }
    Node getLeft(){
        return left;
    }
    Node getRight(){
        return right;
    }
}

class Treeb{
    Node root;
    Treeb(){
        root = null;
    }

    void add(int n){
        addNode(n, root);
    }

    void addNode(int n, Node vert){
        if(vert == null){
            vert = new Node(n);
        }
        else if(vert.left.data < n){
            if(vert.left == null){
                vert.left = new Node(n);
            }
            else{
                addNode(n, vert.left);
            }
        }
        else if(vert.right.data >= n){
            if(vert.right == null){
                vert.right = new Node(n);
            }
            else{
                addNode(n,vert.right);
            }
        }
    }
    void printTree(){
        if(root != null){
            printChild(root);
        }
        System.out.println(root);
    }
    void printChild(Node leaf){
        System.out.print(leaf.data);
        if(leaf.left != null){
            printChild(leaf.getLeft());
        }
        if(leaf.right != null){
            printChild(leaf.getRight());
        }
    }
}
1
  • 2
    Hi, two (small) remarks beside the answers: class names begin with capital letter and it should be nice if you sometimes used modifiers such as private. Furthermore, when you create getters ( getLeft() and getRight()) it is always a good idea to use them instead of instance variables. (This, of course, was made possible because you didn't use 'private' modifier.) Commented May 7, 2013 at 7:42

3 Answers 3

4

You are assigning vert a new reference, but not root, that's why it stays null.

Sign up to request clarification or add additional context in comments.

Comments

3

Your method addNode(int, Node) isn't working.

First:

if(vert == null){
    vert = new Node(n);
}

You're assigning the new node to a local variable. Thus, the new Node gets discarded at the end of the method.

Second:

}
else if(vert.left.data < n){
    // code
}
else if(vert.right.data >= n){

vert.left and vert.rightcan be null, so you would get an NPE when vert is not null.

Comments

3

getLeft() and getRight() can (and will) return null sometime. You should make sure in your printChild() that leaf itself is not null. (You're probably getting NPE in if(leaf.left != null) since leaf is null), you might also want to reconsider your tree construction again, root is null in your case.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.