Comp201: Principles of Object-Oriented Programming I
Spring 2007 -- 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 Dr. Wong .
  3. Do not post your questions on the newsgroup. Contact Dr. Wong (swong@rice.edu) to let him know when you are taking your exam so that you can set up an IM session to get your questions answered.
  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 6 hours in which to complete the exam from the time that you commence.
  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 Owlspace upload page under "Exam3". 
  7. The deadlines for submission is:
    • for graduating seniors: Wed, May 3, 2007 @ noon
    • for non-graduating students: Mon., May 7, 2006 @ noon.

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. a 15 pts    1.b.i 15 pts   1.b.ii  20pts    2.a.i  10 pts   2.a.ii 15 pts  2.b  15  pts    3. 10 pts  Total: 100 pts     
               

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 your 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.)

The problems are not in order of difficulty, so if you get stuck on a problem, move to the next one!!


Problem 1: B-Trees  (50 pts total)

An interesting variation of a binary search tree is the "B-tree", which looks like an n-ary tree ("n" children trees) but with multiple data elements at each node.   In the diagram below (may show up better in Internet Explorer), we see a root node with 3 data elements, which has 4 child trees.  The child trees have various numbers of data elements in their root nodes with corresponding numbers of child trees below them (not shown).

B-trees are very commonly used to store data in databases and file systems.  For instance, the "sectors" one hears about in the formatting of hard disks are actually the data arrays of B-trees.

We will be implementing an immutable B-Tree.

To implement a non-empty B-Tree, we will first start off with a collection of interrelating interfaces to describe

(Note: in the following diagram, the vararg nature of the input parameters for the visitor and execute method is not shown.)

In the supplied code, you will find an implementation of IBTreeFac called BTreeFac that implements INEBTree by using one array to hold the data elements and another to hold the references to the children trees.   You will also find a sample visitor, ToString, that returns a String representation of a B-tree that is very similar to the vertical tree representation we used in class for a binary tree.

Things to note about the supplied code:

Prob. 1.a:  (15 pts) Counting the elements in a B-Tree

To start off, let's try a simple algorithm to count the number of data elements in a B-Tree.   To do so, you will complete the skeleton code for the CountElements visitor.

Algorithm description:

Think simple!   Don't make this algorithm harder than it is!

The unit test code has been provided.

 

Prob. 1.b: Search Tree Criteria for B-Trees

Continuing along with the analogy with a binary search tree, we can define a search tree criteria for B-trees:

For any data element at index i, then all the elements in the entire child tree at index i are less than the data element value and all the elements in the entire child tree at index i+1 are greater than the data element value.

That is,

{all elements in children[i]} < data[i] < {all elements in children[i+1]}

In the above B-Tree diagram, we would thus get:

{all of D-E} < A < {all of F} < B < {all of G-H-I} < C < {all of J}

So we see why it is useful to think of the child trees as being "sandwiched" between the data elements (or the data elements being sandwiched between the child trees, if you prefer).

There is also one more criteria that we now need to explicitly impose because it was implicit for the binary search tree:

The number of data elements in any given node of the tree is limited to a maximum value, called the "order" of the tree.

For a binary search tree, the order was implicitly set to 1.   In the above example, we could surmise that the order is 3.   Note that this implicitly limits the number of children to order+1.

(Note: in the literature, you will often see the order of a B-Tree defined as the maximum number of children, not the maximum number of data elements at a node.  While mathematically equivalent definitions, plus or minus one, we find it much more conceptually useful to think of the limit on the data elements because order = 0 is the empty tree in that case.)

Thus, an empty B-tree satisfies the search tree criteria.

We will now proceed to develop an algorithm, called InsertSearchTree, to insert data elements into a B-tree search tree that preserves the search tree criteria.

 

Prob. 1.b.i:  (15 pts) Utility method to calculate the insertion index.

We always want to break our development process down into as small and isolated chunks as possible.   So, let's concentrate initially on a simple method to calculate the index of the insertion point of a new data element with regards to a given data array.  

A returned  insertion index of 0 means that the new data element is smaller than any existing elements in the data array.  An insertion index value of 1 means that the new data is larger than the first element but smaller than the second.  A  insertion index value of  2 means that it is larger than the second element but smaller than the third and so on.   A returned insertion index of the data array length means that the new data element is larger than any element in the data array.

You are to complete the getInsertIndex method in the InsertSearchTree class.

The InsertSearchTree class utilizes a Comparator object just like the binary search tree to determine the ordering of any given pair of data elements.

 

The unit test code is provided.

See note at top of exam if you get "Uncheck type warnings".

