查找二叉树ADT:性质:对于树中每个节点X,它的左子树所有项的值小于X中的项而它的右子树所有项的值大于X中的项

/*
* 查找二叉树ADT:
* 性质:对于树中每个节点X,它的左子树所有项的值小于X中的项
* 而它的右子树所有项的值大于X中的项
*/
public class BinarySearchTree<AnyType extends Comparable<? super AnyType>> {
/*
* 树节点元素
*/
private static class BinaryNode<AnyType>{
AnyType element; //The data in the node(数据)
BinaryNode<AnyType> left; //lefi Child(左孩子:必须要比祖先节点小)
BinaryNode<AnyType> right; //right Child(右孩子:必须要比祖先节点大)
public BinaryNode(AnyType theElement){
this(theElement, null, null);
}
public BinaryNode(AnyType theElement, BinaryNode<AnyType> lt, BinaryNode<AnyType> rt){
element = theElement;
left = lt;
right = rt;
}
}
private BinaryNode<AnyType> root; //根节点
public boolean isEmpty(){
return root == null;
}
public BinarySearchTree(){
root = null;
}
public void makeEmpty(){
root = null;
}
public boolean contains(AnyType x){
return contains(x, root);
}
/*
* 利用递归的思想来递归调用
*/
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;
}
/*
* 取树中的最小值
*/
public AnyType findMin(){
if(isEmpty())
throw new NullPointerException();
return findMin(root).element;
}
private BinaryNode<AnyType> findMin(BinaryNode<AnyType> t){
if(t == null)
return null;
else if(t.left == null)
return t;
return(findMin(t.left));
}
/*
* 取树中的最大值
*/
public AnyType findMax(){
if(isEmpty())
throw new NullPointerException();
return findMax(root).element;
}
private BinaryNode<AnyType> findMax(BinaryNode<AnyType> t){
if(t == null)
return null;
else if(t.right == null)
return t;
return findMax(t.right);
}
/*
* 插入数据
*/
public void insert(AnyType x){
root = insert(x, root);
}
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);
if(compareResult > 0)
t.right = insert(x, t.right);
else
;
return t;
}
/*
* 删除节点
*/
public void remove(AnyType x){
root = remove(x, root);
}
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){
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 void printTree(){
if(isEmpty())
System.out.println("空树");
else
printTree(root);
}
private void printTree(BinaryNode<AnyType> t){
if(t != null){
printTree(t.left);
System.out.print(t.element+" ");
printTree(t.right);
}
}
/*
* 测试代码
*/
public static void main(String[] args){
BinarySearchTree<Integer> bst = new BinarySearchTree<>();
bst.insert(6);
bst.insert(2);
bst.insert(8);
bst.insert(1);
bst.insert(4);
bst.insert(3);
bst.printTree();
System.out.println("最小值:" + bst.findMin());
}
}