{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

midterm1-sp13-solutions

7 4 binary search trees 28 points total recall the

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

View Full Document Right Arrow Icon
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
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
Background image of page 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
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 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.
Background image of page 10
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}