1 package org.kuali.common.util.tree; 2 3 import static com.google.common.collect.Lists.newArrayList; 4 5 import java.util.List; 6 7 import org.apache.commons.lang3.StringUtils; 8 9 import com.google.common.base.Function; 10 import com.google.common.collect.ImmutableList; 11 import com.google.common.collect.Lists; 12 13 public class Trees { 14 15 public static <T> List<Node<T>> getLeaves(List<Node<T>> nodes) { 16 List<Node<T>> leaves = newArrayList(); 17 for (Node<T> node : nodes) { 18 leaves.addAll(getLeaves(node)); 19 } 20 return leaves; 21 } 22 23 public static <T> List<Node<T>> getLeaves(Node<T> root) { 24 List<Node<T>> leaves = newArrayList(); 25 List<Node<T>> nodes = breadthFirst(root); 26 for (Node<T> node : nodes) { 27 if (node.isLeaf()) { 28 leaves.add(node); 29 } 30 } 31 return leaves; 32 } 33 34 public static <T> List<T> breadthFirstElements(Node<T> node) { 35 return Lists.transform(breadthFirst(node), new NodeElementFunction<T>()); 36 } 37 38 public static <T> List<Node<T>> breadthFirst(List<Node<T>> nodes) { 39 List<Node<T>> list = newArrayList(); 40 for (Node<T> node : nodes) { 41 list.addAll(breadthFirst(node)); 42 } 43 return list; 44 } 45 46 public static <T> List<Node<T>> breadthFirst(Node<T> node) { 47 NodeTraverser<T> nt = NodeTraverser.create(); 48 Iterable<Node<T>> itr = nt.breadthFirstTraversal(node); 49 return newArrayList(itr); 50 } 51 52 public static <T> List<Node<T>> postOrder(Node<T> node) { 53 NodeTraverser<T> nt = NodeTraverser.create(); 54 Iterable<Node<T>> itr = nt.postOrderTraversal(node); 55 return newArrayList(itr); 56 } 57 58 public static <T> List<Node<T>> preOrder(Node<T> node) { 59 NodeTraverser<T> nt = NodeTraverser.create(); 60 Iterable<Node<T>> itr = nt.preOrderTraversal(node); 61 return newArrayList(itr); 62 } 63 64 public static <T> String html(String title, Node<T> node) { 65 Function<Node<T>, String> converter = NodeStringFunction.create(); 66 return html(title, ImmutableList.of(node), converter); 67 } 68 69 public static <T> String html(String title, Node<T> node, Function<Node<T>, String> converter) { 70 return html(title, ImmutableList.of(node), converter); 71 } 72 73 public static <T> String html(String title, List<Node<T>> nodes) { 74 Function<Node<T>, String> converter = NodeStringFunction.create(); 75 return html(title, nodes, converter); 76 } 77 78 public static <T> String html(String title, List<Node<T>> nodes, Function<Node<T>, String> converter) { 79 StringBuilder sb = new StringBuilder(); 80 sb.append("<table border=\"1\">\n"); 81 sb.append(" <th>" + title + "</th>\n"); 82 sb.append(" <tr>\n"); 83 sb.append(" <td>\n"); 84 for (Node<T> node : nodes) { 85 sb.append(html(node, 3, converter)); 86 } 87 sb.append(" </td>\n"); 88 sb.append(" </tr>\n"); 89 sb.append("</table>\n"); 90 return sb.toString(); 91 } 92 93 public static <T> String html(Node<T> node) { 94 Function<Node<T>, String> converter = NodeStringFunction.create(); 95 return html(node, 0, converter); 96 } 97 98 public static <T> String html(Node<T> node, Function<Node<T>, String> converter) { 99 return html(node, 0, converter); 100 } 101 102 public static <T> String html(Node<T> node, int indent, Function<Node<T>, String> converter) { 103 StringBuilder sb = new StringBuilder(); 104 String prefix = StringUtils.repeat(" ", indent); 105 sb.append(prefix + "<table border=\"1\">\n"); 106 sb.append(prefix + " <tr>\n"); 107 sb.append(prefix + " <td>" + converter.apply(node) + "</td>\n"); 108 List<Node<T>> children = node.getChildren(); 109 if (!children.isEmpty()) { 110 sb.append(prefix + " <td>\n"); 111 for (Node<T> child : children) { 112 sb.append(html(child, indent + 3, converter)); 113 } 114 sb.append(prefix + " </td>\n"); 115 } 116 sb.append(prefix + " </tr>\n"); 117 sb.append(prefix + "</table>\n"); 118 return sb.toString(); 119 } 120 121 }