Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8961117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:48:47+00:00 2026-06-15T15:48:47+00:00

I’m trying to write code for a binary search tree, the first method I’m

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T15:48:48+00:00Added an answer on June 15, 2026 at 3:48 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.