/* * Jeremy Lipschutz * * A class representing a tree of integers * List list = new ArrayList(); * SearchTree tree = new SearchTree(); */ public class SearchTree> { private SearchTreeNode overallRoot; // Constructs an empty tree public SearchTree() { overallRoot = null; } // Adds the given value into the current tree, maintaining // the BST property public void add(E value) { overallRoot = add(overallRoot, value); } // Add in the given value into the tree represented by the // given root, maintaining the BST property private SearchTreeNode add(SearchTreeNode root, E value) { if(root == null) { root = new SearchTreeNode(value); } else if (value.compareTo(root.data) < 0) { root.left = add(root.left, value); } else if (value.compareTo(root.data) > 0) { root.right = add(root.right, value); } return root; } // pre: the tree is a binary search tree // Returns whether or not a value is contained within the current // tree public boolean contains(E value) { return contains(overallRoot, value); } // Returns whether or not a value is contained within the // tree represented by the given root of it private boolean contains(SearchTreeNode root, E value) { if(root == null) { // The empty tree holds no values, so the value can't be // in this tree return false; } else if (value.compareTo(root.data) == 0) { return true; } else if (value.compareTo(root.data) < 0) { return contains(root.left, value); } else { // value.compareTo(root.data) > 0 return contains(root.right, value); } } // Prints the preorder traversal of the current tree public void printPreorder() { System.out.print("preorder: "); printPreorder(overallRoot); System.out.println(); } // Prints the preorder traversal of the tree represented by the // given root of it private void printPreorder(SearchTreeNode root) { if(root != null) { System.out.print(root.data + " "); printPreorder(root.left); printPreorder(root.right); } } // Prints the preorder traversal of the current tree public void printInOrder() { System.out.print("inorder: "); printInOrder(overallRoot); System.out.println(); } // Prints the in order traversal of the tree represented by the // given root of it private void printInOrder(SearchTreeNode root) { if(root != null) { printInOrder(root.left); System.out.print(root.data + " "); printInOrder(root.right); } } // Prints the preorder traversal of the current tree public void printPostOrder() { System.out.print("postorder: "); printPostOrder(overallRoot); System.out.println(); } // Prints the post order traversal of the tree represented by the // given root of it private void printPostOrder(SearchTreeNode root) { if(root != null) { printPostOrder(root.left); printPostOrder(root.right); System.out.print(root.data + " "); } } // post: prints the tree contents, one per line, following an // inorder traversal and using indentation to indicate node // depth; prints right to left so that it looks correct when // the output is rotated; prints "empty" for an empty tree public void printSideways() { if(overallRoot == null) { System.out.println("empty tree"); } else { printSideways(overallRoot, 0); } } // post: prints in reversed preorder the tree with given root, // indenting each line to the given level private void printSideways(SearchTreeNode 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); } } }