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;
@Override
public String toString() {
return "BinaryNode{" +
"element=" + element +
", left=" + left +
", right=" + right +
'}';
}
}
//根节点
private BinaryNode<AnyType> 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 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 BufferUnderflowException();
return findMin(root).element;
}
public AnyType findMax(){
if (isEmpty())
throw new BufferUnderflowException();
return findMax(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);
}
//循环找出最大值
private BinaryNode<AnyType> findMax(BinaryNode<AnyType> t){
if (t!=null)
while (t.right!=null)
t=t.right;
return t;
}
public void insert(AnyType x){
root = insert(x, root);
System.out.println(root);
}
//递归找到为空的孩子节点插入数据,也就是让一个节点的左右引用指向一个新建的BinaryNode节点
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 ;
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("Empty tree");
else
printTree(root);
}
private void printTree(BinaryNode<AnyType> t){
if (t!=null){
printTree(t.left);
System.out.println(t.element);
printTree(t.right);
}
}
public static void main(String[] args) {
BinarySearchTree<Integer> tree = new BinarySearchTree<>();
tree.insert(5);
tree.insert(7);
tree.insert(4);
tree.insert(2);
tree.insert(9);
tree.printTree();
}
}
output:
BinaryNode{element=5, left=null, right=null}
BinaryNode{element=5, left=null, right=BinaryNode{element=7, left=null, right=null}}
BinaryNode{element=5, left=BinaryNode{element=4, left=null, right=null}, right=BinaryNode{element=7, left=null, right=null}}
BinaryNode{element=5, left=BinaryNode{element=4, left=BinaryNode{element=2, left=null, right=null}, right=null}, right=BinaryNode{element=7, left=null, right=null}}
BinaryNode{element=5, left=BinaryNode{element=4, left=BinaryNode{element=2, left=null, right=null}, right=null}, right=BinaryNode{element=7, left=null, right=BinaryNode{element=9, left=null, right=null}}}
2
4
5
7
9
03-25