Question: Given the following ML declarations of a binary tree type with integers in the leaf nodes and of five functions operating on trees: datatype tree
Given the following ML declarations of a binary tree type with integers in the leaf nodes and of five functions operating on trees:
datatype tree = L of int (* leaf with an integer *) | N of tree * tree (* node with two subtrees *)
fun max (n, m) = if n > m then n else m
fun a (L i) = i | a (N (l, r)) = a l + a r
fun b (L i) = i | b (N (l, r)) = max (b l, b r)
fun c (L i) = 1 | c (N (l, r)) = c l + c r
fun d (L i) = 0 | d (N (l, r)) = 1 + d l + d r
fun e (L i) = 0 | e (N (l, r)) = 1 + max (e l, e r)
In each function definition, the first line is the case for a leaf node (i.e., the base case of the recursion), and the second line recursively calls the function for the left and right subtrees.
These functions compute:
the height of the tree, and the number of leaves in the tree, the sum of all the values in the leaves, the number of interior nodes in the tree, the maximum value of any leaf node in the tree.
Associate the functions with their descriptions. Try it first without typing them in.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
