返回信息流定义:
public class Node {
private Node parent;
private List<Node> children = new ArrayList<Node>();
private String title;
// Getters and setters.
}
现在有很多这样的节点。每个节点是树中的一个节点。如果Node的parent是null,那么这个节点是根节点。如果a.parent==b,那么b.children.contains(a);
现在需要漂亮地打印出这个树。每个节点用它的title表示。类似tree命令。
root
|- child1
| |- child11
| | |- child111
| |- child12
| |- child121
|- child2
| |- child21
| | |- child211
| |- child22
| |- child221
|- child3
|- child31
| |- child311
|- child32
|- child321
嗯。用Java。
这是一条镜像帖。来源:北邮人论坛 / java / #20709同步于 2011/11/10
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Java机器人发帖
我也来求一次代码
wks
2011/11/10镜像同步13 回复
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
只能想到先序遍历
用一个degree记录递归的深度,控制缩进
【 在 wks (cloverprince) 的大作中提到: 】
: 定义:
: public class Node {
: private Node parent;
: ...................
来段伪代码
int degree=0;//全局变量
void travel(Node root){
空degree个“ ”,打印root;
if(!root.children.isEmpty){
degree++;
travel(每一个children)}
degree--;
}
【 在 buptlong (楚|火属性的小龙|八卦帮之山火贲) 的大作中提到: 】
: 只能想到先序遍历
: 用一个degree记录递归的深度,控制缩进
光说没用,请写
【 在 buptlong (楚|火属性的小龙|八卦帮之山火贲) 的大作中提到: 】
: 来段伪代码
: int degree=0;//全局变量
: void travel(Node root){
: ...................
我承认我今天有点闲的蛋疼,就随便写了一个,没经过严密的测试,随便用了两个用例好像没啥问题
运行结果如图
package tree;
import java.util.List;
public class PrintTree {
public static void main(String[] args) {
Node rootNode = new Node();
rootNode.setTitle("root");
rootNode.setParent(null);
Node child1 = new Node();
child1.setParent(rootNode);
child1.setTitle("child1");
Node child11 = new Node();
child11.setParent(child1);
child11.setTitle("child11");
Node child111 = new Node();
child111.setParent(child11);
child111.setTitle("child111");
child11.getChildren().add(child111);
Node child12 = new Node();
child12.setParent(child1);
child12.setTitle("child12");
child1.getChildren().add(child11);
child1.getChildren().add(child12);
Node child2 = new Node();
child2.setParent(rootNode);
child2.setTitle("child2");
Node child21 = new Node();
child21.setParent(child2);
child21.setTitle("child21");
Node child211 = new Node();
child211.setParent(child21);
child211.setTitle("child211");
child21.getChildren().add(child211);
child2.getChildren().add(child21);
Node child3 = new Node();
child3.setParent(rootNode);
child3.setTitle("child3");
rootNode.getChildren().add(child1);
rootNode.getChildren().add(child2);
rootNode.getChildren().add(child3);
printTreeNode(findRoot(child21), 0);
}
public static Node findRoot(Node node) {
if (node != null) {
while (node.getParent() != null) {
node = node.getParent();
}
return node;
} else {
return null;
}
}
public static void printTreeNode(Node node, int depth) {
if (node != null) {
if (node.getTitle() != null) {
print(node, depth);
}
if (node.getChildren() != null && node.getChildren().size() > 0) {
List<Node> children = node.getChildren();
for (Node child : children) {
printTreeNode(child, depth + 1);
}
}
return;
} else {
return;
}
}
public static void print(Node node, int depth) {
StringBuffer buffer = new StringBuffer();
if (depth > 0) {
while (depth > 0) {
buffer.append("| ");
depth--;
}
buffer.delete(buffer.length() - 2, buffer.length());
buffer.append("- ");
}
System.out.print(buffer);
System.out.print(node.getTitle());
System.out.println();
}
}
【 在 wks 的大作中提到: 】
: 定义:
: public class Node {
: private Node parent;
: ...................
流弊
【 在 ox (小贝) 的大作中提到: 】
: 我承认我今天有点闲的蛋疼,就随便写了一个,没经过严密的测试,随便用了两个用例好像没啥问题
: 运行结果如图
: [upload=1][/upload]
: ...................
足够漂亮了。可以做的更好。
比如:
root
|- child1
| |- child11
| | |- child111
| |- child12
| | |- child121
|- child2
| |- child21
| | |- child211
| |- child22
| | |- child221
|- child3
| |- child31
| | |- child311
| |- child32
| | |- child321
比起上面那个,这个更好:
root
|- child1
| |- child11
| | |- child111
| |- child12
| |- child121
|- child2
| |- child21
| | |- child211
| |- child22
| |- child221
|- child3
|- child31
| |- child311
|- child32
|- child321
【 在 ox 的大作中提到: 】
: 我承认我今天有点闲的蛋疼,就随便写了一个,没经过严密的测试,随便用了两个用例好像没啥问题
: 运行结果如图
: [upload=1][/upload]
: ...................
出去一趟。。。等晚上回来再折腾。。。
【 在 wks (cloverprince) 的大作中提到: 】
: 足够漂亮了。可以做的更好。
: 比如:
: root
: ...................
最近在做TreeTable形式的统计呈现就是类似的玩意儿
不过我们是一堆零散的数据取来统计,根据数据级联关系构建树,而不是树构建完了打印
反正核心思想就是深度优先,递归。。。
真觉得打印的方式太多“|”怎么做都挺难看的。。。
有个FtpList工具倒是蛮漂亮的,就是跟lz的命题要求有点不符合