// Hunter Schafer, CSE 143 // This class represents a tree of any comparable object public class SearchTree> { private TreeNode overallRoot; // Constructs a tree with no numbers public SearchTree() { overallRoot = null; } // Returns true if the value is in this tree, false otherwise public boolean contains(E value) { return contains(overallRoot, value); } // Returns true if the given value is contained in the tree // rooted at the given root, false otherwise private boolean contains(TreeNode root, E value) { if (root == null) { return false; } else if (value.equals(root.data)) { return true; } else if (value.compareTo(root.data) < 0) { // value < root.data return contains(root.left, value); } else { // value > root.data return contains(root.right, value); } } // 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) { root.left = add(root.left, value); } else if (value.compareTo(root.data) > 0) { root.right = add(root.right, value); } return root; } /**** Don't do this or I'll cry :'( private void addNoXEqualsChangeX(IntTreeNode root, int value) { // this doesn't work if overallRoot is null, // need to add a special case in the public method too :'( if (root.left == null && root.right == null) { if (value < root.data) { root.left = new IntTreeNode(value); } else if (value > root.data) { root.right = new IntTreeNode(value); } } else if (value < root.data) { if (root.left == null) { root.left = new IntTreeNode(value); } else { add(root.left, value); } } else if (value > root.data) { if (root.right == null) { root.right = new IntTreeNode(value); } else { add(root.right, value); } } }****/ // prints the numbers in this tree in a pre-order fashion. public void print() { print(overallRoot); } // prints the numbers of the subtree rooted at root // using a pre-order traversal. private void print(TreeNode root) { if (root != null) { System.out.print(root.data + " "); // print the left print(root.left); // print the right print(root.right); } } // Returns the number of numbers in this tree. public int size() { return size(overallRoot); } // Returns the number of nodes in the tree starting at root. private int size(TreeNode root) { if (root == null) { return 0; } else { int leftSize = size(root.left); int rightSize = size(root.right); return 1 + leftSize + rightSize; } } // Represents a single node in the binary search tree private class TreeNode { // note: Your homework should have the node class also static // I had to take it out for generics, but you should make i // a private static inner class public final E data; // data stored at this node public TreeNode left; // reference to left subtree public TreeNode right; // reference to right subtree // Constructs a leaf node with the given data. public TreeNode(E data) { this(data, null, null); } // Constructs a leaf or branch node with the given data and links. public TreeNode(E data, TreeNode left, TreeNode right) { this.data = data; this.left = left; this.right = right; } } }