package tree;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.collections.CollectionUtils;
public class TreeBuilder {
/**
* 将集合建立成树结构
*
* @param dirs
* @return
*/
@SuppressWarnings("unchecked")
private List<Node> buildListToTree(List<Node> dirs) {
List<Node> roots = findRoots(dirs);
List<Node> notRoots = (List<Node>) CollectionUtils.subtract(dirs, roots);
for (Node root : roots) {
root.setChildren(findChildren(root, notRoots));
}
return roots;
}
/**
* 找出集合中的根元素
*
* @param allDirs
* @return
*/
public List<Node> findRoots(List<Node> allNodes) {
List<Node> results = new ArrayList<Node>();
for (Node node : allNodes) {
boolean isRoot = true;
java对于树的构建
最新推荐文章于 2024-07-30 21:12:55 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)