private static <T> void writeTreeBranch( Tree<T> tree, Writer writer, List<Boolean> lines, boolean lastBranch) throws IOException { for (boolean line : lines) { if (line) { writer.write(" | "); } else { writer.write(" "); } } if (lastBranch) { writer.write(" '-- "); } else { writer.write(" |-- "); } writer.write(String.valueOf(tree.getNodeData())); writer.write("\n"); Iterator<Tree<T>> branchIt = tree.iterator(); while (branchIt.hasNext()) { Tree<T> branch = branchIt.next(); List<Boolean> newLines = new ArrayList<Boolean>(); newLines.addAll(lines); newLines.add(!lastBranch); writeTreeBranch(branch, writer, newLines, !branchIt.hasNext()); } }
private static <T> List<List<T>> getAllPaths(Tree<T> tree, List<T> startingWith) { List<List<T>> result = new ArrayList<List<T>>(); List<T> newStartingWith = new ArrayList<T>(); newStartingWith.addAll(startingWith); newStartingWith.add(tree.getNodeData()); Iterator<Tree<T>> it = tree.iterator(); if (it.hasNext()) { while (it.hasNext()) { Tree<T> branch = it.next(); result.addAll(getAllPaths(branch, newStartingWith)); } } else { result.add(newStartingWith); } return result; }