unbalanced tree iteration issue

kost BebiX k.bx at ya.ru
Fri Jan 14 13:41:31 EST 2011


14.01.2011, 20:19, "Ian Kelly" <ian.g.kelly at gmail.com>:
> 2011/1/14 kost BebiX <k.bx at ya.ru>;:
>
>>  Well, isn't tree is a root node and it's children?
>
> And its grandchildren, great-grandchildren, etc.  What Alex is saying
> is that the implementation you posted traverses the root and its
> immediate children, but does not recur any further than that.  That is
> why it was so fast.

Oh, yeah, sorry, forgot the recursion. It should be (if I'm not wrong again):

    def post_order(self):
        for child in self.childs:
            for po in child.post_order():
                yield po
        yield self

if you give it more deepness:

$ python postorder.py 
building tree...done
0:00:25.839462
doing generator post_order...done
0:00:02.776876
doing non-generator post_order...done
0:00:01.092648

still not bad, but if you'll give it more deepness

$ python postorder.py 
building tree...done
0:00:16.078972
doing generator post_order...done
0:00:03.119023
doing non-generator post_order...done
0:00:00.841976

it will be worse

-- 
jabber: k.bx at ya.ru



More information about the Python-list mailing list