class TreeNode { public int val; public TreeNode left; public TreeNode right; public TreeNode(int x) { val = x; left = null; right = null; } } public class BinarySearchTree { private TreeNode root; // Adds a new node with key to the binary search tree. public void insert(int key){ // Base case: if no tree, then create one if (root == null) { this.root = new TreeNode(key); } else { // find and return root where our key should be inserted. This is called the "search" method. Node current = search(this.root, key); int leftVal = (int)(current.left.val); if ((leftVal == 0) && !key.equals(leftVal)) { this.root.left = new TreeNode(key); } else{ // If not inserted yet or a duplicate is found Node current2 = search(current.right, key); int rightVal = (int)(current2.right.val); if ((rightVal == 0) && !key.equals(rightVal)) { this.root.left = new TreeNode(key); } } } } }