Design

Thursday, February 13, 2014

Recursive Turtle Tree

I know the title of this post may sound like random words thrown together, but it's actually a really cool activity to learn about recursion using the turtle module in Python (Linux users need to install tkinter). I've been learning about algorithms and abstract data structures as part of my faking a CS degree personal mission. Problem Solving with Algorithms and Data Structures has been a fantastic resource so far (and free). Although the authors' explanation of recursion is the best I've ever seen, I think I could just add a little to the tree drawing example. Here's the program they provide:

import turtle

def tree(branchLen,t):
    if branchLen > 5:
        t.forward(branchLen)
        t.right(20)
        tree(branchLen-15,t)
        t.left(40)
        tree(branchLen-15,t)
        t.right(20)
        t.backward(branchLen)

def main():
    t = turtle.Turtle()
    myWin = turtle.Screen()
    t.left(90)
    t.up()
    t.backward(100)
    t.down()
    t.color("green")
    tree(75,t)
    myWin.exitonclick()

main()

Now I'm going to focus on the tree function. Notice tree calls itself twice. Now to step through how this function works, I am going to give each line a number and each recursive call a number such that 1.2 is the second time the first recursive call has been made and 2.3 is the third time the second call has been made. 

def tree(branchLen,t):
0    if branchLen > 5:
    1    t.forward(branchLen)
    2    t.right(20)
    3    tree(branchLen-15,t)     first recursive call
    4    t.left(40)
    5    tree(branchLen-15,t)    second recursive call
    6    t.right(20)
    7    t.backward(branchLen)

Let's trace branchLen = 30, following the code and what the turtle does. I've color coded each recursive call:

Code Line    Action                                Recursive Call
0               Passes > 5 test                       none
1               Forward 30                           none
2               Turns right 20 degrees           none
3               Calls tree(15)                        none
0               Passes > 5 test                      1.1
1               Forward 15                           1.1
2               Turns right 20 degrees            1.1
3               Calls tree(0)                           1.1
0               Fails > 5 test                          1.2
4               Turns Left 40 degrees             1.1
5               Calls tree(0)                           1.1
0               Fails > 5 test                          2.1
6               Turns right 20 degrees            1.1
7               Backward 15                         1.1
4               Turns Left 40 degrees             none
5               Calls tree(15)                         none
0               Passes > 5 test                        2.2
1               Forward 15                            2.2
2               Turns right 20 degrees             2.2
3               Calls tree(0)                            2.2
0               Fails > 5 test                           1.3               
4               Turns Left 40 degrees             2.2
5               Calls tree(0)                           2.2
0               Fails > 5 test                           2.3
6               Turns right 20 degrees            2.2
7               Backward 15                         2.2
6               Turns right 20 degrees            none
7               Backward 30                         none

If you choose a color such as red or orange, you can see how the function is executed in its entirety, but with interruptions. The colors that appear nested, such as orange inside the red, appear higher in the stack frame. They are completed first even though they started second, but the program continues to the red after the orange is completed. The next value of branchLen to create more recursive calls is 45. You could also try tracing tree(45), using tree(30) as a shortcut to emphasize the benefit of recursion.
               

1 comment:

  1. Hi. I really enjoyed my brief visit on your site and I’ll be sure to be back for more.
    Can you please consider placing my website on your link list?

    Please email me back.

    Thanks!
    Kevin
    kevincollins1011 gmail.com

    ReplyDelete