什么是递归树
如果我们把这个一层一层的分解过程画成图,它其实就是一棵树。我们给这棵树起一个名字,叫作递归树。
JSON格式结果
[
{
"children": [
{
"children": [
{
"children": [
],
"id": "GN001D110",
"parentId": "GN001D100",
"status": "Y",
"text": "密码修改",
"url": "/admin"
},
{
"children": [
],
"id": "GN001D120",
"parentId": "GN001D100",
"status": "Y",
"text": "新加用户",
"url": "/admin"
}
],
"id": "GN001D100",
"parentId": "GN001D000",
"status": "Y",
"text": "权限管理",
"url": "/admin"
},
{
"children": [
{
"children": [
],
"id": "GN001D210",
"parentId": "GN001D200",
"status": "Y",
"text": "在线用户",
"url": "/admin"
}
],
"id": "GN001D200",
"parentId": "GN001D000",
"status": "Y",
"text": "系统监控",
"url": "/admin"
}
],
"id": "GN001D000",
"parentId": "0",
"status": "Y",
"text": "系统管理",
"url": "/admin"
},
{
"children": [
],
"id": "GN002D000",
"parentId": "0",
"status": "Y",
"text": "订阅区",
"url": "/admin"
},
{
"children": [
],
"id": "GN003D000",
"parentId": "0",
"status": "Y",
"text": "未知领域",
"url": "/admin"
}
]
Java demo
Meun.java
@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class Menu implements Serializable {
private static final long serialVersionUID = -268621224139252995L;
private String id;
private String parentId;
private String text;
private String url;
private String status;
private List<Menu> children;
public Menu(String id, String parentId, String text, String url, String status) {
this.id = id;
this.parentId = parentId;
this.text = text;
this.url = url;
this.status = status;
}
}
MenuTree.java
public class MenuTree {
private List<Menu> menuList = new ArrayList<>();
public MenuTree(List<Menu> menuList) {
this.menuList = menuList;
}
// 建立树形结构
public List<Menu> buildTree() {
ArrayList<Menu> treeMenus = new ArrayList<>();
for (Menu menuNode : getRootNode()) {
menuNode = buildChildTree(menuNode);
treeMenus.add(menuNode);
}
return treeMenus;
}
// 递归,建立子树形结构
private Menu buildChildTree(Menu pNode) {
List<Menu> childMenus = new ArrayList<>();
for (Menu menuNode : menuList) {
if (menuNode.getParentId().equals(pNode.getId())) {
childMenus.add(buildChildTree(menuNode));
}
}
pNode.setChildren(childMenus);
return pNode;
}
// 获取根节点
private List<Menu> getRootNode() {
ArrayList<Menu> rootMenuLists = new ArrayList<>();
for (Menu menuNode : menuList) {
if (menuNode.getParentId().equals("0")) {
rootMenuLists.add(menuNode);
}
}
return rootMenuLists;
}
}
Hello.java
public class Hello {
public static void main(String[] args) {
List<Menu> menuList = new ArrayList<>();
menuList.add(new Menu("GN001D000", "0", "系统管理", "/admin", "Y"));
menuList.add(new Menu("GN001D100", "GN001D000", "权限管理", "/admin", "Y"));
menuList.add(new Menu("GN001D110", "GN001D100", "密码修改", "/admin", "Y"));
menuList.add(new Menu("GN001D111", "GN001D110", "monologue", "/admin", "Y"));
menuList.add(new Menu("GN001D112", "GN001D111", "monster", "/admin", "Y"));
menuList.add(new Menu("GN001D113", "GN001D112", "1111111", "/admin", "Y"));
menuList.add(new Menu("GN001D114", "GN001D113", "2222222", "/admin", "Y"));
menuList.add(new Menu("GN001D115", "GN001D114", "3333333", "/admin", "Y"));
menuList.add(new Menu("GN001D120", "GN001D100", "新加用户", "/admin", "Y"));
menuList.add(new Menu("GN001D200", "GN001D000", "系统监控", "/admin", "Y"));
menuList.add(new Menu("GN001D210", "GN001D200", "在线用户", "/admin", "Y"));
menuList.add(new Menu("GN002D000", "0", "订阅区", "/admin", "Y"));
menuList.add(new Menu("GN003D000", "0", "未知领域", "/admin", "Y"));
MenuTree menuTree = new MenuTree(menuList);
menuList = menuTree.buildTree();
String jsonOutput = JSON.toJSONString(menuList);
System.out.println(jsonOutput);
}
}