org.macrocloud.kernel.toolkit.node.ForestNodeMerger Maven / Gradle / Ivy
package org.macrocloud.kernel.toolkit.node;
import java.util.List;
/**
* 森林节点归并类.
*
* @author macro
*/
public class ForestNodeMerger {
/**
* 将节点数组归并为一个森林(多棵树)(填充节点的children域)
* 时间复杂度为O(n^2).
*
* @param the generic type
* @param items 节点域
* @return 多棵树的根节点集合
*/
public static > List merge(List items) {
ForestNodeManager forestNodeManager = new ForestNodeManager<>(items);
items.forEach(forestNode -> {
if (forestNode.getParentId() != 0) {
INode node = forestNodeManager.getTreeNodeAt(forestNode.getParentId());
if (node != null) {
node.getChildren().add(forestNode);
} else {
forestNodeManager.addParentId(forestNode.getId());
}
}
});
return forestNodeManager.getRoot();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy