多叉树 关系 UsageLink 输出树形结构每个节点的编号,父节点,子节点编号以及所处的层级 控制台输出

package treeNode;
import java.util.ArrayList;
import java.util.List;
public class Node {
private String title;
private List<Node> childs;
private Node parent;
public Node(String title) {
this();
this.title=title;
}
public Node() {
this.childs=new ArrayList<Node>();
}
public List<Node> getchilds() {
return childs;
}
public String gettitle() {
return title;
}
public Node getparent() {
return parent;
}
public Node addchild(Node child){
this.childs.add(child);
return child;
}
public void setparent(Node parent) {
this.parent = parent;
}
public void settitle(String title) {
this.title = title;
}
public static void main(String[] args) {
Node root = new Node();
Node Node01 = new Node("001");
root.addchild(Node01);
Node Node0101 = Node01.addchild(new Node("002"));
Node Node0102 = Node01.addchild(new Node("003"));
Node Node0103 = Node01.addchild(new Node("004"));
Node0101.addchild(new Node("005"));
Node0101.addchild(new Node("006"));
Node0102.addchild(new Node("007"));
Node0103.addchild(new Node("008"));
Node0103.addchild(new Node("009"));
showNode(root);
}
private static void showNode(Node Node) {
for (Node child : Node.getchilds()) {
showNode(child, 0);
}
}
private static void showNode(Node Node, int tabnum) {
for (int i = 0; i < tabnum; i++)
System.out.print("\t");
System.out.println(Node.gettitle());
for (Node child : Node.getchilds())
// 递归调用
showNode(child, tabnum + 1);
}
}
截图:


浙公网安备 33010602011771号