CSE 526: Principles of Programming Languages, Spring 2020

Homework #3

Due: Thu., Apr 16


This is a OCAML programming assignment. You are expected to write an OCAML program that consists of functions described below. Unless otherwise specified, the solution to each question may have multiple OCAML predicates. You may also use the solution to one question to solve another question. Place all the OCAML predicate definitions you write to solve the following problems in a single file called hw3.ml, and submit the file via Blackboard (hand-in procedure is described at the end of this handout).

Important: All functions in this homework should be purely functional. Your solutions should not use ML's imperative features, references, side-effecting primitives, etc.

    Part A (Warm-Up):

  1. before: Write a function before with signature 'a list * 'a -> 'a list such that before(l, x) returns the list of all elements that occur immediately before x in l For instance:

  2. order: Write a function order with signature 'a list * 'a * 'a -> bool such that order(l, x, y) returns true if and only if elements x and y occur in list l such that x occurs before y. For instance:

  3. split: Write a function split with signature 'a list * 'a -> 'a list * 'a list such that split(l, x) returns a pair of lists (p, s) such that p is the list of all elements that occur before x in l, and s is the list of all elements that occur after x in l. The order of elements in p and s are same as that in l. If there is no element x in l, then the function should raise exception NoneSuch. For instance:

    Part B (Trees):

    For the following four questions, consider using OCAML datastructure, defined by
    type 'a tree = Node of 'a * 'a tree * 'a tree | Empty;;
    to represent binary trees.

    balanced tree unbalanced/ordered tree balanced/ordered tree
    (a) (b) (c)

    For instance,

  4. isBalanced: Write a function isBalanced with signature 'a tree -> bool that returns true if and only if t is a binary tree that is height-balanced: i.e., the lengths of the longest root-to-leaf path and the shortest root-to-leaf path differ by at most one.

    For example, trees (a) and (c) in the picture above are balanced.

  5. isOrdered: Write a function isOrdered with signature 'a tree -> bool that returns true if and only if an in-order traversal of t spells out an increasing sequence of values. For this part, you may assume that all values in t are comparable with "<".
    Note: In-order traversal of a tree traverses, at each level, the left subtree, the root, and then the right subtree.

    For example, trees (b) and (c) in the above picture are ordered.

  6. buildBalanced: Write a function buildBalanced with 'a list -> 'a tree such that buildBalanced(l) results in a balanced tree t such that an in-order traversal of t spells out l.

    For example, buildBalanced([1;2;3;5;8;13]) can give the tree (c) in the picture above, as one possible answer. (Other balanced trees may be possible as well; you can make that choice.)

  7. postOrder: Write a function postOrder with signature 'a tree -> 'a list that returns a list l such that post-order traversal of t spells out l.
    Note: Post-order traversal of a tree traverses, the left subtree, the right subtree, and then the root.

    For example,

    Note: For full credit in this problem, your solution should work for trees with millions of nodes. As a point of reference, there are solutions where trees with 100,000 nodes (irrespective of skew and balance) can be processed in less than a second.

Grading:

There are 7 questions in all, worth 6 points each. Note: the difficulty levels vary significantly between questions, but they are all worth the same number of points.

Submission:

Submit your homework via Blackboard. In "Assignments" area there you will see a form for HW1. Fill it out by uploading the file containing your Prolog program.

The file must be named hw3.ml

Make sure you submit the form.

Errata:

Earlier versions had the following errors that have since been corrected:
  1. None so far.

C.R. Ramakrishnan
Last modified: Tue Apr 7 23:16:47 EDT 2020