Breadth First Tree

#!/usr/bin/env python
#
# [SNIPPET_NAME: Breadth First Tree]
# [SNIPPET_CATEGORIES: PyTurtle]
# [SNIPPET_DESCRIPTION: Use turtle to draw a tree.]
# [SNIPPET_DOCS: http://docs.python.org/library/turtle.html]
# [SNIPPET_AUTHOR: Grant Bowman <[email protected]>]
# [SNIPPET_LICENSE: PSF]
# Code authorship from http://python.org/download/releases/2.6.4/


"""      turtle-example-suite:

             tdemo_tree.py

Displays a 'breadth-first-tree' - in contrast
to the classical Logo tree drawing programs,
which use a depth-first-algorithm.

Uses:
(1) a tree-generator, where the drawing is
quasi the side-effect, whereas the generator
always yields None.
(2) Turtle-cloning: At each branching point the
current pen is cloned. So in the end there
are 1024 turtles.
"""
from turtle import Turtle, mainloop
from time import clock

def tree(plist, l, a, f):
    """ plist is list of pens
    l is length of branch
    a is half of the angle between 2 branches
    f is factor by which branch is shortened
    from level to level."""
    if l > 3:
        lst = []
        for p in plist:
            p.forward(l)
            q = p.clone()
            p.left(a)
            q.right(a)
            lst.append(p)
            lst.append(q)
        for x in tree(lst, l*f, a, f):
            yield None

def maketree():
    p = Turtle()
    p.setundobuffer(None)
    p.hideturtle()
    p.speed(0)
    p.tracer(30,0)
    p.left(90)
    p.penup()
    p.forward(-210)
    p.pendown()
    t = tree([p], 200, 65, 0.6375)
    for x in t:
        pass
    print len(p.getscreen().turtles())

def main():
    a=clock()
    maketree()
    b=clock()
    return "done: %.2f sec." % (b-a)

if __name__ == "__main__":
    msg = main()
    print msg
    mainloop()