CS 251: Programming Languages

Fall 2017

hw2: Binary search trees

Due: Wednesday, 09/20 at 22:00

This is a pair programming assignment. If you are on a team, this means that you and your partner should be doing the entirety of this assignment side-by-side, on a single computer, where one person is "driving" and the other is "navigating." Set a timer to swap every 15 minutes, say. You can choose your favourite from this online timer page. Make sure your sound volume is audible, but not so loud to disturb the people around you.

1. Goals

To write Scheme functions that implement integer binary search trees. To test Scheme code using a unit-testing framework.

2. Introduction

A nonempty binary tree can be represented as a list of three entries:

  1. the node's value (an integer),
  2. the node's left subtree, and
  3. the node's right subtree.
Nonempty left and right subtrees are themselves lists of three elements, similarly. An empty tree is represented as an empty list. For example, the list
(5 () ())

represents a tree whose only node is the leaf 5. The list

(5 (3 () (4 () ())) ())

represents the tree whose root node is 5 and has only one child, a left child, 3: this left child has only one child, a right child, 4.

In a binary search tree (BST) it is always the case that values in the left subtree are less than the root's value, and values in the right subtree are greater than the root's value. You may assume that a binary search tree that is given to you will never have duplicate entries, but you will need to make sure when inserting a new entry that it is not a duplicate.

3. Specification

Write the following functions. See hints in the next section before starting.

  1. Functions to create BSTs and access their elements:
    • (null-bst)
      Return an empty binary search tree.

    • (entry bst)
    • (left bst)
    • (right bst)
      These functions return the node's value, left subtree, and right subtree, respectively, of bst. If bst is empty or if bst does not contain three entries where the last two are lists, return #f.
    • (make-bst elt left right)
      This function returns a new tree whose root node's value is elt, left subtree is left, and right subtree is right. You should check that elt is in fact a number, and that left and right are either empty lists or lists of three elements each where the last two are lists. Return #f is the input is bad.

  2. Functions to check emptiness and validity:
    • (bst? bst)
      Return true (#t) if bst represents a valid BST, and false (#f) otherwise.

    • (null-bst? bst)
      Return #t if bst is empty, and #f otherwise.

  3. Functions on valid BSTs:

    In the following, the behaviour when bst is not a valid BST is unspecified. In other words, if the input is not a legal BST, your function can do anything it wants.

    • (member? v bst)
      Return #t if bst contains a node with value v, and #f otherwise.

    • (preorder bst)
    • (inorder bst)
    • (postorder bst)
      Return a list containing all values in bst in the order obtained from a preorder, inorder, or postorder traversal, respectively. Preorder visits the root before traversing the left and then the right subtrees recursively via preorder traversal. Inorder visits the root in between traversing left and right subtrees (recursively via inorder traversal). Postorder visits the root after traversing left and right subtrees (recursively via postorder traversal).
    • (insert v bst)
      Return a new binary search tree identical to bst but with integer v appearing in its proper location. (Do not change bst.) Smaller values are inserted to the left of a node, larger values to the right of a node. If v is already in the tree, just return the original tree without changing it.

4. Notes

5. Optional Challenges

I will mention some optional challenges from time to time. These will sometimes range from fairly easy to ridiculously hard. You should pursue them only for your own leisure. They are not to be turned in for extra credit.

6. Submission

Submit your code in a file called bst.scm. We will be grading this using Moodle's anonymous grading capabilities, so make sure that your names are not included in your file.

If you are part of a pair, one of you should submit to Moodle. The other should submit nothing. You will receive the same grade and comments via Moodle.

Starting from this homework, you will be graded on style.

Start early, have fun, and discuss questions on Moodle.