Exemple #1
0
 private static <A> Stream<String> drawSubTrees(final Show<A> s, final Stream<Tree<A>> ts) {
   return ts.isEmpty()
       ? Stream.nil()
       : ts.tail()._1().isEmpty()
           ? shift("`- ", "   ", ts.head().drawTree(s)).cons("|")
           : shift("+- ", "|  ", ts.head().drawTree(s)).append(drawSubTrees(s, ts.tail()._1()));
 }
Exemple #2
0
 /**
  * Puts the elements of the tree into a Stream, in pre-order.
  *
  * @return The elements of the tree in pre-order.
  */
 public Stream<A> flatten() {
   final F2<Tree<A>, P1<Stream<A>>, Stream<A>> squish =
       new F2<Tree<A>, P1<Stream<A>>, Stream<A>>() {
         public Stream<A> f(final Tree<A> t, final P1<Stream<A>> xs) {
           return cons(
               t.root(),
               t.subForest().map(Stream.<Tree<A>, Stream<A>>foldRight().f(curry()).f(xs._1())));
         }
       };
   return squish.f(this, P.p(Stream.<A>nil()));
 }
Exemple #3
0
 /**
  * Creates a nullary tree.
  *
  * @param root The root element of the tree.
  * @return A nullary tree with the root element in it.
  */
 public static <A> Tree<A> leaf(final A root) {
   return node(root, Stream.nil());
 }