// Helene Martin, CSE 143 // A Set implementation that stores elements in a binary search tree. // Elements are ordered using their natural ordering (from compareTo). // Class invariants: // - nodes to the left of a root have smaller values // - nodes to the right of a root have larger values // - there are no duplicates // New: // * implements a Set interface representing the Set ADT // * TreeNode is a private inner class to provide encapsulation // * uses a type parameter to store any kind of data import java.util.NoSuchElementException; public class TreeSet> implements Set { private TreeNode overallRoot; // Constructs an empty binary search tree. public TreeSet() { overallRoot = null; } // Adds the given value to this BST. // Post: the tree is still a valid BST public void add(E value) { overallRoot = add(overallRoot, value); } // Adds the given value to the BST starting at the given root. // Post: the tree is still a valid BST 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; } // Removes the node with the given value from this BST. public void remove(E value) { overallRoot = remove(overallRoot, value); } private TreeNode remove(TreeNode root, E value) { if (root != null) { if (value.compareTo(root.data) < 0) { root.left = remove(root.left, value); } else if (value.compareTo(root.data) > 0) { root.right = remove(root.right, value); } else { // found it! // leaf node case is same as removeLeaves // (not necessary to have as a separate case!) if (root.left == null && root.right == null) { root = null; } else if (root.right == null) { // left child only root = root.left; } else if (root.left == null) { // right child only; root = root.right; } else { E newRoot = getMin(root.right); remove(newRoot); root.data = newRoot; } } } return root; } // Returns true if the value is in this tree, false otherwise. public boolean contains(E value) { return contains(overallRoot, value); } // Returns true if the value is in the tree starting at the // specified root. private boolean contains(TreeNode root, E value) { if (root == null) { return false; } else if (root.data.equals(value)) { return true; } else if (value.compareTo(root.data) < 0) { return contains(root.left, value); } else { return contains(root.right, value); } } // Returns the minimum value from this BST. // Throws a NoSuchElementException if the tree is empty. public E getMin() { if (overallRoot == null) { throw new NoSuchElementException(); } return getMin(overallRoot); } // Returns the minimum value from the BST with the given root. // Pre: root is not null private E getMin(TreeNode root) { if (root.left == null) { return root.data; } else { return getMin(root.left); } } // Returns the number of nodes in this tree. // Note: we could simply keep a size field instead public int size() { return size(overallRoot); } // Returns the number of nodes in the subtree with root as its root node. private int size(TreeNode root) { if (root == null) { return 0; } else { return 1 + size(root.left) + size(root.right); } } public boolean isEmpty() { return size() == 0; } // Prints an in-order traversal of this tree. public void printInorder() { printInorder(overallRoot); } // Prints an in-order traversal of the tree starting at the specified root private void printInorder(TreeNode root) { if (root != null) { printInorder(root.left); System.out.print(root.data + " "); printInorder(root.right); } } // TreeNode objects stores a single node of a binary tree. private class TreeNode { public 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; } } }