1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
/**
* Removes the mapping for this key from this TreeMap if present.
*
* @param key key for which mapping should be removed
* @return the previous value associated with {@code key}, or
* {@code null} if there was no mapping for {@code key}.
* (A {@code null} return can also indicate that the map
* previously associated {@code null} with {@code key}.)
* @throws ClassCastException if the specified key cannot be compared
* with the keys currently in the map
* @throws NullPointerException if the specified key is null
* and this map uses natural ordering, or its comparator
* does not permit null keys
*/
public V remove(Object key) {
Entry<K,V> p = getEntry(key);
if (p == null)
return null;
V oldValue = p.value;
deleteEntry(p);
return oldValue;
}
/**
* Delete node p, and then rebalance the tree.
*/
private void deleteEntry(Entry<K,V> p) {
modCount++;
size--;
// If strictly internal, copy successor's element to p and then make p 如果严格的内部,将继承的元素复制到p,然后生成p
// point to successor.
if (p.left != null && p.right != null) {
Entry<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. 如果存在替换节点,请开始修复。
Entry<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; //gc
// Fix replacement
if (p.color == BLACK) // 删除的是黑色节点, 那么这个路径上就会少一个黑色节点,破坏黑红树特性5。
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;
}
}
}
/**
* Returns the successor of the specified Entry, or null if no such.
*
*/
static <K,V> TreeMap.Entry<K,V> successor(Entry<K,V> t) {
if (t == null)
return null;
else if (t.right != null) {
Entry<K,V> p = t.right;
while (p.left != null)
p = p.left;
return p;
} else {
Entry<K,V> p = t.parent;
Entry<K,V> ch = t;
while (p != null && ch == p.right) {
ch = p;
p = p.parent;
}
return p;
}
}
/** From CLR */
private void fixAfterDeletion(Entry<K,V> x) {
while (x != root && colorOf(x) == BLACK) {
if (x == leftOf(parentOf(x))) { //x节点为父节点左孩子
Entry<K,V> sib = rightOf(parentOf(x));
if (colorOf(sib) == RED) { //x的兄弟节点是红色;场景1:
setColor(sib, BLACK); //(1).将x的兄弟节点设为"黑色"。
setColor(parentOf(x), RED); //(2).将x的父节点设为"红色"。
rotateLeft(parentOf(x)); //(3).对x的父节点进行左旋。
sib = rightOf(parentOf(x)); //(4).左旋后,重新设置x的兄弟节点。
}
if (colorOf(leftOf(sib)) == BLACK && //x的兄弟节点是黑色,x的兄弟节点的两个孩子都是黑色;
colorOf(rightOf(sib)) == BLACK) { //场景2:
setColor(sib, RED); //(1).将x的兄弟节点设为"红色"。
x = parentOf(x); //(2).设置"x的父节点"为"新的x节点"。
} else {
if (colorOf(rightOf(sib)) == BLACK) { //x的兄弟节点是黑色;x的兄弟节点的左孩子是红色,右孩子是黑色的。场景3:
setColor(leftOf(sib), BLACK); //(1). 将x兄弟节点的左孩子设为"黑色"。
setColor(sib, RED); //(2). 将x兄弟节点设为"红色"。
rotateRight(sib); //(3). 对x的兄弟节点进行右旋。
sib = rightOf(parentOf(x)); //(4). 右旋后,重新设置x的兄弟节点。
}
//x的兄弟节点是黑色;x的兄弟节点的右孩子是红色的,x的兄弟节点的左孩子任意颜色。场景4:
setColor(sib, colorOf(parentOf(x))); //(1). 将x父节点颜色 赋值给x的兄弟节点。
setColor(parentOf(x), BLACK); //(2). 将x父节点设为"黑色"。
setColor(rightOf(sib), BLACK); //(3). 将x兄弟节点的右子节设为"黑色"。
rotateLeft(parentOf(x)); //(4). 对x的父节点进行左旋。
x = root; //(5). 设置"x"为"根节点"。
}
} else { // symmetric 红黑树对称的; x节点为父节点右孩子
Entry<K,V> sib = leftOf(parentOf(x));
if (colorOf(sib) == RED) { //x的兄弟节点是红色;场景1:
setColor(sib, BLACK);
setColor(parentOf(x), RED);
rotateRight(parentOf(x)); //(3).对x的父节点进行右旋。
sib = leftOf(parentOf(x));
}
if (colorOf(rightOf(sib)) == BLACK &&
colorOf(leftOf(sib)) == BLACK) {
setColor(sib, RED);
x = parentOf(x);
} else {
if (colorOf(leftOf(sib)) == BLACK) { //x的兄弟节点是黑色;x的兄弟节点的右孩子是红色,右孩子是黑色的。场景3:
setColor(rightOf(sib), BLACK);
setColor(sib, RED);
rotateLeft(sib); //(3). 对x的兄弟节点进行左旋。
sib = leftOf(parentOf(x));
}
//x的兄弟节点是黑色;x的兄弟节点的左孩子是红色的,x的兄弟节点的左孩子任意颜色。场景3:
setColor(sib, colorOf(parentOf(x)));
setColor(parentOf(x), BLACK);
setColor(leftOf(sib), BLACK);
rotateRight(parentOf(x)); //(4). 对x的父节点进行右旋。
x = root;
}
}
}
setColor(x, BLACK); //当前x设置为黑
}
|