Wednesday, January 18, 2023

Recursion

 Another day, another problem. The little tree below is

done through recursion. Trying to follow the code...                       

                    



mport turtle
screen = turtle.Screen()
screen.setup(500, 500)
class TreeNode(object):
def __init__(self, val, left=None, right: object = None):
self.val = val
self.left = left
self.right = right
from turtle import *

reset()
color("green")
speed(2)
seth(90)
shape('turtle')

def tree(size):
if size < 10:
return
fd(size)
lt(30)
tree(size * 0.6)
color('hotpink')
rt(60)
tree(size * 0.6)
color('blue')
lt(30)
bk(size)

tree(100)
turtle.mainloop()
I've even tried changing the code :

Or again:



Teaching Kids Programming – Draw a Tree in Python using Turtle Graphics (Recursion) | Algorithms, Blockchain and Cloud (helloacm.com)

No comments: