Question: 1. Think of a binary tree to represent basic arithmetic operations. In such a tree, the leaf nodes will be of a numeric type

1. Think of a binary tree to represent basic arithmetic operations. In such a tree, the leaf nodes will be of a numeric type while the internal non-leaf nodes will hold the arithmetic operations of addition, subtraction, multiplication, and division. This means, we need to define a binary tree data type over two distinct data types. Define such a binary tree in OCaml. 2. Next, write down a function that takes a binary tree of the type you defined, and returns a tuple with its first element being a list of operators, and its second element being a list of the numeric values in the nodes. That is, the function's type should be val function name : ('a, 'b) tree -> 'b list * 'a list = where 'a is the operator's type and 'b is the numeric type. 3. Suppose we define the binary tree data type as follows: type 'a binary_tree | Empty | Node of 'a * = * 'a binary_tree 'a binary_tree;; a) Write a function called num_of_leaves to count the number of leaves in a binary tree. b) Write a function called get_all_leaves to collect all the leaves in a list.
Step by Step Solution
There are 3 Steps involved in it
1 Binary Tree Data Type for Arithmetic Expressions OCaml type a exprtree Empty Node of string a exprtree a exprtree Explanation a exprtree This define... View full answer
Get step-by-step solutions from verified subject matter experts
