Comp201: Principles of Object-Oriented Programming I
Spring 2006 -- Exam 3    


Instructions

  1. The examination is open book/notes.  You are allowed to consult any written materials available under the sun that was published or posted before the start of the exam.  If your code is based on materials that we did not provide you, just give us the appropriate references.
  2. You are not allowed to discuss the exam in any way with anybody beside the instructors of this course (i..e. Nguyen and Wong, but not the labbies) .
  3. Do not post your questions on the newsgroup.  Use the WebCT exam chat rooms during the designated times or e-mail both of us, dxnguyen@rice.edu and swong@rice.edu, your questions instead. We will reply to you directly, and if deemed necessary, we will forward our reply to the newsgroup as well.  We will check our e-mail as often as we can to catch and respond to your questions
  4. Write your code in the most object-oriented way possible, that is with the fewest number of control statements and no checking of the states and class types of the objects involved in any way.
  5. You have 5 hours in which to complete the regular questions on the exam and 1 additional hour to complete the extra-credit question.
  6. Submission instruction: you must submit your exam in two ways.
    •  a hard copy with all answers in typewritten form, except perhaps diagrams (if any) which can be hand-drawn.  Be sure to print your name and sign the honor pledge.  You do not need to print out the supplied code unless you have modified it.
    • Zip your work together with your honor pledge (included in the provided code) and upload to the usual WEBCT upload page under "Exam3". 
  7. The deadlines for submission is:
    • for graduating seniors: Thursday, May 4, 2006 at noon
    • for non-graduating students: Wed. May 10, 2006 @ 5:00 PM.

Refresh this page often!

Check the time stamp at the bottom of the page to be sure that you have the latest version with any typographical corrections!

Pledge of Honor

 

 1.  15 pts    2. 15 pts   3.a  10 pts    3.b  5 pts   3.c 15 pts  4.a  15  pts    4.b  10  pts   4.c  15  pts 4.d  15  pts
extra
 Total: 100 pts   
 +  15 pts extra 
                   

PRINT THIS PAGE AND ALL YOUR CODE, WRITE AND SIGN YOUR HONOR PLEDGE AND SLIP UNDER DR. WONG'S DOOR (DH3102) ON THE DUE DATE.

You must supply both the electronic and paper copies of your exam in order to for you exam to be graded!


In general, since we are ignoring the "generics" features of Java, DrJava will occasionally complain with an "unchecked type warning" (or something similar).   Ignore these warnings -- they can be permanently suppressed by un-checking the appropriate box under "Preferences/Compiler Options" in DrJava.)


Problem 1: BiTree Equals (15 pts total)

Since you are such experts now at writing algorithms on lists to determine "deep" equality (recursive equality that checks the entire list), let's move on to the same function on binary trees.

Write an IVisitor algorithm called BRSEquals that will take another BiTree as its input and return true if all the corresponding data elements in the host and input trees are equal (as determined by their equals method) and returns false otherwise. 

Problem 2:  Array Equals (15 pts total)

Now you're on a roll!  So let's move on to array equality.   There's a lot lost in going to arrays, namely polymorphism and intelligence.   Arrays cannot support visitors, so we have to write a separate utility class that takes in the arrays to be processed as input parameters.  Arrays have no internal sense of being empty or non-empty and not recursive data structures, so there things to be checked that in lists and trees are automatically taken care of by polymorphism. 

Write a class called ArrayUtils with a method called isEqual that takes in two arrays of Objects (i.e. type Object[]) and returns true if all the corresponding elements in the arrays are equal (as determined by their equals method).  false should be returned if any corresponding elements are not equal or the arrays are of different length.

Problem 3 - Binary Search Tree (30 pts total)

The following is a proposed algorithm (by a student) to delete the root of a binary search tree of type BiTree.

3.a. (10 pts)  Write a BiTree visitor called RemRootBST to remove the root of a binary search tree as described by the above algorithm.  The stub code in in the package brs.visitor of the provided project (prob3).

3.b. (5 pts) Compare the algorithm in 3.a against BSTDeleter, the algorithm to remove an element from a binary search tree given in lecture 36: http://www.owlnet.rice.edu/~comp201/06-spring/lectures/lec36/.  Discuss the pros and cons.  Write your answer as comments at the top of the visitor code.

3.c (15 pts)  Consider a binary search tree, specifically, the implementation presented in lecture using class BiTree. Write a BiTreevisitor named BSTFindNext that returns the BiTree object that is the next largest object relative to the visitor's input. The visitor's input may or may not be in the binary search tree. If there is no next larger element, return an empty BiTree.  The stub code in in the package brs.visitor.

Examples: Consider the following binary search tree containing Integer.

    57
   /  \
 33    78
      /  \
    63    91
   /  \
 59    71
      /
    70
                     

Let BiTree bst be the above binary search tree and bstfindnext be an instance of BSTFindNext.

 

Problem 4 N-ary Trees (40 pts + 15 pts extra)

A tree structure that allows for an arbitrary number of subtrees is called an n-ary tree.  Such a tree can be represented by what is called the "leftmost child - right sibling" structure as described by the following.

The code for the above tree model is in the package tree of the supplied code (prob4).

