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

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

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
Image of page 7

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

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
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
Image of page 9

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

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
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
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.
  • Fall '09
  • Self-balancing binary search tree, val empty, int list list

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern