java - 否则 master 将永远不会在树形图中的 successor() 中执行
问题描述
我正在学习树形图中 remove() 的源代码。但是有一点我无法理解。
//.......................忽略主要代码,留下这些
private void deleteEntry(Entry<K,V> p) {
if (p.left != null && p.right != null) {
Entry<K,V> s = successor(p);
p.key = s.key;
p.value = s.value;
p = s;
}
}
static <K,V> TreeMap.Entry<K,V> successor(Entry<K,V> t) {`enter code here`
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;
}
}
我对 deleteEntry 函数感到困惑,p 有 2 个孩子。P.left 和 P.right 都不为空。但为什么要判断 t.right 在后继函数中不为空?我的意思是这是绝对的事实。而且因为 t.right 必须不为空。代码永远不会在后继功能中的 else master 中执行。
谁打电话告诉我我的问题在哪里?感谢你们。
解决方案
如果您查看 TreeMap.java ,您会知道从很多地方调用了后继者,delete() 就是这样一个地方。
推荐阅读
- sql - oracle sql中选择具有不同值的列的方法
- react-native - 可触摸的不透明度事件处理停止在高程上工作
- typescript - 如何解决打字稿中隐含的任何类型以索引类型错误?
- mrtk - 如何调试 MRTK UWP IL2CPP?
- angular - 输入后无法在另一个子组件上更新子组件数据
- javascript - 样条图中一个类别中的多个数据
- sql - Spark SQL中PIVOT操作的时间和空间复杂度是多少?
- couchbase - Couchbase 超时但未登录系统:completed_requests
- mysql - 如何为多个子查询赋予特定值?
- c# - 如何在依赖属性的 PropertyChangedCallback 中绑定异步方法?