|
| 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 | +} |
0 commit comments