Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

ocaml language Library functions: You can use any library functions in the List module for this assignment (please do not use library functions in any

ocaml language
image text in transcribed
image text in transcribed
Library functions: You can use any library functions in the List module for this assignment (please do not use library functions in any other modules). You may find the following functions particularly useful, e.g., List.mapi, List.mem_assoc, and List.assoc. Datatypes: Problems 1-3 are about manipulating tree data structures. In OCaml, you can define a tree data structure using datatype: type 'a tree = Leaf Node of 'a tree * 'a * 'a tree We will assume binary search trees in this assignment and can define a bineary search tree insertion function as the following: let rec insert tree x match tree with Leaf -> Node (Leaf, x, Leaf) Node(1, y, r) -> if x = y then tree else if x insert acc x) Leaf 1 Problem 3 The usual recursive formulation of the function that computes the sum of a tree let rec sum_tree t = match t with Leaf -> 0 Node (1, x, r) -> sum_tree 1 + x + sum_treer is not tail-recursive. For a tail recursive implementation, you may need to use an auxiliary function of type aux: int -> tree list -> int Hint: aux acc ts takes as input an accumulator acc as usual and a list of sub-trees obtained from the input tree. The idea is that when the list is empty, acc is the sum of all input-tree elements. Implement a tail recursive function sumtailrec that uses this idea. sumtailrec: int tree -> int In [ ]: let sumtailrec t (* YOUR CODE HERE *) Library functions: You can use any library functions in the List module for this assignment (please do not use library functions in any other modules). You may find the following functions particularly useful, e.g., List.mapi, List.mem_assoc, and List.assoc. Datatypes: Problems 1-3 are about manipulating tree data structures. In OCaml, you can define a tree data structure using datatype: type 'a tree = Leaf Node of 'a tree * 'a * 'a tree We will assume binary search trees in this assignment and can define a bineary search tree insertion function as the following: let rec insert tree x match tree with Leaf -> Node (Leaf, x, Leaf) Node(1, y, r) -> if x = y then tree else if x insert acc x) Leaf 1 Problem 3 The usual recursive formulation of the function that computes the sum of a tree let rec sum_tree t = match t with Leaf -> 0 Node (1, x, r) -> sum_tree 1 + x + sum_treer is not tail-recursive. For a tail recursive implementation, you may need to use an auxiliary function of type aux: int -> tree list -> int Hint: aux acc ts takes as input an accumulator acc as usual and a list of sub-trees obtained from the input tree. The idea is that when the list is empty, acc is the sum of all input-tree elements. Implement a tail recursive function sumtailrec that uses this idea. sumtailrec: int tree -> int In [ ]: let sumtailrec t (* YOUR CODE HERE *)

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

More Books

Students also viewed these Databases questions

Question

Explain what a patent is and describe different types of patents.

Answered: 1 week ago

Question

- 1 6 ( 3 . 7 5 ) 2 + 1 2 0 ( 3 . 7 5 ) + 2

Answered: 1 week ago