#import Python turtle module #More info see https://docs.python.org/2/library/turtle.html import turtle ##Make turtle t recursively draw a tree def drawTree(branchLen,t): if branchLen <= 10: #base case t.forward(10) t.backward(10) else: #draw the trunk t.forward(branchLen) #draw branches t.right(20) drawTree(branchLen-15,t) t.left(40) drawTree(branchLen-15,t) t.right(20) #go back to the starting point t.backward(branchLen) ##Initialize the turtle t = turtle.Turtle() t.speed(6) #Set the turtle speed. 1 is slow and 10 is fast. t.color("green")#Set the turtle color. t.pensize(2) #Set the pen size myWin = turtle.Screen() ##Initialize the pen position t.left(90) #Turn turtle left by 90 degrees. t.up() #Pull the pen up - no drawing when moving. t.backward(100) #move the pen backward by 100 t.down() #Pull the pen down - drawing when moving. #draw the tree drawTree(70,t) ##exit the drawing screen myWin.exitonclick()
Program Revised from http://interactivepython.org/runestone/static/pythonds/Recursion/pythondsintro-VisualizingRecursion.html