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
natural f (natural n)
be a recursive
pseudo-Java method. If f(0)
terminates without
recursion, and f(n)
for positive n terminates except
for calls to f(k)
with k ≤ n, then
f(n)
must termine for all naturals n.
BST
whose objects are rooted binary search trees
somewhat similar to the boolean expression trees of Discussion #7.
In a BST
object every node, leaf or not, has a
String
data item. A BST
object is
valid if for every non-leaf node v, every data item in v's
left subtree is strictly less then v's data item, and and every data
item in v's right subtree is strictly greater than v's data item.
(Thus a valide BST
cannot have two nodes with equal data items.)
definitions, and predicates above, and the statements from Question 1.
public boolean search
(String w)
for this class. If the calling object is
valid, this method returns true
if and only if
one of the data items in the calling object is equal to w. (If the
object is not valid, I don't care what your method does.)
BST
objects, with a base case for one-node trees and an
inductive case for trees with left and right subtrees.
String
. Prove,
by induction on valid BST
objects, using your
definition from part (b), that your search
method is
correct when called with parameter w.
public boolean
isValid( )
for this class. Your method should return
true
if and only if the calling object is valid. You
may want to write helper instance methods. (Hint: Use recursion.)
Also above is the single-step cost matrix for a labeled directed graph D with the same six nodes as G, and two directed edges for each edge in G. The label on a directed edge (x, y) represents the time (in minutes) to drive directly from x to y during rush hour.
For example, at rush hour it takes 15 minutes to drive from Amherst to eithe Hadley or North Amherst. Since one cannot drive directly from Amherst to any of the other three towns, the matrix has a "--" entry in the other locations in the first row. The third row indicates that from North Amherst, it takes 19 minutes to drive to Amherst, 10 to drive to North Hadley, or 14 to drive to Sunderland. Note that driving time between towns depends on which direction you are going, which is why we need a directed graph.
Last modified 22 November 2015