private static String toLispString(Tree<?> tree) { final String value = String.valueOf(tree.getValue()); if (tree.isLeaf()) { return value; } else { return String.format( "(%s %s)", value, tree.getChildren().map(Node::toLispString).mkString(" ")); } }
static <T> Stream<T> inOrder(Tree<T> tree) { if (tree.isLeaf()) { return Stream.of(tree.getValue()); } else { final List<Node<T>> children = tree.getChildren(); return children .tail() .foldLeft(Stream.<T>empty(), (acc, child) -> acc.appendAll(inOrder(child))) .prepend(tree.getValue()) .prependAll(inOrder(children.head())); } }