Advice on tree transversals
Paul Sandoz
paul.sandoz at oracle.com
Thu Mar 28 13:19:27 PDT 2013
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