java.util.TreeMap

主要是JDK源码阅读注释笔记

插入put和删除delete之后要调整红黑树的平衡

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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
private void fixAfterDeletion(Entry<K,V> x) 
{
while (x != root && colorOf(x) == BLACK)
{
//待删除结点是左结点
if (x == leftOf(parentOf(x)))
{
//sib是x兄弟结点
Entry<K,V> sib = rightOf(parentOf(x));
//case:1-5 sib是红结点 **新想法在最下面**
if (colorOf(sib) == RED)
{
setColor(sib, BLACK);
setColor(parentOf(x), RED);
rotateLeft(parentOf(x));
sib = rightOf(parentOf(x));
}
if (colorOf(leftOf(sib)) == BLACK && colorOf(rightOf(sib)) == BLACK)
{
setColor(sib, RED);
x = parentOf(x);
}

//sib是黑结点
else
{
//sib的右子黑
//case:1-2 & 1-4
if (colorOf(rightOf(sib)) == BLACK)
{
setColor(leftOf(sib), BLACK);
setColor(sib, RED);
rotateRight(sib);
sib = rightOf(parentOf(x));
//sib指向x.parent.right从而回到case:1-1
}
//sib的右子红
//case:1-1 & case:1-3
setColor(sib, colorOf(parentOf(x)));
setColor(parentOf(x), BLACK);
setColor(rightOf(sib), BLACK);
rotateLeft(parentOf(x));
x = root;
}
}
else
{ // 对称
Entry<K,V> sib = leftOf(parentOf(x));
//case:2-5
if (colorOf(sib) == RED)
{
setColor(sib, BLACK);
setColor(parentOf(x), RED);
rotateRight(parentOf(x));
sib = leftOf(parentOf(x));
}
if (colorOf(rightOf(sib)) == BLACK && colorOf(leftOf(sib)) == BLACK)
{
setColor(sib, RED);
x = parentOf(x);
}
//
else
{
//case:2-2
if (colorOf(leftOf(sib)) == BLACK)
{
setColor(rightOf(sib), BLACK);
setColor(sib, RED);
rotateLeft(sib);
sib = leftOf(parentOf(x));
}
//case:2-1
setColor(sib, colorOf(parentOf(x)));
setColor(parentOf(x), BLACK);
setColor(leftOf(sib), BLACK);
rotateRight(parentOf(x));
x = root;
}
}
}

setColor(x, BLACK);
}

//主要是寻找后继,所有都归结到 “删除的是叶子节点且该叶子节点是黑色的” 该情况进行修复。
private void deleteEntry(Entry<K,V> p) {
modCount++;
size--;

// If strictly internal, copy successor's element to p and then make 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;

// Fix replacement
if (p.color == BLACK)
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;
}
}
}


//主要完成了算法四上三种不同情况下的颜色平衡问题
//代码不同,思想类似
//x.color == RED 所以以x.parent.color是否为RED从下到上平衡
//x.color == RED && x.parent.colr ==RED
//此时需要考虑的就是:
//(1)x是左是右
//(2)x.parent是左是右
//(3)x.parent.brother是否RED
//由此分出四种情况,其中三种需要平衡
private void fixAfterInsertion(Entry<K,V> x)
{
//插入的结点都是红结点
x.color = RED;
//x.parent是红结点!
while (x != null && x != root && x.parent.color == RED)
{
//x.parent是左子
if (parentOf(x) == leftOf(parentOf(parentOf(x))))
{
//y是x爷结点的右结点
Entry<K,V> y = rightOf(parentOf(parentOf(x)));
// <3> x的爷结点左右子结点都是红结点(左右红链)-->flipColors(x的爷结点)
//也就是x的父结点(x爷结点左子)和y(x爷结点右子)变黑,x爷结点变红
if (colorOf(y) == RED)
//y红,x无论左右
{
setColor(parentOf(x), BLACK);
setColor(y, BLACK);
setColor(parentOf(parentOf(x)), RED);
//左右红链平衡好了,x指向原x的爷结点,继续while循环进行平衡
x = parentOf(parentOf(x));
}
//y黑,x是右子
else
{
// <2> x和x父结点都是红结点(右红链)-->左旋-->右旋
if (x == rightOf(parentOf(x)))
{
x = parentOf(x);
//左旋
rotateLeft(x);
}
//右旋
setColor(parentOf(x), BLACK);
setColor(parentOf(parentOf(x)), RED);
rotateRight(parentOf(parentOf(x)));
//此时变成左右双红链情况<1>,x父结点依然红,继续循环
}
}
//x.parent是右子
else
{
//y是x爷结点的左子
Entry<K,V> y = leftOf(parentOf(parentOf(x)));
// <3> 左右双红链 不需要管x是左是右
if (colorOf(y) == RED)
//y红,x无论左右
{
setColor(parentOf(x), BLACK);
setColor(y, BLACK);
setColor(parentOf(parentOf(x)), RED);
x = parentOf(parentOf(x));
}
else
//y黑,x左
{
// x和x父结点都红,x是左子
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;
}

//就是接包袱,然后认爹
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;
}
}


//红链左旋和算法四类似,只是多了parent变量的维护,少了颜色和size的维护
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;
}
}

//插入
public V put(K key, V value) {
Entry<K,V> t = root;
//空树或者查找微命中 new Entry()
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);
}
//算法四采用递归,put最开始的判断null的语句可以实现新结点插入
//源码采用do-while循环,没有递归调用put,所以需要new Entry()插在树底
//parent就是游标Entry t的父结点
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;
}

//TreeMap.Entry类的一部分,相比于算法四中的Node类多了parent变量需要维护。
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;
}

//返回t的后继(successor) 相当于算法四P260 min(t.right)
static <K,V> TreeMap.Entry<K,V> successor(Entry<K,V> t) {
if (t == null)
return null;
//t的右子树中最小的就是t的后继
else if (t.right != null) {
Entry<K,V> p = t.right;
while (p.left != null)
p = p.left;
return p;
}
//要是t没有右子树
//如果t是左子树,t的后继是t.parent
//如果t是右子树,t的后继是t.parent.parent...直到转弯(画图自己悟吧)也就是不再满足while条件
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;
}
}
1
2
3
4
5
6
7
//对于case:1-5的新想法
if (colorOf(sib) == RED)
{
setColor(sib, BLACK);
setColor(leftOf(sib), RED);
rotateLeft(parentOf(x));
}
Donate here.