Java 代码生成树状图
导读:实现import lombok.Data; import java.util.ArrayList; import java.util.Comparator; import java.util.HashMap; import java.ut...
实现
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 ListTreeNode>
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(ComparatorTreeNode>
comparator) {
children.sort(comparator);
for (TreeNode child : children) {
child.sortChildren(comparator);
}
}
public ListTreeNode>
buildTree(ListTreeNode>
nodes) {
MapInteger, TreeNode>
nodeMap = new HashMap>
();
ListTreeNode>
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) {
// 示例数据
ListTreeNode>
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);
ListTreeNode>
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 层调用
@ApiOperation(value = "获取树状图", httpMethod = "POST")
@RequestMapping("/getTreeData")
@ResponseBody
public Result getTreeData(){
// 示例数据
ListTreeNode>
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);
ListTreeNode>
tree = root.buildTree(nodes);
return ResultUtil.success(tree);
}
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: Java 代码生成树状图
本文地址: https://pptw.com/jishu/504571.html