shz.core.st.bst.ConcurrentRedBlackBST Maven / Gradle / Ivy
package shz.core.st.bst;
import shz.core.lock.ReadWriteLockHolder;
import java.io.Serializable;
/**
* 红黑二叉查找树(红黑树,2-3树的一种等价定义)
*
* 在一棵大小为N的2-3树中,查找和插入操作访问的结点必然不超过lgN个
*
* 满足条件:
* 1:红链接均为左链接
* 2:没有任何一个结点同时和两条红链接相连
* 3:该树是完美黑色平衡的,即任意空链接到根结点的路径上的黑链接数量相同
* 4:根结点总是黑色
*
* 一棵大小为N的红黑树的高度不会超过2lgN
* 一棵大小为N的红黑树中,根结点到任意结点的平均路径长度为~1.00lgN
*/
public abstract class ConcurrentRedBlackBST, T extends ConcurrentRedBlackBST.Node> extends ReadWriteLockHolder implements Serializable{
private static final long serialVersionUID = -324224617832967053L;
protected static abstract class Node> implements Serializable {
private static final long serialVersionUID = -8247800253948740151L;
public T left, right;
public int size = 1;
public boolean red;
public Node(boolean red) {
this.red = red;
}
}
protected T root;
protected ConcurrentRedBlackBST(T root) {
this.root = root;
}
public final int size() {
readLock.lock();
try {
return size(root);
} finally {
readLock.unlock();
}
}
protected final int size(T h) {
return h == null ? 0 : h.size;
}
public final boolean isEmpty() {
return size() == 0;
}
public final boolean isLeaf() {
return size() == 1;
}
protected final boolean isRed(T h) {
return h != null && h.red;
}
protected final T rotateLeft(T h) {
T x = h.right;
h.right = x.left;
x.left = h;
x.red = h.red;
h.red = true;
x.size = h.size;
h.size = 1 + size(h.left) + size(h.right);
return x;
}
protected final T rotateRight(T h) {
T x = h.left;
h.left = x.right;
x.right = h;
x.red = h.red;
h.red = true;
x.size = h.size;
h.size = 1 + size(h.left) + size(h.right);
return x;
}
protected final void flipColors(T h) {
h.red = true;
if (h.left != null) h.left.red = false;
if (h.right != null) h.right.red = false;
}
protected abstract K key(T h);
public final K min() {
readLock.lock();
try {
return root == null ? null : key(min(root));
} finally {
readLock.unlock();
}
}
protected final T min(T h) {
T l;
while ((l = h.left) != null) h = l;
return h;
}
public final K max() {
readLock.lock();
try {
return root == null ? null : key(max(root));
} finally {
readLock.unlock();
}
}
protected final T max(T h) {
T r;
while ((r = h.right) != null) h = r;
return h;
}
/**
* 树的高度
*/
public final int depth() {
readLock.lock();
try {
return depth(root);
} finally {
readLock.unlock();
}
}
protected final int depth(T h) {
if (h == null) return 0;
return Math.max(depth(h.left), depth(h.right)) + 1;
}
/**
* 返回排名为k的节点
*/
public final K select(int k) {
if (k < 1) throw new IllegalArgumentException();
readLock.lock();
try {
T h = select(root, k);
return h == null ? null : key(h);
} finally {
readLock.unlock();
}
}
protected final T select(T h, int k) {
while (h != null) {
int k0 = k - size(h.left) - 1;
if (k0 == 0) break;
if (k0 < 0) h = h.left;
else {
h = h.right;
k = k0;
}
}
return h;
}
public final void deleteMin() {
writeLock.lock();
try {
if (root == null) return;
if (!isRed(root.left) && !isRed(root.right)) root.red = true;
root = deleteMin(root);
if (root == null) return;
if (!isEmpty()) root.red = false;
} finally {
writeLock.unlock();
}
}
protected final T deleteMin(T h) {
if (h.left == null) return h.right;
if (!isRed(h.left) && !isRed(h.left.left)) h = moveRedLeft(h);
h.left = deleteMin(h.left);
return balance(h);
}
protected final T moveRedLeft(T h) {
//假设节点h为红色,h.left和H.left.left都为黑色
//将h.left或者h.left的子节点之一变红
flipColors(h);
if (h.right != null && isRed(h.right.left)) {
h.right = rotateRight(h.right);
h = rotateLeft(h);
}
return h;
}
protected final T balance(T h) {
if (isRed(h.right)) h = rotateLeft(h);
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.size = size(h.left) + size(h.right) + 1;
return h;
}
public final void deleteMax() {
writeLock.lock();
try {
if (root == null) return;
if (!isRed(root.left) && !isRed(root.right)) root.red = true;
root = deleteMax(root);
if (root == null) return;
if (!isEmpty()) root.red = false;
} finally {
writeLock.unlock();
}
}
protected final T deleteMax(T h) {
if (isRed(h.left)) h = rotateRight(h);
if (h.right == null) return h.left;
if (!isRed(h.right) && !isRed(h.right.left)) h = moveRedRight(h);
h.right = deleteMax(h.right);
return balance(h);
}
protected final T moveRedRight(T h) {
//假设节点h为红色,h.right和H.right.left都为黑色
//将h.right或者h.right的子节点之一变红
flipColors(h);
if (h.left != null && !isRed(h.left.left)) h = rotateRight(h);
return h;
}
}