guanxf

我的博客:http://blog.sina.com.cn/17learning

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  71 随笔 :: 1 文章 :: 41 评论 :: 0 Trackbacks

createTree(1, orgNodeTree, sameOrgNodes, 0);


@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
public class NodeTree {
private String pName;
private String name;
private int level;
private List<NodeTree> children;
}

private void createTree(int leave, int ind, Map<String, NodeTree> pIndexNodeNameMap, List<NodeVo> childNodes) {
Map<String, NodeTree> cIndexNodeNameMap = new HashMap();
//构建树
int treeNo = pIndexNodeNameMap.size();
if (treeNo == 0) {
return;
}
int group = 0;
for (int i = ind; i < childNodes.size(); i++) {
NodeVo node = childNodes.get(i);
long index = node.getId() % treeNo;
NodeTree pNode = pIndexNodeNameMap.get(index + "");
List<NodeTree> children = pNode.getChildren();
if (CollectionUtils.isEmpty(children)) {
children = new ArrayList();
}
if (children.size() > 2) {
leave++;
createTree(leave, i, cIndexNodeNameMap, childNodes);
break;
} else {
NodeTree child = new NodeTree();
child.setLevel(leave);
child.setPName(pNode.getName());
child.setName(node.getNodeName());
children.add(child);
pNode.setChildren(children);
cIndexNodeNameMap.put(group + "", child);
group++;
}
}
}


private boolean createTree(int level, List<NodeTree> parentNodes, List<NodeVo> childNodes, int beginIndex) {
//构建树
List<NodeTree> nextLevelNodes = new ArrayList<>();
for (int i = beginIndex; i < childNodes.size(); i++) {
int parentCount = 1;
for (NodeTree pNode : parentNodes) {
List<NodeTree> children = pNode.getChildren();
if (CollectionUtils.isEmpty(children)) {
children = new ArrayList();
pNode.setChildren(children);
}
if (children.size() >= 3) {
if(parentCount >= parentNodes.size()){
return createTree(++level, nextLevelNodes, childNodes, beginIndex);
}
} else {
if (beginIndex >= childNodes.size()) {
return true;
}
NodeTree child = new NodeTree();
child.setLevel(level);
child.setPName(pNode.getName());
NodeVo node = childNodes.get(beginIndex);
child.setName(node.getNodeName());
pNode.getChildren().add(child);
nextLevelNodes.add(child);
beginIndex++;
}
parentCount++;
}
}
return true;
}
posted on 2020-09-07 09:56 管先飞 阅读(246) 评论(0)  编辑  收藏 所属分类: Java技术andriod开发

只有注册用户登录后才能发表评论。


网站导航: