java 中树形结构如何实现 ?请认真思考。
package org.belen.tree;
import java.time.LocalDateTime;
public class Node {
private Integer id;
// 分类名称
private String name;
private String tag;
// 图标
private String logo;
// 级别
private Integer level;
// 路径
private String path;
// 描述
private String description;
// 操作员编号
private Integer creatorId;
// 状态
private boolean status;
// 创建时间
private LocalDateTime createTime;
// 排序号
private Integer sort;
// 父类
private Integer parentId;
public Node(Integer id, String name, boolean status, Integer parentId) {
this.id = id;
this.name = name;
this.status = status;
this.parentId = parentId;
}
public Node() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTag() {
return tag;
}
public void setTag(String tag) {
this.tag = tag;
}
public String getLogo() {
return logo;
}
public void setLogo(String logo) {
this.logo = logo;
}
public Integer getLevel() {
return level;
}
public void setLevel(Integer level) {
this.level = level;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Integer getCreatorId() {
return creatorId;
}
public void setCreatorId(Integer creatorId) {
this.creatorId = creatorId;
}
public boolean isStatus() {
return status;
}
public void setStatus(boolean status) {
this.status = status;
}
public LocalDateTime getCreateTime() {
return createTime;
}
public void setCreateTime(LocalDateTime createTime) {
this.createTime = createTime;
}
public Integer getSort() {
return sort;
}
public void setSort(Integer sort) {
this.sort = sort;
}
public Integer getParentId() {
return parentId;
}
public void setParentId(Integer parentId) {
this.parentId = parentId;
}
}
package org.belen.tree;
import java.util.ArrayList;
import java.util.List;
public class TreeUtils1 {
public static void main(String[] args) {
List nodes = Data.getNodes();
List tree = getTree(nodes, " ", 0, null, 0, 16, "root");
for (Node node : tree) {
System.out.println(node.getTag() + node.getName() + node.getId() + "(" + node.getLevel() + ")");
}
}
private static List getTree(List source, String prefix, Integer level, Integer parentId, Integer levelBegin, Integer levelEnd, String path) {
List result = new ArrayList<>();
List children = getChildren(parentId, source);
Integer childrenCount = children.size();
String space4 = " ".repeat(4);
String tab = (level > 1 ? "│" : " ") + space4;
for (Node node : source) {
if (node.getParentId() == parentId) {
String treeChar = childrenCount > 1 ? "├── " : "└── ";
node.setLevel(levelBegin);
node.setTag(prefix + tab + treeChar);
node.setPath(path + "/" + node.getName());
result.add(node);
if (levelBegin < levelEnd) {
List childNodes = getTree(source, prefix + tab, childrenCount, node.getId(), levelBegin + 1, levelEnd, node.getPath());
if (childNodes != null) {
result.addAll(childNodes);
}
}
childrenCount -= 1;
}
}
return result;
}
private static List getChildren(Integer parentId, List source) {
List result = new ArrayList<>();
for (Node node : source) {
if (node.getParentId() == parentId) {
result.add(node);
}
}
return result;
}
}
package org.belen.tree;
import java.util.ArrayList;
import java.util.List;
public abstract class Data {
public static List getNodes() {
Node node0 = new Node(0, "Languages", true, null);
Node node1 = new Node(1, "Java", true, 0);
Node node2 = new Node(2, "jsp", true, 1);
Node node3 = new Node(3, "Spring", true, 1);
Node node4 = new Node(4, "Spring MVC", true, 3);
Node node5 = new Node(5, "Spring Boot", true, 3);
Node node6 = new Node(6, "JVM", true, 1);
Node node7 = new Node(7, "dotNet", true, null);
Node node8 = new Node(8, "VB.NET", true, 7);
Node node9 = new Node(9, "Csharp", true, 7);
Node node10 = new Node(10, "HTML5", true, null);
Node node11 = new Node(11, "Css3", true, 10);
Node node12 = new Node(12, "BootStrap", true, 11);
Node node13 = new Node(13, "javaScript", true, null);
Node node14 = new Node(14, "jQuery", true, 13);
List nodes = new ArrayList<>();
nodes.add(node0);
nodes.add(node1);
nodes.add(node2);
nodes.add(node3);
nodes.add(node4);
nodes.add(node5);
nodes.add(node6);
nodes.add(node7);
nodes.add(node8);
nodes.add(node9);
nodes.add(node10);
nodes.add(node11);
nodes.add(node12);
nodes.add(node13);
nodes.add(node14);
return nodes;
}
}