Java & JavaScript 递归构建树状图(根据parentId)

1. Java & JavaScript 递归构建树状图(根据parentId)


类似于实现这样的效果

实现思路:

  1. 第一步:找到所有根节点(parentId == null)
  2. 第二步:遍历跟结点,递归生成根节点的子树

Java & JavaScript 递归构建树状图(根据parentId)_第1张图片


1.1 JavaScript 实现

export class TaskManageModal {
     
  taskId: any = ''; // 任务id
  parentId: any = ''; // 父级任务id
  children: Array<TaskManageModal> = [];// 是否含有子列表
}

taskInfoList: Array<TaskManageModal> = 待处理的数据;

//建立树形结构
buildTree(): Array<TaskManageModal> {
     
  let treeList: Array<TaskManageModal> = [];
  let rootNodes = this.getRootNode();
  for (let j = 0; j < rootNodes.length; j++) {
     
    treeList.push(this.buildChildTree(rootNodes[j]));
  }
  return treeList;
}
// 获取根节点(一级树)
getRootNode(): Array<TaskManageModal> {
     
  let rootList: Array<TaskManageModal> = [];
  for (let j = 0; j < this.taskInfoList.length; j++) {
     
    if (this.taskInfoList[j].parentId == undefined) {
     
      rootList.push(this.taskInfoList[j]);
    }
  }
  return rootList;
}
// 递归,建立子树形结构
buildChildTree(task: TaskManageModal): TaskManageModal {
     
  let childList: Array<TaskManageModal> = [];
  for (let j = 0; j < this.taskInfoList.length; j++) {
     
    if (this.taskInfoList[j].parentId == task.taskId) {
     
      childList.push(this.buildChildTree(this.taskInfoList[j]));
    }
  }
  task.children = childList;
  return task;
}

1.2 Java代码实现

我用到的依赖

pom.xml

        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>fastjsonartifactId>
            <version>1.2.60version>
        dependency>
        
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <version>1.18.20version>
            <scope>providedscope>
        dependency>

1.2.1 实体类Menu

Menu.java

import lombok.Data;

import java.util.List;

@Data
public class Menu {
     
    private String id;
    private String parentId;
    private String text;
    private List<Menu> children;

    public Menu(String id, String parentId, String text) {
     
        this.id = id;
        this.parentId = parentId;
        this.text = text;
    }
}

1.2.2 生成树形结构的类

MenuTree.java

import java.util.ArrayList;
import java.util.List;

public class MenuTree {
     
    private List<Menu> menuList = new ArrayList<Menu>();

    public MenuTree(List<Menu> menuList) {
     
        this.menuList = menuList;
    }

    //建立树形结构
    public List<Menu> builTree() {
     
        List<Menu> treeMenus = new ArrayList<Menu>();
        for (Menu menuNode : getRootNode()) {
     
            menuNode = buildChilTree(menuNode);
            treeMenus.add(menuNode);
        }
        return treeMenus;
    }

    //递归,建立子树形结构
    private Menu buildChilTree(Menu pNode) {
     
        List<Menu> chilMenus = new ArrayList<Menu>();
        for (Menu menuNode : menuList) {
     
            if (menuNode.getParentId().equals(pNode.getId())) {
     
                chilMenus.add(buildChilTree(menuNode));
            }
        }
        pNode.setChildren(chilMenus);
        return pNode;
    }

    //获取根节点
    private List<Menu> getRootNode() {
     
        List<Menu> rootMenuLists = new ArrayList<Menu>();
        for (Menu menuNode : menuList) {
     
            if (menuNode.getParentId().equals("0")) {
     
                rootMenuLists.add(menuNode);
            }
        }
        return rootMenuLists;
    }
}

1.2.3 测试类

Test.java

import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.List;

public class Test {
     
    public static void main(String[] args) {
     
        List<Menu> menuList = new ArrayList<Menu>();
        /*插入一些数据*/
        menuList.add(new Menu("GN001D000", "0", "系统管理"));
        menuList.add(new Menu("GN001D100", "GN001D000", "权限管理"));
        menuList.add(new Menu("GN001D110", "GN001D100", "密码修改"));
        menuList.add(new Menu("GN001D120", "GN001D100", "新加用户"));
        menuList.add(new Menu("GN001D200", "GN001D000", "系统监控"));
        menuList.add(new Menu("GN001D210", "GN001D200", "在线用户"));
        menuList.add(new Menu("GN002D000", "0", "订阅区"));
        menuList.add(new Menu("GN003D000", "0", "未知领域"));
        /*让我们创建树*/
        MenuTree menuTree = new MenuTree(menuList);
        menuList = menuTree.builTree();
        /*转为json看看效果*/
        String jsonOutput = JSON.toJSONString(menuList);
        System.out.println(jsonOutput);
    }
}

运行结果:
在这里插入图片描述



你可能感兴趣的:(Java,JS,java,js,数据结构,递归,树)