1

I'm trying to write code for a binary search tree, the first method I'm working on is the add (insert) method. The root seems to insert properly, but I'm getting null pointer exception when adding the second node. I'll indicate the exact problem spot in my code with comments.

If you can see how to fix the bugs, or let me know if my overall logic is flawed it would be incredibly helpful.-- I will mention that this is for school, so I'm not looking to make a really impressive model...most of my layout choices simply reflect the way we've been working in class. Also, method names were selected by the teacher and should stay the same. Feel free to edit the formatting, had a little trouble.

BINARY TREE CLASS

public class BinarySearchTree 
{   
private static Node root;

    public BinarySearchTree()
    {
        root = null;
    }

    public static void Add (Node newNode)
    {
        Node k = root;
        if (root == null)//-----------------IF TREE IS EMPTY -----------------
        {
            root = newNode;
        }

        else // -------TREE IS NOT EMPTY --------

        {
        if (newNode.value > k.value) //-------NEW NODE IS LARGER THAN ROOT---------
        {   
            boolean searching = true;

            while(searching) // SEARCH UNTIL K HAS A LARGER VALUE
            {   //***CODE FAILS HERE****
                if(k.value > newNode.value || k == null) 
                {
                    searching = false;
                }
                else {k = k.rightChild; }
            }

            if ( k == null) { k = newNode;} 
            else if (k.leftChild == null){ k.leftChild = newNode;} 
            else 
            {
                Node temp = k.leftChild;
                k.leftChild = newNode;
                newNode = k.leftChild;

                if(temp.value > newNode.value )
                {
                    newNode.rightChild = temp;
                }
                else
                {
                    newNode.leftChild = temp;
                }
            }

        }

        if (newNode.value < k.value) //-----IF NEW NODE IS SMALLER THAN ROOT---
        {   
            boolean searching = true;

            while(searching) // ----SEARCH UNTIL K HAS SMALLER VALUE
            {// **** CODE WILL PROBABLY FAIL HERE TOO ***
                if(k.value < newNode.value || k == null) {searching = false;}
                else {k = k.leftChild;} 
            }

            if ( k == null) { k = newNode;} 
            else if (k.rightChild == null){ k.rightChild = newNode;} 
            else 
            {
                Node temp = k.rightChild;
                k.rightChild = newNode;
                newNode = k.rightChild;

                if(temp.value > newNode.value )
                {
                    newNode.rightChild = temp;
                }
                else
                {
                    newNode.leftChild = temp;
                }
            }
        }       
           }} // sorry having formatting issues
}

NODE CLASS

public class Node 
{
    int value;
    Node leftChild;
    Node rightChild;

    public Node (int VALUE)
    {
                value = VALUE;
    }
}

TEST APPLICATION

public class TestIT
{

    public static void main(String[] args) 
    {

        BinarySearchTree tree1 = new BinarySearchTree();
        Node five = new Node(5);
        Node six = new Node(6);

        tree1.Add(five);
        tree1.Add(six);

        System.out.println("five value: " + five.value);
        System.out.println("five right: " + five.rightChild.value);
    }

}

1 Answer 1

1

The conditional statement is checked from left to right, so you need to check whether k is null before you check whether k.value > newNode.value because if k is null, then it doesn't have a value.

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

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.