Skip to content

Commit

Permalink
feat-TreeBuilder-可以对死循环递归的树进行判定跳出
Browse files Browse the repository at this point in the history
  • Loading branch information
aruis committed Oct 22, 2024
1 parent 98069b3 commit 55c1f94
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
14 changes: 14 additions & 0 deletions muyun-core/src/main/java/net/ximatai/muyun/model/TreeNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,18 @@ public TreeNode setChildren(List<TreeNode> children) {
return this;
}

@Override
public int hashCode() {
return getId().hashCode();
}

@Override
public boolean equals(Object obj) {
if (obj instanceof TreeNode node) {
return this.getId().equals(node.getId());
} else {
return false;
}
}

}
17 changes: 14 additions & 3 deletions muyun-core/src/main/java/net/ximatai/muyun/util/TreeBuilder.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package net.ximatai.muyun.util;

import net.ximatai.muyun.core.exception.MyException;
import net.ximatai.muyun.model.TreeNode;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public class TreeBuilder {
Expand Down Expand Up @@ -35,8 +38,10 @@ public static List<TreeNode> build(String pkColumn, String parentKeyColumn, List
Map<String, List<Map<String, Object>>> groupedByParentKey = list.stream()
.collect(Collectors.groupingBy(item -> (String) item.get(parentKeyColumn)));

Set<TreeNode> nodes = new HashSet<>();

// 构建树形结构的递归方法
List<TreeNode> treeNodeList = buildChildren(groupedByParentKey, pkColumn, parentKeyColumn, rootID, labelColumn, 1, maxLevel);
List<TreeNode> treeNodeList = buildChildren(groupedByParentKey, pkColumn, parentKeyColumn, rootID, labelColumn, 1, maxLevel, nodes);

if (showMe) {
if (rootID.equals(ROOT_PID)) {
Expand All @@ -53,7 +58,7 @@ public static List<TreeNode> build(String pkColumn, String parentKeyColumn, List
}

private static List<TreeNode> buildChildren(Map<String, List<Map<String, Object>>> groupedByParentKey, String pkColumn, String parentKeyColumn,
String currentRootID, String labelColumn, int currentLevel, int maxLevel) {
String currentRootID, String labelColumn, int currentLevel, int maxLevel, Set<TreeNode> nodes) {
if (currentLevel > maxLevel) {
return Collections.emptyList(); // 超过最大层级,返回空列表
}
Expand All @@ -68,9 +73,15 @@ private static List<TreeNode> buildChildren(Map<String, List<Map<String, Object>
.setLabel((String) node.get(labelColumn))
.setData(node);

if (nodes.contains(treeNode)) {
throw new MyException("树节点出现递归调用");
} else {
nodes.add(treeNode);
}

// 递归构建子节点
List<TreeNode> childNodes = buildChildren(groupedByParentKey, pkColumn, parentKeyColumn,
(String) node.get(pkColumn), labelColumn, currentLevel + 1, maxLevel);
(String) node.get(pkColumn), labelColumn, currentLevel + 1, maxLevel, nodes);
treeNode.setChildren(childNodes);

return treeNode;
Expand Down

0 comments on commit 55c1f94

Please sign in to comment.