Java 递归 构造树形结构

Java 递归 构造树形结构

 

 

 

 1、实现思路:

1.首先从所有数据中获取所有的根节点。
2.为根节点建立次级子树并拼接上。
3.递归为子节点建立次级子树并接上,直至为末端节点拼接上空的“树”。

 

2、实现代码

1、树结构

/**
 * @Author
 * @ClassName Node
 * @Description 树数据结构
 * @Date 2023/12/17 11:22
 * @Version 1.0
 */
@Data
public class Node {

    /**
     * 节点名称
     */
    private String nodeName;
    /**
     * 当前节点id
     */
    private String nodeId;
    /**
     * 父节点id
     */
    private String parentId;

    /**
     * 是否是根节点
     */
    private boolean isRootNode;

    /**
     * 子节点
     */
    private List<Node> children;

    public Node (String nodeName, String nodeId, String parentId, boolean isRootNode) {
        this.nodeName = nodeName;
        this.nodeId = nodeId;
        this.parentId = parentId;
        this.isRootNode = isRootNode;
    }
}

 

2、构建树操作

/**
 * @Author
 * @ClassName BuildTree
 * @Description 构建树的相关实现
 * @Date 2023/12/17 13:03
 * @Version 1.0
 */
public class BuildTree {

    /**
     * 构成树的所有数据
     */
    private final List<Node> nodeList;

    public BuildTree (List<Node> nodes) {
        this.nodeList = nodes;
    }

    /**
     * 建立树形结构
     *
     * @return
     */
    public List<Node> buildTree() {
        List<Node> rootNode = getRootNode();
        for (Node node : rootNode) {
            this.buildChildTree(node);
        }
        return rootNode;
    }

    /**
     * 获取所有的根节点
     *
     * @return
     */
    private List<Node> getRootNode() {
        return nodeList.stream().filter(Node::isRootNode).collect(Collectors.toList());
    }

    /**
     * 递归,建立子树形结构
     *
     * @param pNode
     * @return
     */
    private Node buildChildTree(Node pNode) {
        List<Node> childMenus = new ArrayList<>();
        for (Node node : nodeList) {
            if (node.getParentId().equals(pNode.getNodeId())) {
                childMenus.add(buildChildTree(node));
            }
        }
        pNode.setChildren(childMenus);
        return pNode;
    }
}

 

3、客户端测试

/**
 * @Author
 * @ClassName Client
 * @Description 客户端测试
 * @Date 2023/12/17 13:24
 * @Version 1.0
 */
public class Client {

    public static void main(String[] args) throws JsonProcessingException {
        List<Node> nodeList= new ArrayList<>();
        /*插入一些数据测试*/
        nodeList.add(new Node("A公司","1","",true));
        nodeList.add(new Node("B公司","2","",true));
        nodeList.add(new Node("C公司","3","",true));
        nodeList.add(new Node("A-部门1","1-1","1",false));
        nodeList.add(new Node("A-部门2","1-2","1",false));
        nodeList.add(new Node("A-部门2-1","1-2-1","1-2",false));
        nodeList.add(new Node("B-部门1","2-1","2",false));
        nodeList.add(new Node("B-部门2","2-2","2",false));
        /*让我们创建树*/
        BuildTree treeBuild =new BuildTree(nodeList);
        List<Node> nodes = treeBuild.buildTree();
        /*转为json看看效果*/
        ObjectMapper mapper = new ObjectMapper();
        System.out.println(mapper.writeValueAsString(nodes));
    }
}

 

4、测试结果

[{
    "nodeName": "A公司",
    "nodeId": "1",
    "parentId": "",
    "children": [{
        "nodeName": "A-部门1",
        "nodeId": "1-1",
        "parentId": "1",
        "children": [],
        "rootNode": false
    }, {
        "nodeName": "A-部门2",
        "nodeId": "1-2",
        "parentId": "1",
        "children": [{
            "nodeName": "A-部门2-1",
            "nodeId": "1-2-1",
            "parentId": "1-2",
            "children": [],
            "rootNode": false
        }],
        "rootNode": false
    }],
    "rootNode": true
}, {
    "nodeName": "B公司",
    "nodeId": "2",
    "parentId": "",
    "children": [{
        "nodeName": "B-部门1",
        "nodeId": "2-1",
        "parentId": "2",
        "children": [],
        "rootNode": false
    }, {
        "nodeName": "B-部门2",
        "nodeId": "2-2",
        "parentId": "2",
        "children": [],
        "rootNode": false
    }],
    "rootNode": true
}, {
    "nodeName": "C公司",
    "nodeId": "3",
    "parentId": "",
    "children": [],
    "rootNode": true
}]

 

posted @ 2018-10-31 00:08  邓维-java  阅读(8510)  评论(2编辑  收藏  举报