查找 二叉查找树及红黑树的插入部分代码
均参考算法第四版
BST
codes
public class BST<Key extends Comparable<Key>,Value>{
private Node root;
private class Node
{
private Key key;
private Value val;
private Node left, right;
private int N;
public Node(Key key,Value val,int N)
{
this.key = key;
this.val = val;
this.N = N;
}
}
public int size()
{
return size(root);
}
private int size(Node x)
{
if(x == null) return 0;
else return x.N;
}
public Value get(Key key)
{
return get(root,key);
}
private Value get(Node x,Key key)
{
if (x == null) return null;
int cmp = key.compareTo(x.key);
if(cmp < 0)
return get(x.left,key);
else if (cmp > 0)
return get(x.right,key);
else return x.val;
}
public void put(Key key, Value val)
{
root = put(root, key, val);//相当于对整个数更新
}
private Node put(Node x,Key key,Value val)
{
if(x == null)
return new Node(key,val,1);//返回值插入
int cmp = key.compareTo(x.key);
if (cmp < 0)
x.left = put(x.left,key,val);
else if (cmp > 0)
x.right = put(x.right,key,val);
else x.val = val;//查找到key,更新value
x.N = size(x.left) + size(x.right) + 1;
return x;
}
public Key min()
{
return min(root).key;
}
private Node min(Node x)
{
if (x.left == null)
return x;
return min(x.left);
}
public Key floor(Key key)
//向上取整,找小于等于key的最大键
{
Node x = floor(root,key);
if (x == null) return null;
return x.key;
}
private Node floor(Node x,Key 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;
}
public Key select(int k) //找到排名为k的键
{
return select(root,k).key;
}
private Node select(Node x,int k)
{
if(x == null) return null;
int t = size(x.left);
if (t > k) return select(x.left,k);
else if (t < k) return select(x.right,k-t-1);
else return x;
}
public int rank(Key key) //找到给定键key的排名
{
return rank(key,root);
}
private int rank(Key key,Node x)
{
if(x == null) return 0;
int cmp = key.compareTo(x.key);
if (cmp < 0)
return rank(key,x.left);
else if (cmp > 0)
return 1 + size(x.left) + rank(key,x.right);
else return size(x.left);
}
public void deleteMin()//删除最小键对应的键值对
{
root = deleteMin(root);
}
private Node deleteMin(Node x)//看图理解
{
if(x.left == null) return x.right;//左子树为空,删除根结点
x.left = deleteMin(x.left);
x.N = size(x.left) + size(x.right) + 1;
return x;
}
//删除结点:用后继结点填充,即右子树的最小结点
public void delete(Key key)
{
root = delete(root,key);
}
private Node delete(Node x,Key key)
{
if(x == null) return null;
int cmp = key.compareTo(x.key);
if (cmp < 0)
x.left = delete(x.left,key);
else if (cmp > 0)
x.right = delete(x.right,key);
else
{
if (x.left == null) return x.right;
if(x.right == null) return x.left;
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<Key> keys()
{
return keys(min(),max());
}
public Iterable<Key> keys(Key lo,Key hi)
{
Queue<Key> queue = new Queue<Key>();
keys(root,queue,lo,hi);
return queue;
}
private void keys(Node x,Queue<Key> queue,Key lo,Key hi)
{
if(x == null) return;
int cmplo = lo.compareTo(x.key);
int cmphi = hi.compareTo(x.key);
if(cmplo < 0) keys(x.left,queue,lo,hi);
if(cmplo <= 0 && cmphi >= 0) queue.enqueue(x.key);
if(cmphi > 0) keys(x.right,queue,lo,hi);
}
}
RedBlackBST
codes
public class RedBlackBST<Key extends Comparable<Key>, Value> {
private static final boolean RED = true;
private static final boolean BLACK = false;
private Node root;
private class Node
{
Key key;
Value val;
Node left, right;
int N;
boolean color;
Node(Key key,Value val,int N,boolean color)
{
this.key = key;
this.val = val;
this.N = N;
this.color = color;
}
}
private boolean isRed(Node x)
{
if (x == null) return false;
return x.color == RED;
}
private Node rotateLeft(Node h)
{
Node x = h.right;
// 注意交换顺序
h.right = x.left;
x.left = h;
x.color = h.color;
h.color = RED;
x.N = h.N;
h.N = 1 + size(h.left) + size(h.right);
return x;
}
private Node rotateRight(Node h)
{
Node x = h.left;
h.left = x.right;
x.right = h;
x.color = h.color;
h.color = RED;
x.N = h.N;
h.N = 1 + size(h.left) + size(h.right);
return x;
}
private void flipColors(Node h)
{
h.color = RED;
h.left.color = BLACK;
h.right.color = RED;
}
public int size()
{
return size(root);
}
private int size(Node x)
{
if(x == null) return 0;
else return x.N;
}
public void put(Key key, Value val)
{
root = put(root, key, val);
root.color = BLACK;
}
private Node put(Node h, Key key, Value val)
{
if(h == null)
return new Node(key, val, 1, RED);
int cmp = key.compareTo(h.key);
if (cmp < 0) h.left = put(h.left, key, val);
else if (cmp > 0) h.right = put(h.right,key,val);
else h.val = val;
if(isRed(h.right) && !isRed(h.left)) h = rotateLeft(h);
if(isRed(h.left) && isRed(h.left.left)) h = rotateRight(h);
if(isRed(h.left) && isRed(h.right)) flipColors(h);
h.N = size(h.left) + size(h.right) + 1;
return h;
}
}