写一个BST的java语言实现
import org.omg.CORBA.UserException;
public class BinarySearchTree<AnyType extends Comparable<? super AnyType>>
{
private static class BinaryNode<AnyType>
{
BinaryNode(AnyType theElement){this(theElement,null,null);}
BinaryNode(AnyType theElement,BinaryNode<AnyType> lt,BinaryNode<AnyType> rt)
{
element = theElement;
left = lt;
right = rt;
}
AnyType element;
BinaryNode<AnyType> left;
BinaryNode<AnyType> right;
}
private BinaryNode root;
public BinarySearchTree(){root = null;}
public void makeEmpty(){root = null;}
public boolean isEmpty(){return root == null;}
public boolean contains(AnyType x){ return contains(x,root);}
public AnyType findMin()
{ return findMin(root).element; }
public AnyType findMax()
{ return findMax(root).element; }
public void insert(AnyType x)
{ root = insert(x,root); }
public void remove(AnyType x)
{ root = remove(x,root);}
public void preOrderSearch(){
preOrderSearch(root);
}
private void preOrderSearch(BinaryNode<AnyType> t){
if(t != null)
{
System.out.println(t.element);
preOrderSearch(t.left);
preOrderSearch(t.right);
}
}
private boolean contains(AnyType x,BinaryNode<AnyType> t)
{
if(t == null)
return false;
int compareResult = x.compareTo(t.element);
if(compareResult < 0)
return contains(x,t.left);
else if(compareResult > 0)
return contains(x,t.right);
else
return true;
}
private BinaryNode<AnyType> findMin(BinaryNode<AnyType> t){
if (t == null)
return null;
else if(t.left == null)
return t;
return findMin(t.left);
}
private BinaryNode<AnyType> findMax(BinaryNode<AnyType> t){
if(t == null)
return null;
else if(t.right == null)
return t;
return findMax(t.right);
}
private BinaryNode<AnyType> insert (AnyType x, BinaryNode <AnyType> t)
{
if(t == null)
return new BinaryNode(x,null,null);
int compareResult = x.compareTo(t.element);
if(compareResult < 0 )
t.left = insert(x , t.left);
else if(compareResult > 0)
t.right = insert(x , t.right);
else ;//do nothing
return t;
}
private BinaryNode<AnyType> remove(AnyType x, BinaryNode<AnyType> t)
{
if(t == null) return t;
int compareResult = x.compareTo(t.element);
if(compareResult < 0 )
t.left = remove(x , t.left);
else if (compareResult > 0)
t.right = remove(x , t.right);
else if(t.left != null && t.right != null)//Two children
{
t.element = findMin(t.right).element;
t.right = remove(t.element,t.right);
}
else
t= (t.left != null) ? t.left : t.right; //只有一个儿子的时候
return t;
}
public static void main(String[] args) {
BinarySearchTree bst = new BinarySearchTree();
bst.insert(new Integer(10));
bst.insert(new Integer(20));
bst.insert(new Integer(30));
bst.insert(new Integer(5));
bst.insert(new Integer(15));
bst.preOrderSearch();
}
}