概念
红黑树是一种平衡二叉搜索树。它可以在O(log n)的时间内完成查找,插入和删除。
二叉搜索树: 左边的节点都小于父节点,右边的节点都大于父节点。
平衡二叉树: 任意左右两个子树的叶子节点的高度相差不超过1。
关于二叉树的分类可以看笔者的这篇文章:一句话弄懂常见二叉树类型
分成这两个概念来看,红黑树就非常好理解了。
- 二叉搜索树的特性非常容易满足,每次在添加新节点的时候如果小于当前节点,就放到左边,如果大于当前节点就放到右边。
- 红黑树为了满足平衡二叉树的特性,主要是通过“左旋”和“右旋”两个操作。
那么在什么场景下用“左旋”,什么场景下用“右旋”呢?这就需要判断当前节点是“红”还是“黑”了。也是由于红黑树的节点有红黑之分,所以被称为“红黑树”。
左旋和右旋
左旋demo图和源码
private void rotateLeft(Entry<K,V> p) {
if (p != null) {
Entry<K,V> r = p.right;
p.right = r.left;
if (r.left != null)
r.left.parent = p;
r.parent = p.parent;
if (p.parent == null)
root = r;
else if (p.parent.left == p)
p.parent.left = r;
else
p.parent.right = r;
r.left = p;
p.parent = r;
}
}
右旋demo图和源码
private void rotateRight(Entry<K,V> p) {
if (p != null) {
Entry<K,V> l = p.left;
p.left = l.right;
if (l.right != null) l.right.parent = p;
l.parent = p.parent;
if (p.parent == null)
root = l;
else if (p.parent.right == p)
p.parent.right = l;
else p.parent.left = l;
l.right = p;
p.parent = l;
}
}
源码分析
红黑树的查找操作完全与二叉搜索树相同,因此此处就不增长篇幅分析了。
本文主要分析添加与删除操作的源码。
本文对红黑树"调整结构"部分的源码没有分析。
笔者也花了较多时间在分析该块代码上,虽然在源码上该块代码并不多,但是其判断“左旋”、“右旋”、“设置颜色”的条件没法通过上下文关系理解。
个人想来应该是是1978年的红黑树论文中论证了通过这样的条件可以保证红黑树的平衡性。对于一般的源码阅读者,就像记住定理一样,知道如此能保证红黑树的平衡性就可以了。
.
笔者想,与其“不知其所以然”地将代码在文章中复述一遍,不如就不讲了。
如果有读者对这一块比较熟悉的,欢迎评论交流。
添加源码
主要逻辑:
- 直接当做二叉搜索树来做插入逻辑。
如果值比当前node小,就走左边,如果比当前node大就走右边。 - 在做完插入之后,通过fixAfterInsertion()这个方法来做红黑树的结构修正。
- fixAfterInsertion()中主要有两个逻辑,一是通过左旋右旋让红黑树到达平衡状态,二是重新为各节点附上红黑的颜色。
public V put(K key, V value) {
TreeMapEntry<K,V> t = root;
if (t == null) {
if (comparator != null) {
if (key == null) {
comparator.compare(key, key);
}
} else {
if (key == null) {
throw new NullPointerException("key == null");
} else if (!(key instanceof Comparable)) {
throw new ClassCastException(
"Cannot cast" + key.getClass().getName() + " to Comparable.");
}
}
// END Android-changed: Work around buggy comparators. http://b/34084348
root = new TreeMapEntry<>(key, value, null);
size = 1;
modCount++;
return null;
}
int cmp;
TreeMapEntry<K,V> parent;
// split comparator and comparable paths
Comparator<? super K> cpr = comparator;
if (cpr != null) {
do {
parent = t;
cmp = cpr.compare(key, t.key);
if (cmp < 0)
t = t.left;
else if (cmp > 0)
t = t.right;
else
return t.setValue(value);
} while (t != null);
}
else {
if (key == null)
throw new NullPointerException();
@SuppressWarnings("unchecked")
Comparable<? super K> k = (Comparable<? super K>) key;
do {
parent = t;
cmp = k.compareTo(t.key);
if (cmp < 0)
t = t.left;
else if (cmp > 0)
t = t.right;
else
return t.setValue(value);
} while (t != null);
}
TreeMapEntry<K,V> e = new TreeMapEntry<>(key, value, parent);
if (cmp < 0)
parent.left = e;
else
parent.right = e;
fixAfterInsertion(e);
size++;
modCount++;
return null;
}
删除源码
主要逻辑:
- 先删除在二叉树中的结点。
- 删除该结点后通过fixAfterDeletion()来调整红黑树的结构。
- fixAfterDeletion()中主要有两个逻辑,一是通过左旋右旋让红黑树到达平衡状态,二是重新为各节点附上红黑的颜色。
删除二叉树中的结点有三种情况:
- 如果该结点是叶子结点,那么直接把该结点从父结点删除。
- 如果该结点只有左孩子,那么让他 的左孩子来替代它原来的位置。
- 如果该结点只有右孩子,那么让他 的右孩子来替代它原来的位置。
- 如果该结点左右都有孩子,那么该结点会选择右子树中最小的值来替换自己的值,然后把右子树中最小的值删除。
public V remove(Object key) {
TreeMapEntry<K,V> p = getEntry(key);
if (p == null)
return null;
V oldValue = p.value;
deleteEntry(p);
return oldValue;
}
private void deleteEntry(TreeMapEntry<K,V> p) {
modCount++;
size--;
// If strictly internal, copy successor's element to p and then make p
// point to successor.
if (p.left != null && p.right != null) {
TreeMapEntry<K,V> s = successor(p);
p.key = s.key;
p.value = s.value;
p = s;
} // p has 2 children
// Start fixup at replacement node, if it exists.
TreeMapEntry<K,V> replacement = (p.left != null ? p.left : p.right);
if (replacement != null) {
// Link replacement to parent
replacement.parent = p.parent;
if (p.parent == null)
root = replacement;
else if (p == p.parent.left)
p.parent.left = replacement;
else
p.parent.right = replacement;
// Null out links so they are OK to use by fixAfterDeletion.
p.left = p.right = p.parent = null;
// Fix replacement
if (p.color == BLACK)
fixAfterDeletion(replacement);
} else if (p.parent == null) { // return if we are the only node.
root = null;
} else { // No children. Use self as phantom replacement and unlink.
if (p.color == BLACK)
fixAfterDeletion(p);
if (p.parent != null) {
if (p == p.parent.left)
p.parent.left = null;
else if (p == p.parent.right)
p.parent.right = null;
p.parent = null;
}
}
}
static <K,V> TreeMapEntry<K,V> successor(TreeMapEntry<K,V> t) {
if (t == null)
return null;
else if (t.right != null) {
TreeMapEntry<K,V> p = t.right;
while (p.left != null)
p = p.left;
return p;
} else {
TreeMapEntry<K,V> p = t.parent;
TreeMapEntry<K,V> ch = t;
while (p != null && ch == p.right) {
ch = p;
p = p.parent;
}
return p;
}
}