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 7549889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:04:15+00:00 2026-05-30T10:04:15+00:00

I am developing a binary search tree in java. But i am facing certain

  • 0

I am developing a binary search tree in java. But i am facing certain difficulties in it. Here is the code

class Node {

    Node left, right;
    Integer data;

    Node(Integer d, Node left, Node right) {
        this.data = d;
        this.left = left;
        this.right = right;
    }
}

class BinaryTree {

    Node root;

    public BinaryTree(Node root) {
        this.root = root;
    }

    void insert(int d)
    {
        if(root==null)
            root= new Node(d, null, null);
        insert(root,d);
    }
    void insert(Node root, int d) {
        if (root == null) {
            root=new Node(d,null,null);
        } else if (d > root.data) {
            insert(root.right, d);
        } else if (d < root.data) {
            insert(root.left, d);
        }
    }

    void inorder(Node root) {
        if (root != null) {
            inorder(root.left);
            System.out.println(root.data);
            inorder(root.right);
        }
    }
}

public class BST {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str = null;
        BinaryTree bt=new BinaryTree(null);
        while (!(str = br.readLine()).equalsIgnoreCase("0")) {
            bt.insert(Integer.parseInt(str));
        }
        bt.inorder(bt.root);
    }
}

The problem here i am facing is as in java there is only pass by value. I am getting the root as null in every case except the first case in which i have passed the newly created root into it. Here when i am making a recursive call to the insert function by passing the value of either left or right of the root then in the new call the new root has been created if required for it but when the function gets over it’s values are not reflected to the caller function’s variable.
In short the problem is due to the call by value being followed by the java.

Can anyone please suggest the solution for this problem?

  • 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-05-30T10:04:16+00:00Added an answer on May 30, 2026 at 10:04 am

    Your calls to insert(root.right/left, d) do not change the original right/left nodes if they are null, but simply make the method arguments point to a new variable (which, as you noticed, in Java won’t change the original reference). Your change to the first root works because you call a different method, insert(int).

    Have you considered making left and right BinaryTrees instead of Nodes? Also, instead of using “null”, consider having an “empty” BinaryTree (with a null root and an isEmpty method).

    Note that conceptually, left and right are trees, not nodes, so the design will be cleaner.


    Example code. Untested but the idea should be right:

    class Node {
    
        BinaryTree left, right;
        Integer data;
    
        Node(Integer d, BinaryTree left, BinaryTree right) {
            this.data  = d;
            this.left  = left;
            this.right = right;
        }
    } 
    
    class BinaryTree {
    
        Node root;
    
        // Empty tree
        BinaryTree() {
            this(null);
        }
    
        BinaryTree(Node root) {
            this.root == root;
        }
    
        void insert(int d) {
    
            if (this.root == null) {
    
                // The tree was empty, so it creates a new root with empty subtrees
                this.root = new Node(d, new BinaryTree(), new BinaryTree());
    
            } else if (d > this.root.data) {
                this.root.right.insert(d);
            } else if (d < this.root.data) {
                this.root.left.insert(d);
            }
        }
    }
    

    Notes:

    • I respected the style of your existing code.
    • This implementation will skip repeated elements.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the situation, I am developing a binary search tree and in each
I'm developing a Java Applet, and reducing the size of the binary code will
I am developing a full text search engine for indexing popular binary formats. I
I am developing a TCP/IP client that has to deal with a proprietary binary
Developing websites are time-consuming. To improve productivity, I would code a prototype to show
Developing a heavily XML-based Java-application, I recently encountered an interesting problem on Ubuntu Linux.
Developing server side code i finally got my eyes X-crossed trying to write -
Possible Duplicate: Why can't decimal numbers be represented exactly in binary? I am developing
My current problem is that I'm developing a embedded system that uses a binary
I am developing an application and I would like to be able to search

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.