// Class to represent a generic binary search tree public class SearchTree> { private TreeNode overallRoot; public SearchTree() { this.overallRoot = null; } // adds the given value to this binary search tree public void add(E value) { overallRoot = add(value, overallRoot); } // adds the given value to the binary seach tree beginning // at the given root private TreeNode add(E value, TreeNode root) { if (root == null) { root = new TreeNode(value); } else if (value.compareTo(root.data) > 0) { root.right = add(value, root.right); } else if (value.compareTo(root.data) < 0) { root.left = add(value, root.left); } return root; } // prints this tree using preorder traversal public void printPreorder() { System.out.print("preorder: "); printPreorder(overallRoot); System.out.println(); } // prints the tree with the given root using preorder traversal private void printPreorder(TreeNode root) { if (root != null) { System.out.print(" " + root.data); printPreorder(root.left); printPreorder(root.right); } } // prints this tree using inorder traversal public void printInorder() { System.out.print("inorder: "); printInorder(overallRoot); System.out.println(); } // prints the tree with the given root using inorder traversal private void printInorder(TreeNode root) { if (root != null) { printInorder(root.left); System.out.print(" " + root.data); printInorder(root.right); } } // prints this tree using postorder traversal public void printPostorder() { System.out.print("postorder: "); printPostorder(overallRoot); System.out.println(); } // prints the tree with the given root using postorder traversal private void printPostorder(TreeNode root) { if (root != null) { printPostorder(root.left); printPostorder(root.right); System.out.print(" " + root.data); } } // prints the contents of this tree, one per line, // using indentation to indicate depth // output will look like this tree rotated 90 degrees public void printSideways() { printSideways(overallRoot, 0); } // prints the contents of the tree with the given root // indenting each line to at least the given level private void printSideways(TreeNode root, int level) { if (root != null) { printSideways(root.right, level + 1); for (int i = 0; i < level; i++) { System.out.print(" "); } System.out.println(root.data); printSideways(root.left, level + 1); } } }