入鄉隨俗 先擺上紅黑樹的四(或者五)大規則
規則是在進行更改紅黑樹結構(左旋 右旋)所遵循的
- 一、紅黑樹四大特性
- 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;
}
}