Examples: Using horizontal lines to denote right sibling links and (slanted) vertical lines to denote left-most child links, and the letter ‘e’ to denote an empty n-ary tree, the following illustrates various n-ary trees and their corresponding leftmost child - right sibling representations.  For simplicity, the empty binary trees at the end of leaf nodes on the binary tree have been omitted.

n-ary Tree Leftmost Child - Right Sibling Java Code
empty tree         e  
Tree t = new Tree();
 
	57
 
    57 -- e
    /
   e
Tree t = new Tree();
t.insertRoot(57);
 57    or     57      or     57
 |           /  \           /  \
 33         33   e         e   33
       57 -- e
     /
   33 -- e
  /   
 e    
Tree t = new Tree();
t.insertRoot(57);
Tree lmc = new Tree();
lmc.insertRoot(33);
t.set_leftMostChild(lmc);
    57
  /   \
 33   78
      57 -- e
     /
   33 -- 78 -- e
  /      /
 e      e
Tree t = new Tree();
t.insertRoot(57);
Tree lmc = t.get_leftMostChild();
lmc.insertRoot(33);
lmc.get_rightSibling().insertRoot(78);
 
      57
   /   |   \
 33   78   12
    /    \
   63     19
  /  \   /  \
 42  e  e    6
      57 -- e
     /
   33 -- 78 -- 12 -- e
  /     /
 e    63 ----- 19 -- e
     /         /
   42 -- e    6 -- e
  /          /
 e          e

 

Tree t = new Tree();
t.insertRoot(57);
Tree lmc = t.get_leftMostChild();
lmc.insertRoot(33);
lmc.get_rightSibling().insertRoot(78);
// etc...
 
     

 

4.a. (15 pts)   Write an ITreeVis algorithm called TreeEqual that will take another Tree as its input and return true if all the corresponding data elements in the host and input trees are equal (as determined by their equals method) and returns false otherwise.  The stub code for TreeEqual is in the package tree.visitorHint: this algorithm is very similar to the algorithm in problem 1.  You may need to write anonymous helper visitors here.

 

4.b. (10 pts)   Write a JUnit test class called TestTreeEqual that tests the visitor TreeEqual in 3.a.  The test code must cover at least the following cases for the host:

You are to figure out what the various input Trees should be to thoroughly test the visitor.  The stub code for  TestTreeEqual  is in package tree.visitor.test.

 

4.c. (15 pts)  Like the case of the binary tree structure BiTree, we allow removal of the root only when the leftmost child is empty or when the right sibling is empty.  In the empty leftmost child case, when we remove the root node, the tree node is replaced by the node of its right sibling.  In other words, the tree mutates to become its original right sibling.  In the empty right sibling case, when we remove the root node, the tree node is replaced by its leftmost child.  In other words, the tree mutates to become its original leftmost child.  The root node cannot be removed when both leftmost child and right sibling are non-empty.  A IllegalStateException should be thrown in this case.  Below are some examples to illustrate the root removal specification.

Tree t Resulting t after t.removeRoot()
  empty
NoSuchElementException thrown
 
     57 -- e
    /
  e
  empty
   33 -- 78 -- 12 -- e
  /     /
 e    63 ----- 19 -- e
     /         /
   42 -- e    6 -- e
  /          /
 e          e

 

         78 -- 12 -- e
        /
      63 ----- 19 -- e
     /         /
   42 -- e    6 -- e
  /          /
 e          e
      57 -- e
     /
   33 -- 78 -- 12 -- e
  /     /
 e    63 ----- 19 -- e
     /         /
   42 -- e    6 -- e
  /          /
 e          e
   33 -- 78 -- 12 -- e
  /     /
 e    63 ----- 19 -- e
     /         /
   42 -- e    6 -- e
  /          /
 e          e
         78 -- 12 -- e
        /
      63 ----- 19 -- e
     /         /
   42 -- e    6 -- e
  /          /
 e          e
IllegalStateException thrown
   

Complete the code for the method removeRoot() in NonEmpty.java to implement the above specification.

 

4.d. (15 pts extra)  Write a BiTree visitor called MakeTree that builds and returns an equivalent n-ary Tree for the BiTree host.  For examples,

BiTree host Equivalent Tree
  empty   empty
 
    57
  /   \
 e     e
    57 -- e
    /
   e
    57
  /   \
 33   e
       57 -- e
     /
   33 -- e
  /   
 e    
    57
  /   \
 e     78
       57 -- e
     /
  78  -- e
  /   
 e    
    57
  /   \
 33   78
      57 -- e
     /
   33 -- 78 -- e
  /      /
 e      e
      57
   /     \   
 33       78   
        /    \
      63     19
     /  \   /  \
    42  e  e    6
      57 -- e
     /
   33 -- 78 --- e
  /     /
 e    63 ----- 19 -- e
     /         /
   42 -- e    6 -- e
  /          /
 e          e
           57
         /     \   
       33       78   
     /    \
    89     23
   /  \   /  \
 18  e  e    99
          57 -- e
         /
        33 ---------------- 78 --- e
       /                    /
      89 ----- 23 -- e     e
     /         /
   18 -- e    99 -- e
  /          /
 e          e

The stub code for MakeTree is in the package brs.visitor.  Feel free to write additional helper visitors if needed.

Hints (compare these hints with the above diagrams):

 


Last Revised Thursday, 03-Jun-2010 09:50:18 CDT

©2006 Stephen Wong and Dung Nguyen