0

I constructed a tree in which i am simply traversing the inserted values, but the output contains an extra 0 due to variable initialisation. If variable initialisation is removed then the program gets termainated.

The output for the following is 51 15 5 0

public class TreeImplementation {

    public static class TreeDS{
        TreeDS left;
        TreeDS right;
        int data;
    }

    public static TreeDS root = new TreeDS();

    public static TreeDS insertInTree(TreeDS node,int value) {
        if(node==null) {
            node=new TreeDS();
            node.data=value;
            node.left=null;
            node.right=null;
            return node;
        }
        if(node.data>value) {
            node.left=insertInTree(node.left,value);
        }
        else if(node.data<value) {
            node.right=insertInTree(node.right,value);
        }

        return node;

    }

    public static void main(String[] args) {
        insertInTree(root, 5);
        insertInTree(root, 15);
        insertInTree(root, 51);
        inorder(root);

    }

    private static void inorder(TreeDS node) {
                if(node!=null) {
                    inorder(node.left);
                    System.out.print(" "+node.data);
                    inorder(node.right);
                }
    }

}

What is the issue with this code?

Peter Hull
  • 6,683
  • 4
  • 39
  • 48
anshul goel
  • 78
  • 1
  • 10
  • count how many nodes you have after inserting the first three... that `data` has a default value of zero... – Eugene Sep 26 '18 at 14:07

1 Answers1

0

The 0 comes from the initial value of root.

insertInTree takes the old root and returns the new root, so you need something like

    TreeDS root = null;
    root = insertInTree(root, 5);
    root = insertInTree(root, 15);
    root = insertInTree(root, 51);
    inorder(root);
Peter Hull
  • 6,683
  • 4
  • 39
  • 48
  • i tried this approach also but the program gets terminated after doing this – anshul goel Sep 26 '18 at 14:27
  • anshul, it worked for me. Does it give you any error message when it terminates? – Peter Hull Sep 26 '18 at 14:29
  • no it simply says TreeImplementation [Java Application] – anshul goel Sep 26 '18 at 14:30
  • Are you using Eclipse? See this link, is this the same as what you are seeing: https://stackoverflow.com/questions/14410356/console-shows-terminated-message-in-eclipse – Peter Hull Sep 26 '18 at 14:39
  • The error in eclipse is this only but in the direct text editor also its not working.Its giving a blank output – anshul goel Sep 26 '18 at 14:50
  • Are you sure that ` TreeImplementation [Java Application]` implies error condition ? It may well be normal end-of program. Try running only a simple print out in main (`System.out.println("hello world");`) to see if you get the same termination message. – c0der Sep 26 '18 at 15:04
  • I can't see how that could happen. Can you double check that the code you posted above exactly gives the output you posted above (I don't think it does). Then try changing it line-by-line until you go from wrong output to blank output. – Peter Hull Sep 26 '18 at 15:06