java红黑树操作问题、求大神指点
仝齾翯郻龏 2017-11-19 04:57:54 求大神指点
问题如下:
(此为java Treemap的deleteEntry源码)
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; //1、
p.value = s.value; //2、
p = s; //3、在上面1、2处是不是多余操作了?既然都直接将s覆盖p了,
//为何还用1、2的操作?这样是有何用意呢?
} // 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