0

The root is always null.

This isn't homework, just trying to expand my knowledge. I'm attempting to recursively insert nodes into a Binary Tree. I've done some searching here but each implementation is so different, those changes aren't working.

This is my BinaryTree class:

public class BinaryTree {

    static Node root;

    public static void insertNode(int data){

        if(root != null){

            root.printData();
            insertNew(root, new Node(data));

        }else{

            root = insertNew(root, new Node(data));

        }

    }


    private static Node insertNew(Node current, Node n){


        if(current == null){

            return n;


        }else if(current.data > n.data){

            current.left = insertNew(current.left, n);
            return current;

        }else if(current.data < n.data){

            current.right = insertNew(current.right, n);
            return current;
        }

        return current;

    }

    public static void main(String[] args){


        insertNode(9);

    }


}

And this is my Node:

class Node {

    int data;
    Node left;
    Node right;

    Node(int data){

        this.data = data;

    }

    public int printData(){

        System.out.println(this.data);
        return this.data;
    }
}

Every time I run this, it assumes the root is null. I've got debug print lines in my working code so I can tell where I'm at in the methods. It hits that first if in the insertNew() function every time.

I'd like to understand conceptually where I'm failing.

6
  • 1
    @ChiefTwoPencils It's more for knowledge sake than anything else. I do the check to ensure root has a value. Which is why it's killing me that it never does. After the first call to insertNode(), root should always have a value. Commented Jan 29, 2016 at 22:19
  • 2
    @Phoenix This works when I run it. However you do need to insert another node before the root is not null. For example add insertNode(10) to your main method. Java does not save values or data structures in-between runs. Commented Jan 29, 2016 at 22:35
  • Okay, I see that now. Perhaps I don't understand Trees enough. So would I be unable to add 10 nodes in one execution and then traverse the tree in another? Trees only remain in memory for the duration of that single execution? Commented Jan 29, 2016 at 22:41
  • @Phoenix this is an issue with understanding Java. Each execution is completely independent. Absolutely nothing is transferred from one execution to the next unless you store it somewhere more permanent, such as a file on your computer. Commented Jan 29, 2016 at 22:44
  • 1
    It's not only about trees. Every time you run your program, it initializes all the required variables and structures in RAM. When program is done, all the data is gone. Moreover, garbage collector clears all the memory that has no pointers to it, and data is also erased. To save the data, program should write it to somewhere (drive or database). Then the data is available to be red after program restart. Commented Jan 29, 2016 at 22:48

1 Answer 1

1

May be you don't know that each time you run your program, it initialize all the data in RAM from scratch? In another case, may be your mistake is that you suppose that root.data will be printed after first insertion. You should do at least 2 insertions, to make your code print something, because on first insertion the root is always null.

You could easily find out the problem if tried to debug:

enter image description here

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

1 Comment

Thanks for the answer, it looks like I just didn't understand how Java holds objects in memory, and I'm still kind of hazy on the practical applications of BST outside of academia.

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.