Nyland Sidifall
Nyland Sidifall

Reputation: 33

Second opinion needed on Binary Search Tree Insert and Print

I hope you're doing well today. I need a second opinion with the implementation of this binary search tree with it's insert and print methods. I know I could have implemented this in such a way that may be a bit convoluted, but I wanted to know where I'm messing this up. When I run the program, it outputs the following:

Root value is: 3
Val inserted to left of root: 1
Val inserted at right node: 1
Root value is: 3
Val inserted at left node: 0
Root value is: 3
Val inserted to right of root: 4
Val inserted at right node: 4
Root value is: 3
Val inserted at right node: 5
0
1
1
3
4
4
5

I want this program to output the tree inOrder with the input of: 3,1,0,4,5.

Here is the code that I have now, and Thank You for the second set of eyes.

class tree {
    Node root = null;
    
    //Constructor
    tree(int value){
        root = new Node(value);
    }

    //insert
    void insertStart(int val){
        System.out.println("Root value is: "+root.value);
        //Check val against root & root has none children
        if(val < root.value && root.left == null){
            root.left = new Node(val);
            System.out.println("Val inserted to left of root: "+root.left.value);
        }

        if(val > root.value && root.right == null){
            root.right = new Node(val);
            System.out.println("Val inserted to right of root: "+root.right.value);
        }

        //Check val against root & root has two children
        if(val < root.value){
            //insertRecursion left
            insert(root.left, val);
        }else{
            //insertRecursion right
            insert(root.right, val);
        }
    }

    void insert(Node node, int val){
        //compare the node's value to val
        if(val < node.value){
            //check if left has children if yes call insert again else make new node with val
            if(node.left != null){
                insert(node.left, val);
            }else{
                node.left = new Node(val);
                System.out.println("Val inserted at left node: "+val);
            }
        }else{
            //check if right has children if yes call insert again else make new node with val
            if(node.right != null){
                insert(node.right, val);
            }else{
                node.right = new Node(val);
                System.out.println("Val inserted at right node: "+val);
            }
        }
    }

    //lookup -> return value else return null/zero/print no number here
    int lookUp(int val){
        System.out.println("Number not found");
        return 0;
    }
    //remove
    void remove(int val){

    }

    void printStart(){
        printInOrder(root);
    }

    void printInOrder(Node node){
        if(node == null){
            return;
        }   

        printInOrder(node.left);
        System.out.println(""+node.value);
        printInOrder(node.right);
    }

class Node {
    int value;
    Node left;
    Node right;
 
    Node(int value) {
        this.value = value;
        right = null;
        left = null;
    }

    }

}

public class binaryTree{
    public static void main(String[] args) {
        tree tree = new tree(3);
        tree.insertStart(1);
        tree.insertStart(0);
        tree.insertStart(4);
        tree.insertStart(5);
        tree.printStart();
        
    }
}

Upvotes: 0

Views: 35

Answers (1)

Shridhar R Kulkarni
Shridhar R Kulkarni

Reputation: 7063

Check the comments for the changes that need to be done.

void printPreOrder(Node node){ //rename method to printPreOrder
    if(node == null){
        return;
    }   

    System.out.println(""+node.value); //print here. If you print here it is preorder.
    printInOrder(node.left);
    //don't print here. If you print here it is inorder.
    printInOrder(node.right);
}

Also, in insertStart method, you should return after adding the node.

    if(val < root.value && root.left == null){
        root.left = new Node(val);
        System.out.println("Val inserted to left of root: "+root.left.value);
        return; //add return here
    }

    if(val > root.value && root.right == null){
        root.right = new Node(val);
        System.out.println("Val inserted to right of root: "+root.right.value);
        return; //add return here
    }

If you don't add the above mentioned return statements, you'll get output as

3 1 0 1 4 4 5

instead of

3 1 0 4 5

Upvotes: 1

Related Questions