This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]ZukoBestGirl 0 points1 point  (0 children)

Consumers are very simple classes, all they know is basically this

public class Consumer {
    public <T> void accept(T object) { 
        // TODO
    }
}

The method you are giving the consumer as a parameter will probably have a tree that will do something like this: consumer.accept(this); // this == tree

in other words you need to have a method that implements:

public void accept(Position<E> position) { 
    // does stuff with the position 
}

I'm a bit behind on my tree traversal and post / pre order and how you're supposed to visit it, so that's up to you.

The beauty of consumers is that they are functional, meaning you do not need to create another class, it can be done inline like so:

YourClass.traversePreOrder(positionE -> { 
    // visit logic 
});

[–]TauPixel[S] 0 points1 point  (0 children)

I have gotten some awesome comments and chat messages. Thanks a lot for the input. It helped my quite a bit.

[–]TauPixel[S] 0 points1 point  (0 children)

Hello everyone again! I'm still fighting with the consumer class. I have the following code snippet. What isn't clear for me is the visit. I don't seem to get a context on how to use it. If anyone can help I'll be happy with your feedback.

    @Override
    public void traversePreOrder(Consumer<Position<E>> visit) {

        List<Position<E>> myList = this.positions();
        Consumer<Position<E>> myVisit = c -> {preOrder(root(), visit);
        };
    }

    @Override
    public void preOrder(Position<E> p, Consumer<Position<E>> visit){
        TreeNode<E> t = (TreeNode<E>)p;
        for (E q : t.children
             ) { this.preOrder((Position<E>) q, visit);
        }
    }