{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

midterm1-sp12-blank

Let t1 int tree node node node empty 1 empty 2 empty

Info iconThis preview shows pages 7–11. Sign up to view the full content.

View Full Document Right Arrow Icon
let t1 : int tree = Node (Node (Node (Empty, 1, Empty), 2, Empty), 3, Empty) (a) (b) (c) (d) (e) none of the above let t2 : int tree = Node (Empty, 3, Node (Empty, 2, Node (Empty, 1, Empty))) (a) (b) (c) (d) (e) none of the above let t3 : int tree = Node (Empty, 1, Node (Empty, 2, Node (Empty, 3, Empty))) (a) (b) (c) (d) (e) none of the above let t4 : int tree = Node (Node (Empty, 1, Empty), 2, Node (Empty, 3, Empty)) (a) (b) (c) (d) (e) none of the above 7
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
c. (12 points) Complete this definition of a function that returns the leaves of the given tree from left-to-right. For example, calling leaves on tree (a) returns [1;0;4] . You may use the @ operator (i.e. list append) in your solution. let rec leaves (t:’a tree) : ______________ = 8
Background image of page 8
5. Binary Search Trees (21 points) a. (9 points) Recall the delete function for binary search trees from class. (This function uses the same tree datatype from the previous problem.) let rec tree_max (t:’a tree) : ’a = begin match t with | Empty -> failwith "tree_max called on empty tree" | Node(_,x,Empty) -> x | Node(_,_,rt) -> tree_max rt end let rec delete (t:’a tree) (n:’a) : ’a tree = begin match t with | Empty -> Empty | Node(lt,x,rt) -> if x = n then begin match (lt, rt) with | (Empty, Empty) -> Empty | (Empty, rt) -> rt | (lt, Empty) -> lt | (lt, rt) -> let y = tree_max lt in (Node (delete lt y, y, rt)) end else if n < x then Node(delete lt n, x, rt) else Node(lt, x, delete rt n) end (This problem continues on the next page.) 9
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Let t be the BST depicted below. 6 / \ 4 7 / \ \ 2 5 8 For each separate call to delete with this tree , draw the result: delete t 2 delete t 7 delete t 6 10
Background image of page 10
b. (12 points) Implement bst_filter . The bst_filter function applies a given predicate to each element in an input tree to see if it should be included in the output. (This function is analogous to the list filter function from homework four.) For example below, filtering the tree on the left with a predicate for even numbers results in the tree on the right: 6 6 / \ bst_filter is_even / \ 4 7 --------> 4 8 / \ \ / 2 5 8 2 Below, complete the definition, including the types of pred and the result type of the func- tion. In your implementation, you must use the BST delete function. let rec bst_filter (pred: ________________) (t : ’a tree) : ____________ = 11
Background image of page 11
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}