Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Using Standar ML language: In this programming assignment, you will use case expressions and pattern matching to process a datatype that implements a binary tree.
Using Standar ML language:
In this programming assignment, you will use case expressions and pattern matching to process a datatype that implements a binary tree. You will also write higher-order functions for traversing your datatype and use those higher-order functions with anonymous functions as arguments. The datatype for the binary tree is as follows: datatype Node = Empty NonEmpty of int * Node * Node (1) Construct a value myTree of type Node that stores the binary tree shown on the right. (2) Write a function tree Sum that takes a value of type Node and returns the sum of all the integers stored in the nodes of the tree rooted at that node. (3) Write a function treesize that takes a value of type Node and returns the total number of nodes in the tree rooted at that node. (4) Write a function numGreaterThan that takes a value of type Node and an integer n and returns the total number of nodes holding a value greater than n. (5) Write a function numLeaves that takes a value of type Node and returns the total number of leaves in the tree rooted at that node (hint: use nested pattern matching). (6) Write a function numNoRightChild that takes a value of type Node and returns the total number of non-leaf nodes with no right child in the tree rooted at that node (hint: use nested pattern matching). (7) Write a higher-order function traverseTree to traverse the binary tree. The function should take a value of type Node, a value to return for the Empty variant, and a function to process the Non Empty variant. (8) Write a function tree Sum2 that implements tree Sum using the higher-order function traverseTree and pass anonymous functions to it as arguments. (9) Write a function numGreaterThan2 that implements numGreaterThan using the higher- order function traverseTree and pass anonymous functions to it as arguments. (10)This question is unrelated to questions 1-9. The following function sumof Products computes the logical sum of products for a set of minterms stored as a list of pairs of booleans. fun sumof Products 1st - case 1st of [] => false 1(x, y)::lst' => x andalso y orelse sumof Products (1st') Rewrite this function to perform the same operation but implemented using tail recursion. Do no use the built-in fold function. In this programming assignment, you will use case expressions and pattern matching to process a datatype that implements a binary tree. You will also write higher-order functions for traversing your datatype and use those higher-order functions with anonymous functions as arguments. The datatype for the binary tree is as follows: datatype Node = Empty NonEmpty of int * Node * Node (1) Construct a value myTree of type Node that stores the binary tree shown on the right. (2) Write a function tree Sum that takes a value of type Node and returns the sum of all the integers stored in the nodes of the tree rooted at that node. (3) Write a function treesize that takes a value of type Node and returns the total number of nodes in the tree rooted at that node. (4) Write a function numGreaterThan that takes a value of type Node and an integer n and returns the total number of nodes holding a value greater than n. (5) Write a function numLeaves that takes a value of type Node and returns the total number of leaves in the tree rooted at that node (hint: use nested pattern matching). (6) Write a function numNoRightChild that takes a value of type Node and returns the total number of non-leaf nodes with no right child in the tree rooted at that node (hint: use nested pattern matching). (7) Write a higher-order function traverseTree to traverse the binary tree. The function should take a value of type Node, a value to return for the Empty variant, and a function to process the Non Empty variant. (8) Write a function tree Sum2 that implements tree Sum using the higher-order function traverseTree and pass anonymous functions to it as arguments. (9) Write a function numGreaterThan2 that implements numGreaterThan using the higher- order function traverseTree and pass anonymous functions to it as arguments. (10)This question is unrelated to questions 1-9. The following function sumof Products computes the logical sum of products for a set of minterms stored as a list of pairs of booleans. fun sumof Products 1st - case 1st of [] => false 1(x, y)::lst' => x andalso y orelse sumof Products (1st') Rewrite this function to perform the same operation but implemented using tail recursion. Do no use the built-in fold functionStep 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