midterm1-sp13-solutions

# 7 4 binary search trees 28 points total recall the

This preview shows pages 7–10. Sign up to view the full content.

7

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

View Full Document
4. Binary Search Trees (28 points total) Recall the definition of generic binary trees and the binary search tree insert function: type ’a tree = | Empty | Node of ’a tree * ’a * ’a tree let rec insert (t:’a tree) (n:’a) : ’a tree = begin match t with | Empty -> Node(Empty, n, Empty) | Node(lt, x, rt) -> if x = n then t else if n < x then Node (insert lt n, x, rt) else Node(lt, x, insert rt n) end a. (5 points) Circle the trees that satisfy the binary search tree invariant . (Note that we have omitted the Empty nodes from these pictures.) (a) (b) (c) (d) (e) 4 3 2 3 7 \ / \ / \ \ \ 5 2 6 1 6 6 6 \ / \ / \ \ 6 5 4 2 7 5 \ \ 7 4 Answer: (a), (b) b. (8 points) For each definition below, circle the letter of the tree above that it constructs or “none of the above”. Grading Scheme: 4 points per answer let t1 : int tree = Node(Node(Empty, 1, Empty), 2, Node(Empty, 6, (Node (Empty, 4, Empty)))) (a) (b) (c) (d) (e) none of the above Answer: (c) let t2 : int tree = insert (insert (insert (insert Empty 4) 5) 6) 7 (a) (b) (c) (d) (e) none of the above Answer: (a) let t3 : int tree = insert (insert (insert (insert Empty 2) 5) 3) 6 8
(a) (b) (c) (d) (e) none of the above Answer: none of the above let t4 : int tree = Node(Empty, 3, Node(Node (Empty, 2, Empty), 6, (Node (Empty, 7, Empty)))) (a) (b) (c) (d) (e) none of the above Answer: (d) c. (15 points) Complete the definition of a function bst_separate that, when given an integer x, separates a binary search tree into two parts. The first part should contain the values less than x, the second part should contain the values greater than or equal to x. For example, when given the binary search tree t 5 / \ 3 6 / \ 0 4 the result of bst_separate 5 t is the pair of binary search trees: 3 5 / \ and \ 0 4 6 Your solution must take advantage of the binary search tree invariant to avoid traversing the entire tree and should not refer to any of the bst operations such as insert , remove , and inorder . (Use the next page for your implementation.) 9

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

View Full Document
let rec bst_separate (x:int) (t : int tree) : int tree * int tree = begin match t with | Empty -> (Empty, Empty) | Node (l, y, r) -> if x = y then (l, Node (Empty, y, r)) else if x < y then let (l1, l2) = bst_separate x l in (l1, Node (l2, y, r)) else let (r1, r2) = bst_separate x r in (Node (l, y, r1), r2) Grading Scheme: no deduction for minor syntax errors rough allocation of points: 2 for empty case, 3 for equality case, 5 for < case, and 5 for > case.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• 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.

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

• 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.

Dana University of Pennsylvania ‘17, Course Hero Intern

• 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.

Jill Tulane University ‘16, Course Hero Intern