Question: Homework 0 9 : Trees This week, we ve been introduced to our second recursively - defined data structure, trees! ( As a reminder, the

Homework 09 : Trees This week, weve been introduced to our second recursively-defined data structure, trees! (As a reminder, the f irst one was linked lists.) Recall from lecture that a tree is either: the empty tree, represented by None in Python, or anode that points to other trees. Nodes usually contain some data as well. Important Note As trees have a natural recursive definition and structure, functions that operate on tree often have simple and elegant recursive definitions. For this homework, all of your function implementations must be recursive and you cannot use any iteration (i.e. for/while loops). Part 1: Simple Tree Functions In the starter code file, simple_tree_functions.py, implement the following functions. height(t) Returns the height of the tree t. Returns-1 for the empty tree. size(t) Returns the number of nodes in the tree t. find_min(t) Returns the minimum element contained within a node in the tree t. Returns float("inf") for the empty tree. find_max(t) Returns the maximum element contained within a node in the tree t. Returns float("-inf") for the empty tree. contains(t, k) Returns True if and only if k is contained within one of the nodes of t (returns False otherwise). in_order(t) Returns a list containing contents of each node in the tree in the order of an in-order traversal of the tree. pre_order(t) Returns a list containing contents of each node in the tree in the order of an pre-order traversal of the tree. post_order(t) Returns a list containing contents of each node in the tree in the order of an post-order traversal of the tree. 1Examples """0/\14//\253""">>>n1=TreeNode(1,left=TreeNode(2))>>>n4=TreeNode(4,left=TreeNode(5),right=TreeNode(3))>>>tree = TreeNode(0,left=n1,right=n4)>>>height(tree)2>>>size(tree)6>>>find_min(tree)0>>>find_max(tree)5>>>contains(tree,3) True >>>contains(tree,6) False >>>in_order(tree)[2,1,0,5,4,3]>>>pre_order(tree)[0,1,2,4,5,3]>>>post_order(tree)[2,1,5,3,4,0] Part2:Higher-OrderTreeFunctions Ahigher-orderfunctionisafunctionthatdoesat leastoneofthefollowing: takesoneormorefunctionsasarguments,or returnsafunctionasitsresult. Inthispartof thehomework,wewillexploresomefunctionsthattakeother functionsas inputandalso operateontrees. Oftentimesthefunctionspassedtoourhigher-orderfunctionswill taketheformofalambdaexpression, (e.g.lambdax:x4).Theseexpressionsprovideanelegantwayforustocreatesmallanonymousfunctions. Foralittlemoreinfoonlambdaexpressions,checkout: https://docs.python.org/3/reference/expressions.html#lambda https://www.w3schools.com/python/python_lambda.asp Inthestartercodefile,higher_order_tree_functions.py, implementthefollowingfunctions. count_failures(pred,t)2Takesapredicatefunction(afunctionthatreturnseitherTrueorFalse)pred,andreturnsthe numberofnodesinthetreetwhosedatafailsthepredicate. Iftisanon-emptytreenode,thenitsdatafailsthepredicateif pred(t.data)returnsFalse.tree_map(f,t)Takesafunctionf,andreturnsanewtreeconsistingofthenodesfromtsuchthatfhasbeen appliedtothedatawithineachofthenodesint.ThisfunctiondoesNOTmutatethetreet, itreturnsanewtree. tree_apply(f,t)Takesafunctionf,andapplies ittothedataheldwitheachnodeofthetreet.Thisfunctionmutatesthetreet. Examples """0/\14//\253""">>>n1=TreeNode(1,left=TreeNode(2))>>>n4=TreeNode(4,left=TreeNode(5),right=TreeNode(3))>>>tree = TreeNode(0,left=n1,right=n4) #CountFailures >>>count_failures(lambda x:x%2==0,tree)3 #3nodescontainintegersthatarenoteven >>>count_failures(lambda x:x4, tree)2 #2nodescontainintegersthatarenotlessthan4>>>count_failures(lambda x:x0, tree)6 #6nodescontainintegersthatarenotlessthan0 #TreeMap >>>another_tree = tree_map(lambda x:x**2,tree)>>>in_order(another_tree)[4,1,0,25,16,9]>>>in_order(tree)[2,1,0,5,4,3] #originaltreewasnotmutatedby tree_map #TreeApply >>>tree_apply(lambdax:x**3,tree)3>>> in_order(tree)[8,1,0,125,64,27] # original tree was mutated by tree_apply Testing Note that we are not requiring unit tests in your submission. At this point in the semester, we expect that you are developing your own test suites for these questions as we do not provide any unit tests on Gradescope. Therefore, your tests will not be graded, but developing a thorough suite of tests is the only way that you will be able to ensure your function implementations exhibit the correct behavior. Imports No imports allowed on this assignment, with the following exceptions: Any modules you have written yourself. typing- this is not required, but some students have requested it. Submission Submit the following files to Gradescope: simple_tree_functions.py higher_order_tree_functions.py Please note this assignment is 100% manually graded. Students must submit individually by the due date (Tuesday, April 9th at 11:59 pm EST) to receive credit.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting Questions!