Answered step by step
Verified Expert Solution
Question
1 Approved Answer
In SML: Tree type: datatype 'a Tree = LEAF of 'a | NODE of 'a * ('a Tree) * ('a Tree); (a) addTrees - 15%
In SML:
Tree type:
datatype 'a Tree = LEAF of 'a | NODE of 'a * ('a Tree) * ('a Tree);
(a) addTrees - 15% Write a function addTrees takes two int Tree values and returns an int Tree where the corresponding nodes from the two trees are added. The trees might have different depth. You should copy particular branchesodes of the trees as it is if the other tree doesn't have that branchode. See the example below The type of the addTrees function should be int Tree -> int Tree -> int Tree 1 2 2 2 14 12 16 18 4 10 5 10 We can create the above int Tree (s) as follows: left vai T1 NODE (1, NODE (2, NODE (3, LEAF 4 ,LEAF 5), LEAF 6), NODE (7, LEAF 8, LEAF 9)) right: val T2 NODE (1, NODE (2, LEAF 3, LEAF 6), NODE (7, NODE (8, LEAF 10 , LEAF 11), LEAF 9)) And addTrees T1 T2 will return the rightmost int Tree which is equivalent to the following: NODE (2,NODE (4, NODE (6,LEAF 4,LEAF 5),LEAF 12), NODE (14, NODE (16,LEAF 10,LEAF 11), LEAF 18)) (a) addTrees - 15% Write a function addTrees takes two int Tree values and returns an int Tree where the corresponding nodes from the two trees are added. The trees might have different depth. You should copy particular branchesodes of the trees as it is if the other tree doesn't have that branchode. See the example below The type of the addTrees function should be int Tree -> int Tree -> int Tree 1 2 2 2 14 12 16 18 4 10 5 10 We can create the above int Tree (s) as follows: left vai T1 NODE (1, NODE (2, NODE (3, LEAF 4 ,LEAF 5), LEAF 6), NODE (7, LEAF 8, LEAF 9)) right: val T2 NODE (1, NODE (2, LEAF 3, LEAF 6), NODE (7, NODE (8, LEAF 10 , LEAF 11), LEAF 9)) And addTrees T1 T2 will return the rightmost int Tree which is equivalent to the following: NODE (2,NODE (4, NODE (6,LEAF 4,LEAF 5),LEAF 12), NODE (14, NODE (16,LEAF 10,LEAF 11), LEAF 18))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