import java.util.ArrayList;
import java.util.List;
public class ManyNodeTree {
private ManyTreeNode root;
public ManyNodeTree(String Root) {
root = new ManyTreeNode(new TreeNode(Root));
}
public ManyNodeTree createTree(List<TreeNode> treeNodes, String root) {
if (treeNodes == null || treeNodes.size() <= 0) {
return null;
}
ManyNodeTree manyNodeTree = new ManyNodeTree(root);
for (TreeNode treeNode : treeNodes) {
if (treeNode.getParentId().equals(root)) {
manyNodeTree.getRoot().getChildList().add(new ManyTreeNode(treeNode));
} else {
addChild(manyNodeTree.getRoot(), treeNode);
}
}
return manyNodeTree;
}
public void addChild(ManyTreeNode manyTreeNode, TreeNode child) {
for (ManyTreeNode item : manyTreeNode.getChildList()) {
if (item.getData().getNodeId().equals(child.getParentId())) {
item.getChildList().add(new ManyTreeNode(child));
break;
} else {
if (item.getChildList() != null && item.getChildList().size() > 0) {
addChild(item, child);
}
}
}
}
public String iteratorTree(ManyTreeNode manyTreeNode) {
List<String> rsData = new ArrayList<>();
StringBuilder buffer = new StringBuilder();
if (manyTreeNode != null) {
for (ManyTreeNode index : manyTreeNode.getChildList()) {
buffer.append(index.getData().getNodeId() + "\n");
if (index.getChildList() != null && index.getChildList().size() > 0) {
buffer.append(iteratorTree(index));
}else {
System.out.println("叶子节点"+index.getData().getNodeId());
}
}
}
return buffer.toString();
}
public String getLeafNode(ManyTreeNode manyTreeNode,StringBuilder leafNodes) {
StringBuilder buffer = new StringBuilder();
if (manyTreeNode != null) {
for (ManyTreeNode index : manyTreeNode.getChildList()) {
if (index.getChildList() != null && index.getChildList().size() > 0) {
buffer.append(getLeafNode(index,leafNodes));
}else {
System.out.println("叶子节点"+index.getData().getNodeId());
leafNodes.append(index.getData().getNodeId() + ",");
}
}
}
return leafNodes.toString();
}
public ManyTreeNode getRoot() {
return root;
}
public void setRoot(ManyTreeNode root) {
this.root = root;
}
public static void main(String[] args) {
List<TreeNode> treeNodes = new ArrayList<TreeNode>();
String Root = "1";
treeNodes.add(new TreeNode("1", "root"));
treeNodes.add(new TreeNode("101", "1"));
treeNodes.add(new TreeNode("102", "1"));
treeNodes.add(new TreeNode("103", "1"));
treeNodes.add(new TreeNode("10101", "101"));
treeNodes.add(new TreeNode("1010101", "10101"));
treeNodes.add(new TreeNode("10102", "101"));
treeNodes.add(new TreeNode("1010102", "10101"));
treeNodes.add(new TreeNode("10201", "102"));
treeNodes.add(new TreeNode("1020101", "10201"));
ManyNodeTree tree = new ManyNodeTree(Root);
StringBuilder leafNodes = new StringBuilder();
String LeafNode = tree.getLeafNode(tree.createTree(treeNodes,Root).getRoot(),leafNodes);
System.out.println(leafNodes);
}
}
import java.util.ArrayList;
import java.util.List;
public class ManyTreeNode {
private TreeNode data;
private List<ManyTreeNode> childList;
public ManyTreeNode(TreeNode data) {
this.data = data;
this.childList = new ArrayList<ManyTreeNode>();
}
public ManyTreeNode(TreeNode data, List<ManyTreeNode> childList) {
this.data = data;
this.childList = childList;
}
public TreeNode getData() {
return data;
}
public void setData(TreeNode data) {
this.data = data;
}
public List<ManyTreeNode> getChildList() {
return childList;
}
public void setChildList(List<ManyTreeNode> childList) {
this.childList = childList;
}
}
public class TreeNode {
private String nodeId;
private String parentId;
private String text;
public TreeNode(String nodeId) {
this.nodeId = nodeId;
}
public TreeNode(String nodeId, String parentId) {
this.nodeId = nodeId;
this.parentId = parentId;
}
public String getNodeId() {
return nodeId;
}
public void setNodeId(String nodeId) {
this.nodeId = nodeId;
}
public String getParentId() {
return parentId;
}
public void setParentId(String parentId) {
this.parentId = parentId;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
参考文章