Advice on tree transversals

Jose jgetino at telefonica.net
Fri Mar 29 13:59:04 PDT 2013


Hi Paul,

I think I'm using something like you suggest (a LinkedList) for breadth
first. It's not a pure "stream" approach, because I have to push nodes to
the queue in bunches (the children of a node) instead one by one, but not so
bad.

However depth first transversals seems more elusive to me, I find no other
way than storing all nodes in a list before processing them. 
  



-----Mensaje original-----
De: Paul Sandoz [mailto:paul.sandoz at oracle.com] 
Enviado el: jueves, 28 de marzo de 2013 21:19
Para: Jose
CC: lambda-dev at openjdk.java.net
Asunto: Re: Advice on tree transversals

Hi Jose,

Write an Iterator that internally uses an ArrayDeque as the stack, where
hasNext() will do the push/pop of nodes.

Then hook that iterator up to a stream (see the java.util.streams.Stream
class).

Paul.

On Mar 28, 2013, at 8:57 PM, Jose <jgetino at telefonica.net> wrote:

> 
> 
> I'd like to know if there is any chance of lambdifing tree transversals
and
> visitors. 
> 
> For example, a make a depth first transversal with a recursive call like
> this
> 
> 
> void  depthFirstVisit(Visitor visitor, VisitMode visitMode) {
>        if (visitMode == VisitMode.PRE_ORDER) {
>             visitor.visit(this);
>         }
>         for (Node<D> child : getChildren()) {
>                 child.depthFirstVisit(visitMode);
>         }
>         if (visitMode == VisitMode.POST_ORDER) {
>             visitor.visit(this);
>         }
> 	   return list;
> }
> 
> I would like to get rid off the visitor's pattern and replace it with
> filters/consumers, which seems not a big deal. But the final goal would be
> to replace the transversal code with an appropriate pipeline, which means
> decoupling the stream creation from the stream processing. 
> 
> I feel I should be simple enough, parallelism is not a concern here, but I
> don't see how to generate the tree node stream.  
> 
> Thanks 
> 
> 



More information about the lambda-dev mailing list