0

I am testing a bit of code in main() and for some reason I am getting "7" instead of "7 8". This is a very basic implementation of a Binary Search Tree, and I can't figure out what the problem is.

public class BSTNode{
public static void main(String [] args) {
    BSTNode root = new BSTNode(7);
    BSTNode.insert(root, 8);
    BSTNode.print(root);
}

public Integer val;
public BSTNode left = null;
public BSTNode right = null;

public BSTNode(Integer val) {
    this.val = val;
}

public static void print(BSTNode root){
    if(root == null) return;
    print(root.left);
    System.out.print(root.val + " ");
    print(root.right);
}

public static void insert(BSTNode root, Integer val){
    if(root == null) root = new BSTNode(val);
    else {
        if(val <= root.val) insert(root.left, val);
        else insert(root.right, val);
    }
}


}
Greg
  • 223
  • 2
  • 11

0 Answers0