packageBSTree;public class BST_1 ,Value>{private Node root;//二叉查找树的根
private classNode{privateKey key;privateValue value;privateNode lchild,rchild;private int N; //以该节点为根的子树中的结点个数//构造方法
public Node(Key key,Value value,intN) {this.key =key;this.value =value;this.N =N;
}
@OverridepublicString toString() {return "Node [key=" + key + ", value=" + value + ", N=" + N + "]";
}
}//获取节点个数N
public intsize() {returnsize(root);
}private intsize(Node x) {if(x==null) return 0;returnx.N;
}//通过Key查找Value
publicValue search(Key key) {returnsearch(root,key);
}privateValue search(Node x,Key key) {//找不到,返回null
if(x==null) return null;//如果不为空,用待查找的值与当前根节点的值进行比较
int result = key.compareTo(x.key);//返回一个整数,大于或小于或等于0
if(result>0)return search(x.rchild, key);//大于,往右子树递归查
else if(result<0)returnsearch(x.lchild, key);else
returnx.value;
}//插入
public voidinsert(Key key,Value value){
root=insert(root,key,value);
}privateNode insert(Node x, Key key, Value value) {//如果key已经存在,则修改value为新的value值,不存在,则创建一个新的结点
if(x==null) return new Node(key, value, 1);int result =key.compareTo(x.key);if(result>0)
x.rchild=insert(x.rchild, key, value);else if(result<0)
x.lchild=insert(x.lchild, key, value);elsex.value=value;
x.N= size(x.rchild)+size(x.rchild)+1;returnx;
}//查找最小键
publicKey min() {returnmin(root).key;
}privateNode min(Node x) {if(x.lchild==null) returnx;else returnmin(x.lchild);
}//二叉查找树中最难的就是删除,先从删最简单的最小结点开始
public voiddeleteMin() {
root=deleteMin(root);
}//返回已经删了最小结点的根节点
privateNode deleteMin(Node x) {//在找到最小结点时x时,x=x.right
if(x.lchild==null) returnx.rchild;
x.lchild=deleteMin(x.lchild);
x.N= size(x.rchild)+size(x.rchild)+1;returnx;
}/**删除任意节点
* 1.如果树为null或者找不到key,返回null
* 2.否则,通过比较找到键Key的结点:
* 如果该结点没有右子树 ,只有左子树 x = x.left
* 如果该结点没有左子树 ,只有有子树x = x.right
* 该结点左右子树都有,先用Node t = x 存x结点,
* 找到以t.right为根节点的树的最小键, 赋予x: x = min(x.right),及替换x结点
* 然后把这个最小键删了,把t结点的左子树赋予x.left
* 3.返回 返回已经删了结点的根节点
**/
public voiddelete(Key key) {
root=delete(root,key);
}privateNode delete(Node x, Key key) {if(x==null) return null;int result =key.compareTo(x.key);if(result>0) x.rchild =delete(x.rchild, key);else if(result<0) x.lchild =delete(x.lchild, key);else{if(x.rchild==null) returnx.lchild;if(x.lchild==null) returnx.rchild;
Node t=x;
x=min(t.rchild);
x.rchild=deleteMin(t.rchild);
x.lchild=t.lchild;
}
x.N= size(x.lchild)+size(x.rchild)+1;returnx;
}//前序遍历:根--左子树--右子树
public voidpreOrder() {
preOrder(root);
}private voidpreOrder(Node x) {if(x!=null) {
System.out.print("["+x.key+":"+x.value+"]"+" ");
preOrder(x.lchild);
preOrder(x.rchild);
}
}//中序遍历:左子树--根节点--右子树
public voidinOrder() {
inOrder(root);
}private voidinOrder(Node x) {if(x!=null) {
inOrder(x.lchild);
System.out.print("["+x.key+":"+x.value+"]"+" ");
inOrder(x.rchild);
}
}//后序遍历:左子树--右子树--根节点
public voidpostOrder() {
postOrder(root);
}private voidpostOrder(Node x) {if(x!=null) {
postOrder(x.lchild);
postOrder(x.rchild);
System.out.print("["+x.key+":"+x.value+"]"+" ");
}
}
}