// Erika Wolfe, CSE 143 // This class represents a binary search tree of values. // A binary search tree is one that every value to the left of a value is less // than it, and every value to the right is greater than it. public class SearchTree> { private TreeNode overallRoot; // Constructs an empty tree public SearchTree() { overallRoot = null; } // pre: This is a binary search tree // post: Adds the value to the tree such that // it maintains the binary search tree property public void add(E value) { overallRoot = add(overallRoot, value); } // pre: the subtree rooted at `root` is a binary search tree // post: adds the given value to the subtree rooted at // `root` such that it preserves the binary search tree // property 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 if (value.compareTo(root.data) > 0) { // value > root.data root.right = add(root.right, value); } return root; } // post: prints the tree contents using an inorder traversal public void printInorder() { System.out.print("inorder:"); printInorder(overallRoot); System.out.println(); } // post: prints in inorder the tree with given root private void printInorder(TreeNode root) { if (root != null) { printInorder(root.left); System.out.print(" " + root.data); printInorder(root.right); } } }