-1

In the following insert method for a binary search tree (BST), the left side of the tree is being updated correctly, but there is an issue with inserting values on the right side. Despite using the debugger to check the output, the method adds some values incorrectly to both the left and right sides. Can someone help identify and fix the issue? This got me confused as it is my first time exercise on a binary search tree question

//here is the code

public void insert(int number) {
        Node newNode = new Node(number);
        if (isEmpty()) {
            root = newNode;
        }
        Node current = root;
        Node parent;
        while (true) {
            parent = current;
            if (current.data > number) {
                current = current.left;
                if (current == null){
                    parent.left = newNode;
                    return;
                }
            }
            else{
                current = current.right;
                if(current == null){
                    parent.right = newNode;
                    return;
                }
            }
        }
    }
7
  • 1
    Please provide code that we can compile and run (without modification) and that reproduces the problem you mention. Commented Aug 13, 2024 at 21:59
  • 1
    And specific inputs and output, as well as expected output. Commented Aug 13, 2024 at 22:02
  • 1
    Specifically saying: "I tried to debug but I couldn't figure it out" isn't helpful. SO isn't some sort of gated homework situation where you cannot pass the gate unless you have debugged. The point of debugging is to tell us what we need to know to help you. This code does not compile on its own, you have provided no details about how or what you debugged and what wisdoms you gained from doing so. As written, I don't think this question is salvageable. if you still can't figure it out - try again, make a self contained test case... Commented Aug 13, 2024 at 23:04
  • 1
    explain which exact inputs you provided, what happend vs what you expected, what you did to attempt to debug it, what you learned, and how that surprised you. "I just cannot figure it out" does not work here - if you debugged, somewhere along the line some step resulted in an effect you were not expecting. Because otherwise you got to the end, and the answer is what you expected: That'd mean there is no problem at all. Commented Aug 13, 2024 at 23:05
  • To support what previous comments have said, I suggest these resources and pages they link to: Tour, How to Ask a Good Question, Other pages in the Help Center, and Minimal Reproducible Example Commented Aug 14, 2024 at 0:04

1 Answer 1

0

There are two points that need to be edited in the code block.

  • End Process If Tree is Empty: After the isEmpty() check, if the tree is empty, I added return statement after setting the new node as root. This prevents unnecessary looping.

  • Initializing the Parent Variable: Before the loop, I initialized the parent variable to null. This is important to ensure that the parent and current variables are updated correctly within the loop.

      public void insert(int number) {
          Node newNode = new Node(number);
    
          if (isEmpty()) {
              root = newNode;
              return; // End Process If Tree is Empty.
          }
    
          Node current = root;
          Node parent = null; // Initializing the Parent Variable
    
          while (true) {
              parent = current;
              if (current.data > number) {
                  current = current.left;
                  if (current == null) {
                      parent.left = newNode;
                      return;
                  }
              } else {
                  current = current.right;
                  if (current == null) {
                      parent.right = newNode;
                      return;
                  }
              }
          }
      }
    

Hope it was useful.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.