mirror of
https://github.com/Estom/notes.git
synced 2026-02-06 20:14:37 +08:00
Java内容重新整理删除过期的东西
This commit is contained in:
118
Java/JavaDemo/codedemo/tree/PrettyTree.java
Normal file
118
Java/JavaDemo/codedemo/tree/PrettyTree.java
Normal file
@@ -0,0 +1,118 @@
|
||||
package cn.aofeng.demo.tree;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 输出类似目录树的结构。
|
||||
*
|
||||
* @author <a href="mailto:aofengblog@163.com">聂勇</a>
|
||||
*/
|
||||
public class PrettyTree {
|
||||
|
||||
private final static String NODE_INDENT_PREFIX = "| ";
|
||||
private final static String NODE_PARENT_PREFIX = "├── ";
|
||||
private final static String NODE_LEAF_PREFIX = "└── ";
|
||||
private final static String LINE_SEPARATOR = System.getProperty("line.separator", "\n");
|
||||
|
||||
public void renderRoot(Node root, StringBuilder buffer) {
|
||||
renderParentNode(root, 0, buffer);
|
||||
}
|
||||
|
||||
private void renderParentNode(Node node, int indent, StringBuilder buffer) {
|
||||
addIndent(indent, buffer);
|
||||
buffer.append(NODE_PARENT_PREFIX)
|
||||
.append(node.getName())
|
||||
.append(LINE_SEPARATOR);
|
||||
List<Node> childList = node.getChild();
|
||||
if (null == childList) {
|
||||
return;
|
||||
}
|
||||
for (Node child : childList) {
|
||||
if (child.isLeaf()) {
|
||||
renderLeafNode(child, indent+1, buffer);
|
||||
} else {
|
||||
renderParentNode(child, indent+1, buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void renderLeafNode(Node node, int indent, StringBuilder buffer) {
|
||||
addIndent(indent, buffer);
|
||||
buffer.append(NODE_LEAF_PREFIX)
|
||||
.append(node.getName())
|
||||
.append(LINE_SEPARATOR);
|
||||
}
|
||||
|
||||
private void addIndent(int indent, StringBuilder buffer) {
|
||||
for (int i = 0; i < indent; i++) {
|
||||
buffer.append(NODE_INDENT_PREFIX);
|
||||
}
|
||||
}
|
||||
|
||||
public static class Node {
|
||||
|
||||
/** 节点名称。 */
|
||||
private String name;
|
||||
|
||||
/** 子节点列表 */
|
||||
private List<Node> child = new LinkedList<Node>();
|
||||
|
||||
public Node(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Node(String name, List<Node> child) {
|
||||
this.name = name;
|
||||
this.child = child;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断当前节点是否为叶子节点。
|
||||
*
|
||||
* @return 如果是叶子节点返回true;否则返回false。
|
||||
*/
|
||||
public boolean isLeaf() {
|
||||
if (null == child || child.isEmpty()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量添加子节点。
|
||||
*
|
||||
* @param child 子节点列表。
|
||||
*/
|
||||
public void addAll(List<Node> nodeList) {
|
||||
this.child.addAll(nodeList);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加单个子节点。
|
||||
*
|
||||
* @param node 子节点。
|
||||
*/
|
||||
public void add(Node node) {
|
||||
this.child.add(node);
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public List<Node> getChild() {
|
||||
return child;
|
||||
}
|
||||
|
||||
public void setChild(List<Node> child) {
|
||||
this.child = child;
|
||||
}
|
||||
} // end of Node
|
||||
|
||||
}
|
||||
41
Java/JavaDemo/codedemo/tree/PrettyTreeTest.java
Normal file
41
Java/JavaDemo/codedemo/tree/PrettyTreeTest.java
Normal file
@@ -0,0 +1,41 @@
|
||||
package cn.aofeng.demo.tree;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import cn.aofeng.demo.tree.PrettyTree.Node;
|
||||
|
||||
public class PrettyTreeTest {
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrintRoot() {
|
||||
Node p11 = new Node("p1-1");
|
||||
Node p12 = new Node("p1-2");
|
||||
Node p1 = new Node("p1");
|
||||
p1.add(p11);
|
||||
p1.add(p12);
|
||||
|
||||
Node p21 = new Node("p21");
|
||||
Node p2 = new Node("p2");
|
||||
p12.add(p21);
|
||||
|
||||
Node root = new Node("Root");
|
||||
root.add(p1);
|
||||
root.add(p2);
|
||||
|
||||
StringBuilder buffer = new StringBuilder(256);
|
||||
PrettyTree pt = new PrettyTree();
|
||||
pt.renderRoot(root, buffer);
|
||||
System.out.print(buffer.toString());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user