Write the function in python. a) Binary Tree. Write a function in file pl.py wit
ID: 3707462 • Letter: W
Question
Write the function in python.
a) Binary Tree. Write a function in file pl.py with signature binary_tree(depth, length) that draws a binary tree figure like that from Figure 1 using the turtle module. The figure produced is similar to the example from the textbook with a significant difference that you will notice immediately: the left branches align on a right(60) angle from the vertical and the right branches all align on a left(60) angle from the vertical. The binary tree drawing must look like that from Figure 1, except the dots are NOT required: Figure 1. Binary tree produced by calling binary_tree(160, 5), with dots shown at starting points for clarification. Include a screenshot of the figure drawn by the call binary_tree(6, 160). To get credit for part a): e do not change the function signature, do NOT draw any black dots, the screenshot must be of the figure drawn by the call binary_tree(6, 160).Explanation / Answer
//xC,yC is the starting coordinate for drawing
//height is taken to be 10*(3**depth) by default can be passed as a parameter in the tree function
def tree(depth,xC,yC):
#depth is the depth
if depth==0: #base case
return 0
Nodea = t.Turtle()
Nodeb = t.Turtle()
Nodea.penup()
Nodeb .penup()
Nodea.goto(xC,yC)
Nodeb.goto(xC,yC) # move to the correct position
Nodea.pendown()
Nodeb .pendown()
Nodea.left(45)//for specifying the angle on left node
Nodeb .right(45)//for specifying the angle on right node
Nodea.forward(10*(3**depth))
Nodeb.forward(10*(3**depth))
ax,ay = Nodea.pos() #get position of new turtles
bx,by = Nodeb .pos()
tree(depth-1,ax,ay) #for the top branch recursion
tree(depth-1,bx,by) #for the bottom branch recursion
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.