Left-leaning Red Black Tree
看算法4(算法 第4版 Algorithms 4th Edition)3.4节时,后面的习题有实现左倾红黑树删除操作的代码,刚开始看得云里雾里的,后来查了不少资料慢慢理解了,发现还是对LLRBT的性质认识得不够深刻,如果认识深刻的话还是可以理解代码的。今天做一个总结,希望能帮到遇到类似问题的朋友,如有错误,还望大家指出。
- 总是完美黑色平衡,且所有右链接都是黑链接,只有左链接有可能是红链接。
- 旋转操作(rotate)不会影响平衡。
- 转换颜色操作(flipColors)不会影响平衡(前提是左右子节点同色,而根节点与子节点们异色)。
转换颜色: - moveRedLeft往左添红,删除最小节点或当前节点大于需删除节点时使用。
- moveRedRight往右添红,删除最大节点或当前节点小于需删除节点时使用。
- moveRedLeft和moveRedRight的代码实现中,也只是在不同条件下,使用了不同的旋转操作或者转换颜色操作,所以也不影响平衡。
- 当需要删除一个节点时,如果这个节点刚好是红节点(指向它的是红链接),那就直接删除1即可(红节点不影响平衡),但是这是理想状况,现实不可能是理想的,再但是!我们可以追求理想!
- 为了追求7中的理想,也就是当前节点永远是红节点,LLRBT的删除操作中有个隐藏约束,那就是确保当前节点或者当前节点的左子节点是红节点,为什么当前节点的左子节点是红节点也行呢,因为这时候我们不是能通过右旋操作把当前节点变成红节点嘛,再为什么是左子节点而不是右子节点呢,因为LLRBT只有左子节点可能是红啊。
- 做到8,基本就能达到7中我们所追求的理想状态了。但是又有,如果从root出我们开始时当前节点(此时就是root节点)和它的左子节点都不是红的怎么办(此时的隐藏信息是右子节点也不为红,因为LLRBT所有右链接都为黑),那我们就直接把根节点改为红!(不影响平衡,只是黑高减了1)
- 至此,我们就把现实变成了理想的样子。遇到待删除节点,就可以直接(zhanzhe)把它(qian)删除(zhengle)。 2
下面附上涉及到的代码
// make a left-leaning link lean to the right
private Node rotateRight(Node h) {
assert (h != null) && isRed(h.left);
// assert (h != null) && isRed(h.left) && !isRed(h.right); // for insertion only
Node x = h.left;
h.left = x.right;
x.right = h;
x.color = x.right.color;
x.right.color = RED;
x.size = h.size;
h.size = size(h.left) + size(h.right) + 1;
return x;
}
// make a right-leaning link lean to the left
private Node rotateLeft(Node h) {
assert (h != null) && isRed(h.right);
// assert (h != null) && isRed(h.right) && !isRed(h.left); // for insertion only
Node x = h.right;
h.right = x.left;
x.left = h;
x.color = x.left.color;
x.left.color = RED;
x.size = h.size;
h.size = size(h.left) + size(h.right) + 1;
return x;
}
// flip the colors of a node and its two children
private void flipColors(Node h) {
// h must have opposite color of its two children
// assert (h != null) && (h.left != null) && (h.right != null);
// assert (!isRed(h) && isRed(h.left) && isRed(h.right))
// || (isRed(h) && !isRed(h.left) && !isRed(h.right));
h.color = !h.color;
h.left.color = !h.left.color;
h.right.color = !h.right.color;
}
/**
* Removes the smallest key and associated value from the symbol table.
*
* @throws NoSuchElementException if the symbol table is empty
*/
public void deleteMin() {
if (isEmpty()) throw new NoSuchElementException("BST underflow");
// if both children of root are black, set root to red
if (!isRed(root.left) && !isRed(root.right))
root.color = RED;
root = deleteMin(root);
if (!isEmpty()) root.color = BLACK;
// assert check();
}
// delete the key-value pair with the minimum key rooted at h
private Node deleteMin(Node h) {
if (h.left == null)
return null;
if (!isRed(h.left) && !isRed(h.left.left))
h = moveRedLeft(h);
h.left = deleteMin(h.left);
return balance(h);
}
// Assuming that h is red and both h.left and h.left.left
// are black, make h.left or one of its children red.
private Node moveRedLeft(Node h) {
// assert (h != null);
// assert isRed(h) && !isRed(h.left) && !isRed(h.left.left);
flipColors(h);
if (isRed(h.right.left)) {
h.right = rotateRight(h.right);
h = rotateLeft(h);
flipColors(h);
}
return h;
}
/**
* Removes the largest key and associated value from the symbol table.
*
* @throws NoSuchElementException if the symbol table is empty
*/
public void deleteMax() {
if (isEmpty()) throw new NoSuchElementException("BST underflow");
// if both children of root are black, set root to red
if (!isRed(root.left) && !isRed(root.right))
root.color = RED;
root = deleteMax(root);
if (!isEmpty()) root.color = BLACK;
// assert check();
}
// delete the key-value pair with the maximum key rooted at h
private Node deleteMax(Node h) {
if (isRed(h.left))
h = rotateRight(h);
if (h.right == null)
return null;
if (!isRed(h.right) && !isRed(h.right.left))
h = moveRedRight(h);
h.right = deleteMax(h.right);
return balance(h);
}
// Assuming that h is red and both h.right and h.right.left
// are black, make h.right or one of its children red.
private Node moveRedRight(Node h) {
// assert (h != null);
// assert isRed(h) && !isRed(h.right) && !isRed(h.right.left);
flipColors(h);
if (isRed(h.left.left)) {
h = rotateRight(h);
flipColors(h);
}
return h;
}
/**
* Removes the specified key and its associated value from this symbol table
* (if the key is in this symbol table).
*
* @param key the key
* @throws IllegalArgumentException if {@code key} is {@code null}
*/
public void delete(Key key) {
if (key == null) throw new IllegalArgumentException("argument to delete() is null");
if (!contains(key)) return;
// if both children of root are black, set root to red
if (!isRed(root.left) && !isRed(root.right))
root.color = RED;
root = delete(root, key);
if (!isEmpty()) root.color = BLACK;
// assert check();
}
// delete the key-value pair with the given key rooted at h
private Node delete(Node h, Key key) {
// assert get(h, key) != null;
if (key.compareTo(h.key) < 0) {
if (!isRed(h.left) && !isRed(h.left.left))
h = moveRedLeft(h);
h.left = delete(h.left, key);
} else {
if (isRed(h.left))
h = rotateRight(h);
if (key.compareTo(h.key) == 0 && (h.right == null))
return null;
if (!isRed(h.right) && !isRed(h.right.left))
h = moveRedRight(h);
if (key.compareTo(h.key) == 0) {
Node x = min(h.right);
h.key = x.key;
h.val = x.val;
// h.val = get(h.right, min(h.right).key);
// h.key = min(h.right).key;
h.right = deleteMin(h.right);
} else h.right = delete(h.right, key);
}
return balance(h);
}