// This class represents a binary search tree of values public class SearchTree> { private TreeNode overallRoot; // Constructs an empty binary search tree. public SearchTree() { overallRoot = null; } // Adds the given value to the binary search tree public void add(E value) { overallRoot = add(overallRoot, value); } // Adds the value to the subtree with the given root private TreeNode add(TreeNode root, E value) { if (root == null) { root = new TreeNode(value); } else if (value.compareTo(root.data) < 0) { // value < root.data root.left = add(root.left, value); } else { // value >= root.data root.right = add(root.right, value); } return root; } // Prints the numbers in this tree in a in-order fashion. public void printInorder() { System.out.print("inorder:"); printInorder(overallRoot); System.out.println(); } // Prints the numbers of the subtree rooted at root // using a in-order traversal. private void printInorder(TreeNode root) { if (root != null) { printInorder(root.left); System.out.print(" " + root.data); printInorder(root.right); } } // Class that represents a single node in the tree private static class TreeNode { public E data; public TreeNode left; public TreeNode right; // Constructs a leaf node with given data public TreeNode(E data) { this(data, null, null); } // Constructs a branch node with given data, left subtree, right subtree public TreeNode(E data, TreeNode left, TreeNode right) { this.data = data; this.left = left; this.right = right; } } }