import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
public class BstNode2 {
private Node root;
private class Node{
private int N;
private String key;
private int value;
private Node left;
private Node right;
public Node(int N,String key,int value){
this.N = N;
this.key = key;
this.value = value;
}
}
public int size(){
return size(root);
}
public int size(Node x){
if(x==null)
return 0;
return x.N;
}
public int get(String key){
return get(root,key);
}
public int get(Node x,String key){
int cmp = key.compareTo(x.key);
if(x == null)
return 0;
if(cmp<0)
return get(x.left,key);
if(cmp>0)
return get(x.right,key);
return x.value;
}
public void put(String key,int value){
root = put(root,key,value);
}
public Node put(Node x,String key,int value){
if(x==null)
return new Node(1, key, value);
int cmp = key.compareTo(x.key);
if(cmp<0)
return put(x.left,key,value);
if(cmp>0)
return put(x.right,key,value);
x.value = value;
x.N =size(x.left)+size(x.right)+1;
return x;
}
public String min(){
return min(root).key;
}
public Node min(Node x){
if(x.left!=null)
return min(x.left);
return x;
}
public String max(){
return max(root).key;
}
public Node max(Node x){
if(x.right!=null)
return max(x.right);
return x;
}
//floor
public String floor(String key){
Node x = floor(root,key);
if(x!=null)
return x.key;
return null;
}
public Node floor(Node x,String key){
if(x==null)
return null;
int cmp = key.compareTo(x.key);
if(cmp == 0)
return x;
if(cmp <0)
return floor(x.left,key);
Node t = floor(x.right, key);
if(t!=null)
return t;
else
return x;
}
//select 选择
//rank 排名
public String select(int k){
return select(root,k).key;
}
public Node select(Node x,int k){
if(x==null)
return null;
int t = size(x.left);
if(k<t)
return select(x.left,k);
if(k>t)
return select(x.right,k-t-1);
else
return x;
}
//rank
public int rank(String key){
return rank(root,key);
}
public int rank(Node x,String key){
if(x==null)
return 0;
int cmp = key.compareTo(x.key);
if(cmp<0)
return rank(x.left,key);
if(cmp>0)
return size(x.left)+1+rank(x.right,key);
else
return size(x.left);
}
//deleteMin
public void deleteMin(){
root = deleteMin(root);
}
public Node deleteMin(Node x){
if(x==null)
return null;
if(x.left == null)
return x.right;
x.left = deleteMin(x.left);
x.N = size(x.left)+size(x.right)+1;
return x;
}
//deleteMax
public void deleteMax(){
root = deleteMax(root);
}
public Node deleteMax(Node x){
if(x==null)
return null;
if(x.right ==null)
return x.left;
x.right = deleteMax(x.right);
x.N = size(x.left)+size(x.right)+1;
return x;
}
//删除任意一个delete
public void delete(String key){
root = delete(root,key);
}
public Node delete(Node x, String key){
if(x==null)
return null;
int cmp = key.compareTo(x.key);
if(cmp>0)
return delete(x.right, key);
if(cmp<0)
return delete(x.left, key);
if(x.left == null)
return x.right;
if(x.right == null)
return x.left;
else{
Node t = x ;
x = min(t.right);
x.right = deleteMin(t.right);
x.left = t.left;
}
x.N = size(x.left)+size(x.right)+1;
return x;
}
public Iterable<String> keys(){
return keys(min(),max());
}
public Iterable<String> keys(String lo,String hi){
Queue<String> queue = new LinkedList();
keys(root,queue,lo,hi);
return queue;
}
public void keys(Node x,Queue<String> queue,String low,String hi){
if(x == null)
return ;
int cmplo = low.compareTo(x.key);
int cmphi = hi.compareTo(x.key);
if(cmplo<0)
keys(x.left,queue,low,hi);
if(cmplo<=0&&cmphi>=0)
queue.offer(x.key);
if(cmphi>0)
keys(x, queue, low, hi);
}
}
//其中主要比较难实现的是delete的那个方法
我们的deleteMin 中接受一个指向结点的链接,并且返回一个指向结点的链接
删除主要涉及下面几步
1.找到要删除的那个结点
2.保存要删除的那个节点
3.用x 指向它的后继结点min(t.right);
4.x的左结点指向原来的结点
5. x的右结点指向deleteMin(t.right); // 这步很重要