Java 代码生成树状图

2023-07-08 18:54:33 浏览数 (3)

实现

代码语言:javascript复制
import lombok.Data;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Data
public class TreeNode {
    private int id;
    private int dataLevel;
    private String dataName;
    private String alias;
    private int code;
    private int parentCode;
    private List<TreeNode> children;

    public TreeNode(int id, int dataLevel, String dataName, String alias, int code, int parentCode) {
        this.id = id;
        this.dataLevel = dataLevel;
        this.dataName = dataName;
        this.alias = alias;
        this.code = code;
        this.parentCode = parentCode;
        this.children = new ArrayList<>();
    }

    public void addChild(TreeNode child) {
        children.add(child);
    }

    public void sortChildren(Comparator<TreeNode> comparator) {
        children.sort(comparator);
        for (TreeNode child : children) {
            child.sortChildren(comparator);
        }
    }

    public List<TreeNode> buildTree(List<TreeNode> nodes) {
        Map<Integer, TreeNode> nodeMap = new HashMap<>();
        List<TreeNode> rootNodes = new ArrayList<>();

        for (TreeNode node : nodes) {
            nodeMap.put(node.getCode(), node);
        }

        for (TreeNode node : nodes) {
            int parentCode = node.getParentCode();
            if (parentCode == 0) {
                rootNodes.add(node);
            } else {
                TreeNode parent = nodeMap.get(parentCode);
                if (parent != null) {
                    parent.addChild(node);
                }
            }
        }

        for (TreeNode rootNode : rootNodes) {
            rootNode.sortChildren(Comparator.comparing(TreeNode::getCode));
        }

        return rootNodes;
    }

    public static void main(String[] args) {
        // 示例数据
        List<TreeNode> nodes = new ArrayList<>();
        nodes.add(new TreeNode(0, 1, "Node 1", "Alias 11", 100, 0));
        nodes.add(new TreeNode(0, 1, "Node 1", "Alias 11", 101, 0));
        nodes.add(new TreeNode(1, 1, "Node 1", "Alias 12", 102, 0));
        nodes.add(new TreeNode(2, 2, "Node 2", "Alias 2", 10101, 101));
        nodes.add(new TreeNode(3, 2, "Node 3", "Alias 3", 10201, 102));
        nodes.add(new TreeNode(4, 3, "Node 4", "Alias 4", 1010101, 10101));
        nodes.add(new TreeNode(5, 3, "Node 5", "Alias 5", 1010102, 10101));
        nodes.add(new TreeNode(6, 3, "Node 6", "Alias 6", 1020101, 10201));

        TreeNode root = new TreeNode(0, 0, "Root", "", 0, 0);
        List<TreeNode> tree = root.buildTree(nodes);

        // 打印树状结构
        for (TreeNode node : tree) {
            printNode(node, 0);
        }
    }

    private static void printNode(TreeNode node, int level) {
        StringBuilder indent = new StringBuilder();
        for (int i = 0; i < level; i  ) {
            indent.append("  ");
        }

        System.out.println(indent.toString()   "Code: "   node.getCode()   ", Data Name: "   node.getDataName());

        for (TreeNode child : node.getChildren()) {
            printNode(child, level   1);
        }
    }
}

controller 层调用

代码语言:javascript复制
  @ApiOperation(value = "获取树状图", httpMethod = "POST")
    @RequestMapping("/getTreeData")
    @ResponseBody
    public Result getTreeData(){

        // 示例数据
        List<TreeNode> nodes = new ArrayList<>();
        nodes.add(new TreeNode(0, 1, "Node 1", "Alias 11", 100, 0));
        nodes.add(new TreeNode(0, 1, "Node 1", "Alias 11", 101, 0));
        nodes.add(new TreeNode(1, 1, "Node 1", "Alias 12", 102, 0));
        nodes.add(new TreeNode(2, 2, "Node 2", "Alias 2", 10101, 101));
        nodes.add(new TreeNode(3, 2, "Node 3", "Alias 3", 10201, 102));
        nodes.add(new TreeNode(4, 3, "Node 4", "Alias 4", 1010101, 10101));
        nodes.add(new TreeNode(5, 3, "Node 5", "Alias 5", 1010102, 10101));
        nodes.add(new TreeNode(6, 3, "Node 6", "Alias 6", 1020101, 10201));

        TreeNode root = new TreeNode(0, 0, "Root", "", 0, 0);
        List<TreeNode> tree = root.buildTree(nodes);


        return ResultUtil.success(tree);

    }

0 人点赞