Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem 1. Recursive Functions a) Binary Tree. Write a function in file pl.py wi

ID: 3708219 • Letter: P

Question

Problem 1. Recursive Functions 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): do not change the function signature, do NOI draw any black dots, the screenshot must be of the figure drawn by the call binary_tree(6, 160).

Explanation / Answer

CODING:

//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

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote