二叉查找树,二叉排序树,二叉搜索树都是一回事儿。
public class binarySearchTree<T extends Comparable<T>> {
//维护一个根节点,也即一棵树
private Node<T> root;
private static class Node<T>{
T value;
Node<T> left;
Node<T> right;
public Node(){
value=null;
}
public Node(T value){
this(value,null,null);
}
public Node(T value, Node<T> left, Node<T> right){
this.value=value;
this.left=left;
this.right=right;
}
}
public binarySearchTree(){
root=null;
}
public void clear(){
root=null;
}
public boolean isEmpty(){
return root==null;
}
/*查找方法*/
//查找指定的元素,默认从根节点开始查询
public boolean contains(T t){
return contains(t,root);
}
//从某个节点开始查找元素
public boolean contains(T t, Node<T> node){
if(node==null){
return false;
}
int result=t.compareTo(node.value);
if(result>0)
return contains(t,node.right);
else if(result<0)
return contains(t,node.left);
else
return true;
}
//求二叉查找树树中的最小值,返回最小值,迭代
public T findMin(){
Node<T> temp=root;
if(temp!=null){
while(temp.left!=null){
temp=temp.left;
}
}
return temp.value;
}
//求二叉查找树树中的最小值,返回最小值,迭代
public T findMax(){
Node<T> temp=root;
if(temp!=null){
while(temp.right!=null){
temp=temp.right;
}
}
return temp.value;
}
//递归
public Node<T> findMin(Node<T> node){
if(node==null)
return null;
else if(node.left==null)
return node;
else
return findMin(node.left);
}
//迭代插入
public boolean insert(T t){
if(root==null){
root=new Node(t);
return true;
}
Node<T> newNode=new Node(t);
Node<T> pointer=root;
while(true){
if(newNode.value.compareTo(pointer.value)>0){
if(pointer.right==null){
pointer.right=newNode;
return true;
}else{
pointer=pointer.right;
}
}else if(newNode.value.compareTo(pointer.value)<0){
if(pointer.left==null){
pointer.left=newNode;
return true;
}else{
pointer=pointer.left;
}
}else{
return false;
}
}
}
//递归插入
public void insertData(T t){
root=insertData(t,root);
}
public Node<T> insertData(T t,Node<T> node){
if(node==null){
node=new Node<T>(t);
}
int result=t.compareTo(node.value);
if(result>0)
node.right=insertData(t,node.right);
else if(result<0)
node.left=insertData(t,node.left);
else;
return node;
}
//递归删除,删除分三种情况
public void remove(T t){
root=remove(t,root);
}
public Node<T> remove(T t, Node<T> node){
if(node==null)
return node;
int result=t.compareTo(node.value);
if(result>0)
node.right=remove(t,node.right);
else if(result<0)
node.left=remove(t,node.left);
else if(node.left!=null && node.right!=null){
node.value=findMin(node.right).value;
node.right=remove(node.value,node.right);
}
else
node=(node.left!=null)?node.left:node.right;
return node;
}
public Node<T> get(T t){
Node<T> cur=root;
while(cur!=null){
if(cur.value.compareTo(t)>0){
cur=cur.right;
}else if(cur.value.compareTo(t)<0){
cur=cur.left;
}else{
break;
}
}
return cur;
}
public void inOrderReverse(){
printLRR(root);
}
private void printLRR(Node<T> node){
while(node!=null){
printLRR(node.left);
System.out.println(node.value);
printLRR(node.right);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
binarySearchTree<Integer> bst=new binarySearchTree<Integer>();
bst.insert(20);
bst.insert(10);
bst.insert(30);
bst.inOrderReverse();
}
}