The tollowing Python program generates Motzkin trees. They are made ot leaves, u
ID: 3749011 • Letter: T
Question
The tollowing Python program generates Motzkin trees. They are made ot leaves, unary noces (noces with one branct) and binary nodes (nodes with two brances) t Motzkin tree of 3ize n dei mot (n) : if r--0: yiold 'loaf else for m in not (n-l): yield 'unary',m) for k in range(,n1): for 1 in mot. (k): or r in mot (n-2-k) : yield ('binary',1,r) def test (m) or n in range (m): ist (mapiprint, list. (mot. (n)))) write a Python program that generates the game output without using the "yield" atatement. The program may use Python arrays, tuples, dictionarie but no cla53ea To tacilitate grading, the program should provide a tunction similar to "test (m)", that given an natural number m, prints out all trees of size less than , one per line, in the aame formatExplanation / Answer
Code:
def motzkin(n) :
if n==0:
dp = [None] * (n+1)
#fundamental case
dp[0] = dp[1] = 1;
add = ['a', 'b', 'c']
add.append('leaf') #using array to deine
print(add)
for i in month(n-1):
# Finding i-th Motzkin number.
while i <= n(unary node) :
dp[i] = ((2 * i + 1) * dp[i - 1] +
(3 * i - 3) * dp[i - 2]) / (i + 2);
i = i + 1
return dp[n];
#using python tuple
for m in mot(n-1) :
my_tuple = ('u,n,a,r,y',m)
for k in range(0,n-1) :
for l in mot(k) :
print(my_tuple.count('unary'))
my_tuple = ('b,i,n,a,r,y',l,r)
for r in mot(n-2-k) :
print(my_tuple.index('binary',l,r))
('binary',l,r)
# Driver code
n = 8
print( motzkin(n) )
#using python dictionary
my_dict = {'leaves':'unary', 'binary':size n}
my_dict['m'] = "output"
print(my_dict)
def check(m) :
for n in range(m) : //provides a natural number m
print.trees([<<m]) //print trees less than m
list(map(print,list(mot(n))))
leaves = {}.fromnodes(['unary','binary'], 0)
for branch in nodes.leaves():
list(map(print,list(mot(n))))
print(list(mot))
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.