Question: Homework 0 9 : Trees This week, we ve been introduced to our second recursively - defined data structure, trees! ( As a reminder, the
Homework : Trees This week, weve been introduced to our second recursivelydefined 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 ie forwhile loops Part : Simple Tree Functions In the starter code file, simpletreefunctions.py implement the following functions. heightt Returns the height of the tree t Returns for the empty tree. sizet Returns the number of nodes in the tree t findmint Returns the minimum element contained within a node in the tree t Returns floatinf for the empty tree. findmaxt Returns the maximum element contained within a node in the tree t Returns floatinf" for the empty tree. containst k Returns True if and only if k is contained within one of the nodes of t returns False otherwise inordert Returns a list containing contents of each node in the tree in the order of an inorder traversal of the tree. preordert Returns a list containing contents of each node in the tree in the order of an preorder traversal of the tree. postordert Returns a list containing contents of each node in the tree in the order of an postorder traversal of the tree. Examples nTreeNodeleftTreeNodenTreeNodeleftTreeNoderightTreeNodetree TreeNodeleftnrightnheighttreesizetreefindmintreefindmaxtreecontainstree True containstree False inordertreepreordertreepostordertree Part:HigherOrderTreeFunctions Ahigherorderfunctionisafunctionthatdoesat leastoneofthefollowing: takesoneormorefunctionsasargumentsor returnsafunctionasitsresult Inthispartof thehomework,wewillexploresomefunctionsthattakeother functionsas inputandalso operateontrees. Oftentimesthefunctionspassedtoourhigherorderfunctionswill taketheformofalambdaexpression, eglambdax:xTheseexpressionsprovideanelegantwayforustocreatesmallanonymousfunctions. Foralittlemoreinfoonlambdaexpressions,checkout: https:docspython.orgreferenceexpressionshtml#lambda https:wwwwschools.compythonpythonlambda.asp Inthestartercodefile,higherordertreefunctions.py implementthefollowingfunctions. countfailurespredtTakesapredicatefunctionafunctionthatreturnseitherTrueorFalsepredandreturnsthe numberofnodesinthetreetwhosedatafailsthepredicate. Iftisanonemptytreenode,thenitsdatafailsthepredicateif predtdatareturnsFalsetreemapftTakesafunctionfandreturnsanewtreeconsistingofthenodesfromtsuchthatfhasbeen appliedtothedatawithineachofthenodesint.ThisfunctiondoesNOTmutatethetreet itreturnsanewtree. treeapplyftTakesafunctionfandapplies ittothedataheldwitheachnodeofthetreet.Thisfunctionmutatesthetreet Examples nTreeNodeleftTreeNodenTreeNodeleftTreeNoderightTreeNodetree TreeNodeleftnrightn #CountFailures countfailureslambda x:xtree #nodescontainintegersthatarenoteven countfailureslambda x:x tree #nodescontainintegersthatarenotlessthancountfailureslambda x:x tree #nodescontainintegersthatarenotlessthan #TreeMap anothertree treemaplambda x:xtreeinorderanothertreeinordertree #originaltreewasnotmutatedby treemap #TreeApply treeapplylambdax:xtree inordertree # original tree was mutated by treeapply 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: simpletreefunctions.py higherordertreefunctions.py Please note this assignment is manually graded. Students must submit individually by the due date Tuesday April th at : pm EST to receive credit.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
