CMPSCI 250: Introduction to Computation

Second Midterm Exam

David Mix Barrington

12 November 2015


  Q1: 20 points
  Q2: 25 points
  Q3: 25+10 points
  Q4: 30 points
Total: 100+10 points

Question 3 uses the following real-Java class, whose objects are binary search trees. Every node, leaf or not, contains a String data item.

public class BST {
   boolean isLeaf;
   String data;
   BST left, right;

   public BST (String w) {isLeaf = true; data = w;}
   public BST (String w, BST lTree, BST rTree) {
      isLeaf = false; data = w; left = lTree; right = rTree;}
   // getters and setters
   // instance methods added in Question 3

A BST object is defined to be valid if for every non-leaf node v, every item in the left subtree of v is strictly less than the data item of v itself, and every data item in the right subtree of v is strictly greater than that of v itself.

You will probably want the following two methods of the real Java String class. If x and y are String objects, x.equals(y) returns true if and only if x and y have the same letters in the same order. And x.compareTo(y) returns an int that is negative if x comes before y in alphabetical order, 0 if they are equal, and positive if y comes before x.

Question 4 uses the undirected graph G, and the implicit directed graph D, given below. Each graph has the same six nodes, each representing a town in the Pioneer Valley. In G, there is an edge from x to y if it is possible to drive directly from x to y. Each node in G is also labeled with the value h(x), which is the driving time (in minutes) from x to Northampton (Ntn) with no traffic.

The directed graph D has two directed edges for each edge in G. The edge from x to y, if it exists, is labeled with the driving time from x to y during rush hour. This value is given in the row for x and the column for y in the matrix below. A "--" entry in the matrix indicates a pair of towns for which there is no direct connection.

Undirected graph G representing road connections in the Pioneer Valley:

     18 Sun -------------
       /   \             \
      /     \             \
     /       \             \
    /         \  16         \    25
   |           NHa --------- NAm
   |            |            |
   |            |            |
   |            |            |
   | 0          | 10         | 18
  Ntn -------- Had --------- Amh 

(Numbers by towns represent h(x), driving time to NO without traffic.)

Labels for directed graph D (single-step cost matrix for rush hour):

     Amh Had NAm NHa Ntn Sun
 Amh   0  15  15  --  --  --
 Had  12   0  --   8  30  --
 NAm  19  --   0  10  --  14
 NHa  --  10   9   0  --  12
 Ntn  --  15  --  --   0  20
 Sun  --  --  11  12  22   0

Last modified 22 November 2015