final-fa12-solutions

# The tree nodelt x rt is a binary search tree if and

• Notes
• 17

This preview shows page 16 - 17 out of 17 pages.

The tree Node(lt, x, rt) is a binary search tree if and only if: lt and rt are both binary search trees and x is greater than all values in lt and less than all values in rt . Rubric: 5 pts.: 2 pts. for lt and rt are both bsts. 3 pts for less-than and greater-than constrainta b. Write an OCaml function range that, given an integer binary search tree t and integers low and hi such that low < hi , returns the list of BST nodes such that low <= x <= hi (in sorted order). For example, range t 1 6 would yield [1;2;3;5] when t is the tree to the right. Use the binary search tree invariant to avoid processing more of the tree than necessary. If you need help remembering OCaml syntax, see the examples in problem 6. 3 / \ / \ 1 8 / \ / 0 2 5 \ 7 ( * Assumes: t is a binary search tree * ) let rec range (t:int tree) (low:int) (hi:int) : int list = begin match t with | Empty -> [] | Node(lt,x,rt) -> if low <= x && x <= hi then (range lt low hi) @ [x] @ (range rt low hi) else if x < low then range rt low hi else range lt low hi end Rubric: 15 pts. 3 pts for base case 5 pts for x is in range and recursive calls 7 pts for using invariant to search when x is not in range 16

Subscribe to view the full document.

• Fall '09

{[ snackBarMessage ]}

###### "Before using Course Hero my grade was at 78%. By the end of the semester my grade was at 90%. I could not have done it without all the class material I found."
— Christopher R., University of Rhode Island '15, Course Hero Intern

### 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