public class BST> { // BinarySearchTree private BSTNode root; // the only data field public BST() { root = null; } // the only constructor private static class BSTNode { // BinarySearchTreeNode--main data structure private E data; private BSTNode left; private BSTNode right; private BSTNode(E data, BSTNode left, BSTNode right) { // std this.data = data; this.left = left; this.right = right; // constructor } private BSTNode(E data) { this(data, null, null); } // leaf constructor } public void makeEmpty() { root = null; } public boolean isEmpty() { return root == null; } public E findMin() throws Exception { // min is leftmost node if (isEmpty()) throw new Exception("BST is null; no min exists"); return findMin(root).data; } private BSTNode findMin(BSTNode node) { for (; node.left!=null; node=node.left); return node; } public E findMax() throws Exception { // max is rightmost node if (isEmpty()) throw new Exception("BST is null; no max exists"); return findMax(root).data; } private BSTNode findMax(BSTNode node) { for (; node.right!=null; node=node.right); return node; } public boolean contains(E e) { return contains(e, root); } private boolean contains(E e, BSTNode node) { if (node == null ) return false; int compareResult = e.compareTo(node.data); if (compareResult < 0) return contains(e, node.left); if (compareResult > 0) return contains(e, node.right); return true; // compareResult==0, so found it } public void insert(E e) { root = insert(e, root); } private BSTNode insert(E e, BSTNode node) { if (node == null) return new BSTNode(e); int compareResult = e.compareTo(node.data); if( compareResult < 0 ) node.left = insert(e, node.left); else if( compareResult > 0 ) node.right = insert(e, node.right); else ; // Duplicate; do nothing return node; } public void remove(E e) { root = remove(e, root); } private BSTNode remove(E e, BSTNode node) { if (node == null) return node; // Item not found; do nothing int compareResult = e.compareTo(node.data); if (compareResult < 0) node.left = remove(e, node.left); else if (compareResult > 0) node.right = remove(e, node.right); else if (node.left != null && node.right != null ) { // Found; 2 children node.data = findMin(node.right).data; // These 2 stmts should be combined node.right = remove(node.data, node.right); // since both descend the tree } else // Found: < 2 children node = (node.left != null) ? node.left : node.right; return node; } public void printTree() { if (isEmpty()) System.out.println("Empty tree"); else printTree(root, 0); } private void printTree(BSTNode node, int depth) { // inorder traversal if (node != null) { printTree(node.left, depth+1); for (int i=0; i { Integer first, second; TwoIntegers(int first, int second) { this.first=first; this.second=second;} public int compareTo(TwoIntegers pair) { Integer Sum = this.first+this.second; return Sum.compareTo(pair.first+pair.second); } public String toString() { return "("+first+","+second+")"; } } private static class ThreeIntegers extends TwoIntegers { Integer third; ThreeIntegers(int first, int second, int third) { super(first, second); this.third = third; } public String toString() { return "("+first+","+second+","+third+")"; } } public static void main(String[]arg) throws Exception { BST tree = new BST(); tree.printTree(); ThreeIntegers three3 = new ThreeIntegers(1,2,20); ThreeIntegers three5 = new ThreeIntegers(3,2,13); BSTNode node5 = new BSTNode(three5); BSTNode node3 = new BSTNode(three3); tree.insert(three5); tree.printTree(); tree.insert(three3); tree.printTree(); System.out.println("Min = " + tree.findMin()); System.out.println("Max = " + tree.findMax()); System.out.println("ok"); } } // Local Variables: // c-basic-offset: 2 // compile-command: "javac BST.java && java BST" // End: