Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please code in OCaml. For this question, just code the pre_order, depth, and trim functions using the tree_fold function that is provided. You are not

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Please code in OCaml.

For this question, just code the pre_order, depth, and trim functions using the tree_fold function that is provided. You are not allowed to use recursion for implementing these functions, nor are you allowed to use recursive helpers for these methods (you can use helper methods, but just not recursive).

Please only use library functions found in the Stdlib module and the List module libraries, and please check to see if your code works using examples. Thank you.

type 'a tree = | Node of 'a tree *'a 'a 'a tree Leaf let tree_fold f init tree = let rec aux = function Leaf init | Node (l,v,r)f(auxl)v( aux r) in aux tree let map tree f= let f facc lr= Node (l,fx,r) in tree_fold f facc Leaf tree pre_order tree - Type: ('a tree 'a list) - Description: Using tree_fold, write a function that will return a list containing the pre order traversal of the tree. - Examples: let treea = Node(Node(Leaf, 1, Leaf), 2, Node(Leaf, 3, Leaf)) let treeb = Node(Node(Leaf, 1, Leaf), 2, Node(Node(Leaf, 3, Leaf), 4, Leaf)) \( \begin{array}{l}\text { pre_order treea }=[2 ; 1 ; 3] \\ \text { pre_order treeb }=[2 ; 1 ; 4 ; 3]\end{array} \) depth tree - Type: ('a tree int) - Description: Using tree_fold write a function that returns the depth of the deepest node in the tree. - Examples: let treea = Node(Node(Leaf, 1, Leaf), 2, Node(Leaf, 3, Leaf)) in let treeb = Node(Node(Leaf, 1, Leaf), 2, Node(Node(Leaf, 3, Leaf), 4, Leaf)) in depthtreea=2depthtreeb=3depthLeaf=0 trim tree n - Type: ('a tree int ' a tree) - Description: Using tree_fold write a function that takes in a complete binary tree tree and an integer n, and trims off nodes at the bottom of the tree such that the depth of the returned tree is at most n. - Examples: let tree = Node(Node(Node(Leaf, 4, Leaf), 2, Leaf), 1, Node(Node(Leaf, 4, Leaf), 2, Node(Leaf, 4, Leaf))) in trim tree 1= Node(Leaf, 1, Leaf); trim tree 2= Node(Node(Leaf, 2, Leaf), 1, Node(Leaf, 2, Leaf))

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Jeffrey A. Hoffer Fred R. McFadden

4th Edition

0805360476, 978-0805360479

Students also viewed these Databases questions