Linear Time Tree Traversal Generator

Steve D'Aprano steve+python at pearwood.info
Tue Sep 20 23:36:06 EDT 2016


On Wed, 21 Sep 2016 12:47 pm, Chris Angelico wrote:

> On Wed, Sep 21, 2016 at 12:34 PM, Steve D'Aprano
> <steve+python at pearwood.info> wrote:
>>     "since values from the leaves of the tree have to be yielded
>>     multiple times to the top of the tree"
>>
>> Each leaf is yielded once, and I don't understand what you mean by
>> yielding to the top of the tree.
> 
> Ultimately, yes. But AIUI he's talking about this:
> 
> def leaf():
>     yield 42
> def intermediate():
>     yield next(leaf())
> def root():
>     yield next(intermediate())


I don't see that in the OP's code. Nor do I understand where this would be
used. Surely root should be the root of the tree, not a function that walks
a linear chain to yield the bottom of the chain?



> The value 42 gets "passed up the chain" until it gets to the top.

How else do you expect to get to the bottom of the chain starting from the
top except by traversing the chain?

If you don't want to traverse the chain, then don't start at the top.





-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list