Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Note : You can use google drive lecture notes to solve below : https://drive.google.com/file/d/1ZOuSgDKs-k3O2KoyIsuxPPB0_dvDz0Ew/view?usp=sharing Trees An important collection of data types are trees. We will
Note : You can use google drive lecture notes to solve below :
https://drive.google.com/file/d/1ZOuSgDKs-k3O2KoyIsuxPPB0_dvDz0Ew/view?usp=sharing
Trees An important collection of data types are trees. We will see them in action in lecture 10, where they are used to represent (the structure of) expressions, but they can also be used to store values, which we cover in lecture 9. Here is a tree type used to store integers: type value)integer O type tree): leaf I(node,valueo tree),tree)) Here are some example trees beo (node,2,leaf, (node, 2,leat,leaf) blo(node,2. (node, 2,leaf,leat), (node,3,leaf,leaf)) b20 (node, 2. (node,1,leaf.leaf),b1)) 1.1 Define a function tdepth that takes a tree and measures the number of nodes in longest branch in the tree. For example, the depth of be ) and b1) is 2, and the depth of b2) is 3 1.2 Define a function leaves that takes a tree and counts the number of Leafs in the tree. For example, this function would return 4 when applied to b1) 1.3 Define a function flatten that takes a tree and returns a list of integers got by "flattening the tree into a list. At each point in the tree you should list the numbers in the left-hand sub tree before the value at the node, and this should be before the values in the right-hand tree 1.4 Define a function idx that takes a tree Tand a number N and retums the Nth element of T when the elements are ordered as in question 1.3 above, and counting begins at zero. You might find it useful to define a function tnodes that counts the number of nodes in a tree T. For example idx(b2().) is 1, idx(b2(). 1) is 2 and idx(b2() ,4) is 3 Trees An important collection of data types are trees. We will see them in action in lecture 10, where they are used to represent (the structure of) expressions, but they can also be used to store values, which we cover in lecture 9. Here is a tree type used to store integers: type value)integer O type tree): leaf I(node,valueo tree),tree)) Here are some example trees beo (node,2,leaf, (node, 2,leat,leaf) blo(node,2. (node, 2,leaf,leat), (node,3,leaf,leaf)) b20 (node, 2. (node,1,leaf.leaf),b1)) 1.1 Define a function tdepth that takes a tree and measures the number of nodes in longest branch in the tree. For example, the depth of be ) and b1) is 2, and the depth of b2) is 3 1.2 Define a function leaves that takes a tree and counts the number of Leafs in the tree. For example, this function would return 4 when applied to b1) 1.3 Define a function flatten that takes a tree and returns a list of integers got by "flattening the tree into a list. At each point in the tree you should list the numbers in the left-hand sub tree before the value at the node, and this should be before the values in the right-hand tree 1.4 Define a function idx that takes a tree Tand a number N and retums the Nth element of T when the elements are ordered as in question 1.3 above, and counting begins at zero. You might find it useful to define a function tnodes that counts the number of nodes in a tree T. For example idx(b2().) is 1, idx(b2(). 1) is 2 and idx(b2() ,4) is 3Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started