// A Set implementation that stores elements in a binary search tree. // Elements are ordered using their natural ordering (from compareTo). // Class invariant: // - 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.*; public class TreeSet> implements Set { private TreeNode overallRoot; private int size; // Constructs an empty binary tree public TreeSet() { overallRoot = null; size = 0; } // returns true if the value is found in this tree, false otherwise public boolean contains(E value) { return contains(overallRoot, value); } 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); } } // adds a value to the bst // post: the tree is still a valid bst public void add(E value) { overallRoot = add(overallRoot, value); } private TreeNode add(TreeNode root, E value) { if (root == null) { size++; root = new TreeNode(value); // return root; // useful for visualizing with jGRASP debugger } 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; } // 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); } private E getMin(TreeNode root) { if (root.left == null) { return root.data; } else { return getMin(root.left); } } // 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! size--; if (root.left == null && root.right == null) { // leaf node root = null; } else if (root.right == null) { root = root.left; } else if (root.left == null) { root = root.right; } else { E min = getMin(root.right); root.data = min; size++; // to compensate for the remove below root.right = remove(root.right, min); } } } return root; } public int size() { return size; } public boolean isEmpty() { return size == 0; } // IntTreeNode objects stores a single node of a binary tree of ints. 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; } } }