为什么需要红黑树
高度为h的二叉搜索树进行增删改查的时间复杂度为o(h),因此高度h成为二叉搜索树性能的关键。对于具有n个节点的二叉搜索树,我们希望它的高度为log2n,但我们无法保证,为此红黑树出现了,它保证了在最坏情况下基本动态操作的时间复杂度为O(lgn)。
关于红黑树的详细定义和插入、删除操作,大家可以看《算法导论》的第13章《红黑树》,这里我就不赘述了。总之一句话,红黑树是“平衡”二叉搜索树的一种,它能保证树的相对平衡,从而使得相关操作的性能得到保证。
TreeMap
TreeMap是基于红黑树实现的。
注:TreeMap是“平衡”二叉搜索树的一种,所以它保证了key的排序。key的排序要不使用自定义的比较器comparator或者key实现了Comparable接口。
/**
* The comparator used to maintain order in this tree map, or
* null if it uses the natural ordering of its keys.
*
* @serial
*/
private final Comparator<? super K> comparator;
private transient Entry<K,V> root;
/**
* The number of entries in the tree
*/
private transient int size = 0;
private static final boolean RED = false;
private static final boolean BLACK = true;
// 节点的定义
static final class Entry<K,V> implements Map.Entry<K,V> {
K key;
V value;
Entry<K,V> left;
Entry<K,V> right;
Entry<K,V> parent;
boolean color = BLACK; // 虽然新节点刚开始为红色,但在进行调整的第一步就是把节点的颜色变为红色,所以为什么不直接把节点默认为红色?
/**
* Make a new cell with given key, value, and parent, and with
* {@code null} child links, and BLACK color.
*/
Entry(K key, V value, Entry<K,V> parent) {
this.key = key;
this.value = value;
this.parent = parent;
}
/**
* Returns the key.
*
* @return the key
*/
public K getKey() {
return key;
}
/**
* Returns the value associated with the key.
*
* @return the value associated with the key
*/
public V getValue() {
return value;
}
/**
* Replaces the value currently associated with the key with the given
* value.
*
* @return the value associated with the key before this method was
* called
*/
public V setValue(V value) {
V oldValue = this.value;
this.value = value;
return oldValue;
}
public boolean equals(Object o) {
if (!(o instanceof Map.Entry))
return false;
Map.Entry<?,?> e = (Map.Entry<?,?>)o;
return valEquals(key,e.getKey()) && valEquals(value,e.getValue());
}
public int hashCode() {
int keyHash = (key==null ? 0 : key.hashCode());
int valueHash = (value==null ? 0 : value.hashCode());
return keyHash ^ valueHash;
}
public String toString() {
return key + "=" + value;
}
}
put()方法
put()方法的大致流程:先根据二叉搜索树的性质找到父节点,执行插入,由于插入可能导致红黑树的性质被破坏(不能出现连续红、根节点为黑色),所有进行调整,调整的步骤请参考《算法导论》的第13章红黑树,里面有非常详细的介绍。
//如果自定义的比较器comparator不为null,comparator的compatr()方法比较对象,
//否则用key自带的比较器(此时key必须实现comparable接口)。
final int compare(Object k1, Object k2) {
return comparator==null ? ((Comparable<? super K>)k1).compareTo((K)k2)
: comparator.compare((K)k1, (K)k2);
}
public V put(K key, V value) {
Entry<K,V> t = root;
// 没有根节点
if (t == null) {
compare(key, key); // type (and possibly null) check
root = new Entry<>(key, value, null);
size = 1;
modCount++;
return null;
}
int cmp;
Entry<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) // 当前key小,则往左走
t = t.left;
else if (cmp > 0) // 当前key大,则往右走
t = t.right;
else
return t.setValue(value); // 认为是相同的key,则覆盖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);
}
Entry<K,V> e = new Entry<>(key, value, parent);
if (cmp < 0)
parent.left = e;
else
parent.right = e;
// 插入了新节点可能导致红黑树的性质遭到破坏,所以进行调整
fixAfterInsertion(e);
size++;
modCount++;
return null;
}
/** 调整 完全按照了《算法导论》第13章红黑树调整的伪代码流程 */
private void fixAfterInsertion(Entry<K,V> x) {
x.color = RED;
while (x != null && x != root && x.parent.color == RED) {
if (parentOf(x) == leftOf(parentOf(parentOf(x)))) {
Entry<K,V> y = rightOf(parentOf(parentOf(x)));
if (colorOf(y) == RED) {
setColor(parentOf(x), BLACK);
setColor(y, BLACK);
setColor(parentOf(parentOf(x)), RED);
x = parentOf(parentOf(x));
} else {
if (x == rightOf(parentOf(x))) {
x = parentOf(x);
rotateLeft(x);
}
setColor(parentOf(x), BLACK);
setColor(parentOf(parentOf(x)), RED);
rotateRight(parentOf(parentOf(x)));
}
} else {
Entry<K,V> y = leftOf(parentOf(parentOf(x)));
if (colorOf(y) == RED) {
setColor(parentOf(x), BLACK);
setColor(y, BLACK);
setColor(parentOf(parentOf(x)), RED);
x = parentOf(parentOf(x));
} else {
if (x == leftOf(parentOf(x))) {
x = parentOf(x);
rotateRight(x);
}
setColor(parentOf(x), BLACK);
setColor(parentOf(parentOf(x)), RED);
rotateLeft(parentOf(parentOf(x)));
}
}
}
root.color = BLACK;
}
get()方法
get()方法并没有什么特别之处,和普通的二叉搜索树一样进行搜索就OK。
public V get(Object key) {
Entry<K,V> p = getEntry(key);
return (p==null ? null : p.value);
}
final Entry<K,V> getEntry(Object key) {
// Offload comparator-based version for sake of performance
if (comparator != null)
return getEntryUsingComparator(key);
if (key == null)
throw new NullPointerException();
@SuppressWarnings("unchecked")
Comparable<? super K> k = (Comparable<? super K>) key;
Entry<K,V> p = root;
// key必须实现了Comparable接口
while (p != null) {
int cmp = k.compareTo(p.key);
if (cmp < 0)
p = p.left;
else if (cmp > 0)
p = p.right;
else
return p;
}
return null;
}
//是用自定义的比较器Comparator进行搜索
final Entry<K,V> getEntryUsingComparator(Object key) {
@SuppressWarnings("unchecked")
K k = (K) key;
Comparator<? super K> cpr = comparator;
if (cpr != null) {
Entry<K,V> p = root;
while (p != null) {
int cmp = cpr.compare(k, p.key);
if (cmp < 0)
p = p.left;
else if (cmp > 0)
p = p.right;
else
return p;
}
}
return null;
}