简体   繁体   中英

Java Binary Search Tree implementation problem.!

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?

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 BinaryTree s instead of Node s? 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.

Suggestions,

  • I wouldn't use an Integer if you mean to use an int value.
  • If you are reproducing code which is in the JVM already, I would read how the code works there first (and copy what you need)
  • When I have a bug in my code, I use the debugger to work out what is going wrong.
  • I start with a the simplest unit I can make which shows the problem, and fixes that simple situation.

I would post the simplest unit test, which anyone can reproduce, and what you see in the debugger here if it doesn't make any sense.

This doesn't really answer your question, but is too long for a comment. ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM