Skip to content

Commit 24a4d6a

Browse files
committed
在控制台输出类似目录树的结构
1 parent 518e8e7 commit 24a4d6a

3 files changed

Lines changed: 163 additions & 0 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@
4444
* [Gson教程-自定义序列化](http://aofengblog.com/2015/08/14/Gson-%E8%87%AA%E5%AE%9A%E4%B9%89%E5%BA%8F%E5%88%97%E5%8C%96/)
4545
* [Gson教程-自定义反序列化](http://aofengblog.com/2015/08/17/Gson-%E8%87%AA%E5%AE%9A%E4%B9%89%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96/)
4646

47+
## 数据结构
48+
### 代码
49+
* [在控制台输出类似目录树的结构](src/cn/aofeng/demo/tree)
50+
4751
##定时器
4852
###代码
4953
* [Timer](src/cn/aofeng/demo/java/util/timer/TimerDemo.java)
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package cn.aofeng.demo.tree;
2+
3+
import java.util.LinkedList;
4+
import java.util.List;
5+
6+
/**
7+
* 输出类似目录树的结构。
8+
*
9+
* @author <a href="mailto:[email protected]">聂勇</a>
10+
*/
11+
public class PrettyTree {
12+
13+
private final static String NODE_INDENT_PREFIX = "| ";
14+
private final static String NODE_PARENT_PREFIX = "├── ";
15+
private final static String NODE_LEAF_PREFIX = "└── ";
16+
private final static String LINE_SEPARATOR = System.getProperty("line.separator", "\n");
17+
18+
public void renderRoot(Node root, StringBuilder buffer) {
19+
renderParentNode(root, 0, buffer);
20+
}
21+
22+
private void renderParentNode(Node node, int indent, StringBuilder buffer) {
23+
addIndent(indent, buffer);
24+
buffer.append(NODE_PARENT_PREFIX)
25+
.append(node.getName())
26+
.append(LINE_SEPARATOR);
27+
List<Node> childList = node.getChild();
28+
if (null == childList) {
29+
return;
30+
}
31+
for (Node child : childList) {
32+
if (child.isLeaf()) {
33+
renderLeafNode(child, indent+1, buffer);
34+
} else {
35+
renderParentNode(child, indent+1, buffer);
36+
}
37+
}
38+
}
39+
40+
private void renderLeafNode(Node node, int indent, StringBuilder buffer) {
41+
addIndent(indent, buffer);
42+
buffer.append(NODE_LEAF_PREFIX)
43+
.append(node.getName())
44+
.append(LINE_SEPARATOR);
45+
}
46+
47+
private void addIndent(int indent, StringBuilder buffer) {
48+
for (int i = 0; i < indent; i++) {
49+
buffer.append(NODE_INDENT_PREFIX);
50+
}
51+
}
52+
53+
public static class Node {
54+
55+
/** 节点名称。 */
56+
private String name;
57+
58+
/** 子节点列表 */
59+
private List<Node> child = new LinkedList<Node>();
60+
61+
public Node(String name) {
62+
this.name = name;
63+
}
64+
65+
public Node(String name, List<Node> child) {
66+
this.name = name;
67+
this.child = child;
68+
}
69+
70+
/**
71+
* 判断当前节点是否为叶子节点。
72+
*
73+
* @return 如果是叶子节点返回true;否则返回false。
74+
*/
75+
public boolean isLeaf() {
76+
if (null == child || child.isEmpty()) {
77+
return true;
78+
}
79+
80+
return false;
81+
}
82+
83+
/**
84+
* 批量添加子节点。
85+
*
86+
* @param child 子节点列表。
87+
*/
88+
public void addAll(List<Node> nodeList) {
89+
this.child.addAll(nodeList);
90+
}
91+
92+
/**
93+
* 添加单个子节点。
94+
*
95+
* @param node 子节点。
96+
*/
97+
public void add(Node node) {
98+
this.child.add(node);
99+
}
100+
101+
public String getName() {
102+
return name;
103+
}
104+
105+
public void setName(String name) {
106+
this.name = name;
107+
}
108+
109+
public List<Node> getChild() {
110+
return child;
111+
}
112+
113+
public void setChild(List<Node> child) {
114+
this.child = child;
115+
}
116+
} // end of Node
117+
118+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package cn.aofeng.demo.tree;
2+
3+
import org.junit.After;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
7+
import cn.aofeng.demo.tree.PrettyTree.Node;
8+
9+
public class PrettyTreeTest {
10+
11+
@Before
12+
public void setUp() throws Exception {
13+
}
14+
15+
@After
16+
public void tearDown() throws Exception {
17+
}
18+
19+
@Test
20+
public void testPrintRoot() {
21+
Node p11 = new Node("p1-1");
22+
Node p12 = new Node("p1-2");
23+
Node p1 = new Node("p1");
24+
p1.add(p11);
25+
p1.add(p12);
26+
27+
Node p21 = new Node("p21");
28+
Node p2 = new Node("p2");
29+
p12.add(p21);
30+
31+
Node root = new Node("Root");
32+
root.add(p1);
33+
root.add(p2);
34+
35+
StringBuilder buffer = new StringBuilder(256);
36+
PrettyTree pt = new PrettyTree();
37+
pt.renderRoot(root, buffer);
38+
System.out.print(buffer.toString());
39+
}
40+
41+
}

0 commit comments

Comments
 (0)