Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Exercise 2.1 For each of the following tree operations, invent an expression which encodes it, briefly describe how it works, and B-reduce an example to
Exercise 2.1 For each of the following tree operations, invent an expression which encodes it, briefly describe how it works, and B-reduce an example to show it that works.) .NIL should represent an empty tree. . (MAKETREE e a b) should make a tree with e at the root, and with the given subtrees a, b attached as the left and right children. Give a couple of examples. (ROO ) shouhnttedat the root of the tree. (LEFT ) should return the subtree which is the left child of the root. (RIGHT t) should return the subtree which is the right child of the root. (ISEMPTY t) should return TRUE if the tree is empty, FALSE otherwise. (ISLEAF t) should return TRUE if the tree is just a leaf, FALSE otherwise. You do not have to include error handling (i.e. it doesn't matter what a nonsense expression like LEFT NIL) reduces to. Exercise 2.2 Using the operations you defined above, write recursive expressions for the following, more complex func- tions. briefly describe how they work, but you do not need to give fully worked examples. (SUM t) should sum all the values stored in the tree. (HEIGHT t) should return the height of the tree. ISPROPER t) should return TRU E if t is a proper tree (every position is either a leaf, or it has 2 children). .(MAKEPROPER t) should return a proper tree, equivalent to the original tree except wherever a position had exactly one child, that child is no longer in the tree (i.e. we return the maximal proper subtree of t.)
Step 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