package Examples; import java.util.logging.Logger; import Sequenic.T3.SuiteUtils.Inference.Comment; /** * A class implementing unbalanced binary search tree. Note that all * "matching" is based on the compareTo method. * *
Adapted from original code by Mark Weiss. * *
Is this class correct....? * * @author Mark Allen Weiss * */ // ******************PUBLIC OPERATIONS********************* // void insert( x ) --> Insert x // void remove( x ) --> Remove x // Comparable find( x ) --> Return item that matches x // Comparable findMin( ) --> Return smallest item // Comparable findMax( ) --> Return largest item // boolean isEmpty( ) --> Return true if empty; else false // void makeEmpty( ) --> Remove all items public class BinarySearchTree { private final static Comment comment = new Comment(Logger.getLogger(BinarySearchTree.class.getName())) ; /** * Construct the tree. */ public BinarySearchTree() { root = null; } /** * Insert x into the tree; duplicates are ignored. * @param x the item to insert. */ public void insert(Comparable x) { if (x==null) throw new IllegalArgumentException(); comment.with("inputs ok").end(); root = insert(x, root); } /** * Remove from the tree. Nothing is done if x is not found. * @param x the item to remove. */ public void remove(Comparable x) { if (x==null) throw new IllegalArgumentException(); comment.with("inputs ok").end(); //System.out.println("**>> remove " + x) ; root = remove(x, root); } /** * Find the smallest item in the tree. * @return smallest item or null if empty. */ public Comparable findMin() { return elementAt(findMin(root)); } /** * Find the largest item in the tree. * @return the largest item of null if empty. */ public Comparable findMax() { return elementAt(findMax(root)); } /** * Find an item in the tree. * @param x the item to search for. * @return the matching item or null if not found. */ public Comparable find(Comparable x) { if (x==null) throw new IllegalArgumentException(); comment.with("inputs ok").end(); return elementAt(find(x, root)); } /** * Make the tree logically empty. */ public void makeEmpty() { root = null; } /** * Test if the tree is logically empty. * @return true if empty, false otherwise. */ public boolean isEmpty() { return root == null; } /** * Internal method to get element field. * @param t the node. * @return the element field or null if t is null. */ private Comparable elementAt(BinaryNode t) { comment.with("dead end").If(t==null) ; return t == null ? null : t.element; } /** * Internal method to insert into a subtree. * @param x the item to insert. * @param t the node that roots the tree. * @return the new root. */ private BinaryNode insert(Comparable x, BinaryNode t) { /* 1*/ if (t == null) /* 2*/ { t = new BinaryNode(x, null, null); /* 3*/ } else if (x.compareTo(t.element) < 0) /* 4*/ { t.left = insert(x, t.left); /* 5*/ } else if (x.compareTo(t.element) > 0) /* 6*/ { t.right = insert(x, t.right); /* 7*/ } else { comment.with("Duplicate; do nothing").end() ; /* 8*/ } /* 9*/ return t; } /** * Internal method to remove from a subtree. * @param x the item to remove. * @param t the node that roots the tree. * @return the new root. */ private BinaryNode remove(Comparable x, BinaryNode t) { if (t == null) { comment.with("Item not found; do nothing").end() ; return t; } if (x.compareTo(t.element) < 0) { t.left = remove(x, t.left); } else if (x.compareTo(t.element) > 0) { t.right = remove(x, t.right); } else if (t.left != null && t.right != null) // Two children { comment.with("Two children").end() ; t.element = findMin(t.right).element; t.right = remove(t.element, t.right); } else { comment.with("Has only one child").end() ; comment.with("No right child").If(t.left != null) ; t = (t.left != null) ? t.left : t.right; } return t; } /** * Internal method to find the smallest item in a subtree. * @param t the node that roots the tree. * @return node containing the smallest item. */ private BinaryNode findMin(BinaryNode t) { if (t == null) { comment.with("No minimum").end() ; return null; } else if (t.left == null) { return t; } return findMin(t.left); } /** * Internal method to find the largest item in a subtree. * @param t the node that roots the tree. * @return node containing the largest item. */ private BinaryNode findMax(BinaryNode t) { if (t != null) { while (t.right != null) { t = t.right; } } comment.with("No maximum").If(t == null) ; return t; } /** * Internal method to find an item in a subtree. * @param x is item to search for. * @param t the node that roots the tree. * @return node containing the matched item. */ private BinaryNode find(Comparable x, BinaryNode t) { if (t == null) { return null; } if (x.compareTo(t.element) < 0) { return find(x, t.left); } else if (x.compareTo(t.element) > 0) { return find(x, t.right); } else { return t;// Match } } /** The tree root. */ private BinaryNode root; }