Prob. 1.b.ii: (20 pts) The rest of the Search Tree Insertion Algorithm

Now we can complete the search tree insertion algorithm.   You are to complete the mtCase and neCase methods of the InsertSearchTree class.

The insertion index value can be thought of as either the index where the new data element is inserted into the existing data array or  is the index of the child tree that would hold the new data.

Since the number of data elements at any given node can vary from nothing (empty tree) to the order of the search tree, when we insert a new value, we will either

 

The InsertSearchTree visitor is executed along with a single input parameter, the new data element.    The order, comparator, and IBTree factory are passed to its constructor.

Algorithm description:

mtCase:

  1. Use the saved IBTreeFactory to instantiate a new INEBTree that has exactly one data element, params[0], and whose children are all empty.

neCase:

 

Hints:

 

Teaser: Next semester we will see a much fancier, more powerful and  mutable B-tree implementation whose search tree insertion code is not much longer than the above code and which will simultaneously balance the tree for optimal performance and even repair the tree if it is damaged!


Problem 2:  A Fancier RAC and TAMU (40 pts total)

Prob. 2.a:  RAC's with both insertion and retrieval policies.

The Restricted Access Containers (RAC's) discussed in class were slightly limited because they were based on two assumptions:

  1. The priorities or retrieval order of the stored elements was static and did not change during the time that the elements were in the RAC.
  2. The relative insertion and retrieval speeds were irrelevant so the system was written for slow insertion but fast retrieval.

But neither of the two above assumptions are always true, so in order to address these shortcomings, we need to modify our RAC implementation. 

A simple way to fix the problem is to use both insertion and retrieval strategies (policies).   Looking specifically at our LRStruct-based implementation, this means supplying an IAlgo visitor to determine where in the internal storage list, a newly inserted ("put") element is saved as well as another IAlgo visitor to retrieve ("get") the desired element from the list.

See note at top of exam if you get "Uncheck type warnings".

Prob. 2.a.i.   (10 pts) Modify ALRSRACFactory.LRSRAContainer

Modify the e ALRSRACFactory.LRSRAContainer class (inside the ALRSRACFactory.java file) so that it's get() and peek() methods use the _retrieveStrategy to obtain the desired value from the _lrs list.    (Replace the original get() and peek() code that has been provided for your reference.)

 

Prob. 2.a.ii.   (15 pts) A new, fast insertion queue

Create a new implementation of a queue, called LRSQueue2Factory, that unlike the existing queue implementation, has a fast, order 1, O(1), insertion but a slow, order N, O(N), retrieval.

 

Prob. 2.b: (15 pts) Retrieving dynamically changing entities from a RAC

Welcome to the Texas Asylum for the Mentally Unstable  (“TAMU”)!   As the director of the Asylum, you have the authority to admit “patients” (whom we call “students” so as not to alarm them).   Of course, when they enter the Asylum, they are all “insane” (read: freshmen).    But luckily, due to the vagaries of a government-directed education, they can unexpectedly become “sane” (note the quotation marks) at any time.   In accordance to the Charter of the Asylum, you can only release “sane” patients (read: transfer students).     Not only a respected and learned psychologist, you are also an accomplished computer scientist.   This superior training will enable you to devise a computer model and implementation that will accomplish this goal.

Click here to see a demo of TAMU in action!

You decide, oh so wisely, to go with a restricted access container—a computerized padded cell as it were.  This will enable you to “store” your patients until they become “sane”.   However, you need to write insertion and retrieval policies that will enable you to “store” a new patient ("sane" or "insane") and then to “retrieve” a “sane” patient.   You will only retrieve and thus release one “sane” patient at a time.   The problem is that since patients arbitrarily vacillate between sane and insane, you cannot determine at the time of their admission when they can be discharged.

Notes on the Patient class:

Notes on the supplied code and demo:

You are to complete the code for TAMUFactory to implement the above described behavior for inserting and retrieving a patient from the RAC by writing the insertion and retrieval policy visitors. 

Insertion visitor:   Inserts a new patient into the list.

Retrieval visitor:   Returns an LRStruct whose first has the desired patient.

Hints:

 


Problem 3: Mapping a Binary Tree (10 pts)

Returning back to our original BiTree implementation of a binary tree, let's complete the writing of an IVisitor called MapBRS that implements the higher-order function "Map" over the binary tree.   But we'll have a little twist, of course: This map will take an arbitrary number of ILambda objects that it will apply to the root tree and every sub-tree.

Algorithm specifications:

Notes:

 The unit test code is supplied.


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

©2007 Stephen Wong and Dung Nguyen