Question: 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 Note : You can use google drive lecture notes to solve below

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 3

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 Databases Questions!