入乡随俗 先摆上红黑树的四(或者五)大规则
规则是在进行更改红黑树结构(左旋 右旋)所遵循的
- 一、红黑树四大特性
- 1.根节点是黑色。
- 2.每个叶节点(NIL节点,空节点)是黑色的。
- 3.每个红色节点的两个子节点都是黑色。(从每个叶子到根的所有路径上不能有两个连续的红色节点)
- 4.从任一节点到其每个叶子的所有路径都包含相同数目的黑色节点。
- 二、 红黑树的插入操作:
-
- treeMap底层的实现为红黑树 代码说话:
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; // 父节点
// Red-black mechanics
// private static final boolean RED = false;
// private static final boolean BLACK = true;
boolean color = BLACK; // 颜色 true 为黑 false 为红
/**
* 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;
}
}
-
- 红黑树的插入数据操作
取自 1.8版本
public V put(K key, V value) {
Entry<K,V> t = root; // root 为根节点
if (t == null) { // 如果root 为空 则第一次插入数据
compare(key, key); // type (and possibly null) check 仅仅是为了检查是否为空
root = new Entry<>(key, value, null); // 直接根节点为此节点 size 设为一 modCount ++ 返回
size = 1;
modCount++; // 此字段在HASHMAP中有用到 某一个索引下插入的数据过多 转换为红黑树所用到 treemap本人没做太多研究
return null;
}
// root 不为空 即非首次插入
int cmp;
Entry<K,V> parent;
// split comparator and comparable paths
Comparator<? super K> cpr = comparator; // 创建一个比较器 个人感觉 下面的比较器写在一个判断中 然后对key进行check
/** 比如这样
* Comparator<? super K> cpr = comparator ;
* if(cpr==null){
* // 进行创建 cpr
* }
* if(key == null){
* throw new NullPointerException();
* }
**/
if (cpr != null) {
do {
parent = t;
cmp = cpr.compare(key, t.key); // 拿索要插入的key 与 rootkey 进行比较
// 根据规则寻找插入位置
if (cmp < 0) // 取出左节点 继续遍历该分支
t = t.left;
else if (cmp > 0) // 取出右节点 继续遍历该分支
t = t.right;
else // cmp = 0 即 索要插入的key==root.key 进行value的覆盖即可
return t.setValue(value); // 直接返回
} while (t != null); // 直到找到一个空节点 寻址完毕 即 t = 该空节点 准备插入操作
}
else {
if (key == null) // 不用创建 cmp 则需要check key
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); // parent 为一个空节点 有父节点
if (cmp < 0)
parent.left = e;
else
parent.right = e;
fixAfterInsertion(e); // 此处就重点来了 划重点 对红黑树的结构做修正(因为插入数据可能造成五大性质中某一条或者多条性质发生改变, 进行左旋、右旋操作。后续再讲)
size++;
modCount++;
return null;
}
- 3.红黑树的颜色修正
-
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; 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; } // 比较器存在 直接拿数据 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; }
- 4.红黑树的左旋右旋
private void fixAfterInsertion(Entry<K,V> x) {
x.color = RED;// 首先进行该节点赋予颜色 默认红 然后对此树结构进行判断是否遵循五大特性
while (x != null && x != root && x.parent.color == RED) { //此处x节点为红色 父节点也为红色 则违背 特性四 进行变色了 哈哈哈
// 把三代颜色更换 再遍历查看是否符合规则 如果符合返回
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)); // 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;
}
- 5、左旋 右旋 左旋右旋看其他博客的图片介绍就OK
/** From CLR */
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;
}
}
/** From CLR */
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;
}
}