jdk1.8 源码如下
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)
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);
}
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;
}
我们来分析一下。
这段代码的意思是:
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);
}
解释一下上面的代码他会看一下你在构造函数调用的时候有没有传入一个比较器,
如果有的话,插入的时候就用这个比较器来找递归找位置,如果没有的话,就将插入的对象转换成Comparable接口 与 根元素开始进行递归比较。直到找到合适的位置。并用parent这个成员变量 引用。
接着 创建一个新的对象,并设置他与parent的 “关系”。完成插入。
Entry<K,V> e = new Entry<>(key, value, parent);
if (cmp < 0)
parent.left = e;
else
parent.right = e;
接着会调用 fixAfterInsertion(e); 进行一次修正。使得插入后的树还是符合红黑树的性质。(从一个节点到该节点的子孙节点的所有路径上包含相同数目的黑节点)。
下面来分析一下这个代码,看看红黑树是如何修正的。
private void fixAfterInsertion(Entry<K,V> x) {
//要插入的节点设置为红色
x.color = RED;
//进行递归处理 如果不为null 并且不是根 并且他父亲的颜色是红色
//如果父亲的颜色是黑色的话,不影响 你插入的颜色是红色
while (x != null && x != root && x.parent.color == RED) {
//父亲的颜色是红
if (parentOf(x) == leftOf(parentOf(parentOf(x)))) {//父亲是左孩子
//获取叔叔节点
Entry<K,V> y = rightOf(parentOf(parentOf(x)));
// 1.叔叔为红色
if (colorOf(y) == RED) {
setColor(parentOf(x), BLACK);//将父亲设置为黑色
setColor(y, BLACK); //将叔叔设置为黑色
setColor(parentOf(parentOf(x)), RED);//将爷爷设置为红色
x = parentOf(parentOf(x)); //将x变成爷爷
} else {
// 2.父亲节点是红色,叔叔节点是黑色,并且当前节点是父亲节点的右孩子
if (x == rightOf(parentOf(x))) {
x = parentOf(x);// 1.将当前节点设置为父亲节点
rotateLeft(x);// 2.进行左旋
//进过变换后 正好符合第三种情况
}
// 3.父亲是红色 叔叔是黑色 当且节点是左孩子
setColor(parentOf(x), BLACK); //将父亲变成黑色
setColor(parentOf(parentOf(x)), RED);//祖父变成红色
rotateRight(parentOf(parentOf(x)));//对祖父进行右旋
}
} else {
//如果父亲是右边的孩子
//获取叔叔 叔叔在左边
Entry<K,V> y = leftOf(parentOf(parentOf(x)));
//1.如果叔叔是红色的
if (colorOf(y) == RED) {
setColor(parentOf(x), BLACK);//父亲变黑
setColor(y, BLACK);//叔叔变黑
setColor(parentOf(parentOf(x)), RED);//祖父变红
x = parentOf(parentOf(x));// x变成祖父
} else {
//2.如果叔叔是黑色的 父亲是红色的 并且自己是左边的儿子
if (x == leftOf(parentOf(x))) { //当前节点是父亲的左边的节点
x = parentOf(x); //将父亲作为当前节点
rotateRight(x); //进行右旋
}
//3.如果叔叔是黑色的,父亲是红色的 并且自己是右边儿子
setColor(parentOf(x), BLACK); //父亲设置为黑色
setColor(parentOf(parentOf(x)), RED);//祖父设置为红色
rotateLeft(parentOf(parentOf(x))); //祖父进行左旋
}
}
}
root.color = BLACK; //将根设置为黑色
}