This commit is contained in:
huangchengxing 2022-07-04 18:31:44 +08:00
parent 81cbb0d8a0
commit 04b6c3bcdc
2 changed files with 10 additions and 9 deletions

View File

@ -128,9 +128,10 @@ public class LinkedForestMap<K, V> implements ForestMap<K, V> {
// 2.将目标的子节点直接将目标的父节点作为父节点 // 2.将目标的子节点直接将目标的父节点作为父节点
if (target.hasParent()) { if (target.hasParent()) {
final TreeEntryNode<K, V> parent = target.getDeclaredParent(); final TreeEntryNode<K, V> parent = target.getDeclaredParent();
final Map<K, TreeEntry<K, V>> targetChildren = target.getChildren();
parent.removeDeclaredChild(target.getKey()); parent.removeDeclaredChild(target.getKey());
target.getDeclaredChildren() target.clear();
.forEach((k, c) -> parent.addChild((TreeEntryNode<K, V>)c)); targetChildren.forEach((k, c) -> parent.addChild((TreeEntryNode<K, V>)c));
} }
return target; return target;
} }
@ -577,6 +578,7 @@ public class LinkedForestMap<K, V> implements ForestMap<K, V> {
), null); ), null);
// 调整该节点的信息 // 调整该节点的信息
child.parent = this;
child.traverseChildNodes(true, (i, c) -> { child.traverseChildNodes(true, (i, c) -> {
c.root = getRoot(); c.root = getRoot();
c.weight = i + getWeight() + 1; c.weight = i + getWeight() + 1;
@ -598,14 +600,13 @@ public class LinkedForestMap<K, V> implements ForestMap<K, V> {
} }
// 断开该节点与其父节点的关系 // 断开该节点与其父节点的关系
child.getDeclaredParent().children.remove(key); this.children.remove(key);
// 将子节点从当前节点中移除并重置子节点及其下属节点的相关属性 // 重置子节点及其下属节点的相关属性
children.remove(child.getKey());
child.parent = null; child.parent = null;
child.traverseChildNodes(true, (index, node) -> { child.traverseChildNodes(true, (i, c) -> {
node.root = child; c.root = child;
node.weight = index; c.weight = i;
}, null); }, null);
} }

View File

@ -119,7 +119,7 @@ public interface TreeEntry<K, V> extends Map.Entry<K, V> {
* @return 是否 * @return 是否
*/ */
default boolean hasChildren() { default boolean hasChildren() {
return CollUtil.isEmpty(getDeclaredChildren()); return CollUtil.isNotEmpty(getDeclaredChildren());
} }
/** /**