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

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

Subscribe to view the full document.

Reference Appendix Make sure all of your answers are written in your exam booklet. These pages are provided for your reference—we will not grade any answers written in this section. Reader JavaDoc (excerpt) for problem 3 From the Reader JavaDocs: int throws IOException Reads a single character. This method will block until a character is available, an I/O error occurs, or the end of the stream is reached. Subclasses that intend to support efficient single-character input should override this method. Returns: The character read, as an integer in the range 0 to 65535 ( 0x00 - 0xffff ), or -1 if the end of the stream has been reached Throws: IOException — If an I/O error occurs 17
Image of page 17

{[ snackBarMessage ]}

Get FREE access by uploading your study materials

Upload your study materials now and get free access to over 25 million documents.

Upload now for FREE access Or pay now for instant access
Christopher Reinemann
"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

Ask a question for free

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