前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java 代码生成树状图

Java 代码生成树状图

作者头像
一写代码就开心
发布2023-07-08 18:54:33
3100
发布2023-07-08 18:54:33
举报
文章被收录于专栏:java和pythonjava和python

实现

代码语言: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);

    }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2023-07-03,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 实现
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档