(Latest Revision: Tue May 14 2002) gotchas

gotchas


From john@alcyone.csustan.edu Tue May 14 19:54:11 2002
Date: Tue, 14 May 2002 19:54:10 -0700 (PDT)
From: John Sarraille 
To: cs2500@waganupa.csustan.edu
Subject: CS2500 Information

Hi CS 2500 Class --

I was talking with one of you today and I answered a question
about using the itemClass (defined in files Data.h and Data.cpp).
Let me share some sample code that may help you understand how to
use the itemClass.

Here is some of the code that might be found in the function in
your driver that takes care of the "insert" command.

      /* Declare a variable that is the data type we store in the tree */
  itemClass  item ; 

     /* Declare a string variable */
  string stringBuf ;  

     /* extract a string from the input and put it into stringBuf */
  cin >> stringBuf ; 

     /* Use the setKey method of the itemClass to put the string
        into the item variable */
  item.setKey(stringBuf) ;


     /* Now tell the tree to insert the item into the tree. */
  bool Success ;
  tree.SearchTreeInsert(item, Success) ;

-------
For technical reasons, we don't store just "a string" in the tree.
Instead we store "a box with a string inside it" in the tree.
We use the itemClass as the box that contains the string.

-------
Note that this code:

  cin >> stringBuf ; 
  item.setKey(stringBuf) ;

can be replaced with this code:

  cin >> item.theString ;

Either way works fine.  (In a previous version of this
assignment, "theString" was a private data member.  In that
situation, only the first method above was available to the
client of the itemClass.)

-------

If you look at the code in BST2.cpp you will see several
instances of code like this:

   // else search for the insertion position
   else if (NewItem.Key() < TreePtr->Item.Key())

Note that the code calls the "Key" method of the itemClass here
to get the value of the list key.  If you look at the definition
of the itemClass in Data.h and Data.cpp you will see that I did
define a method called "Key" that returns the string that is
stored in the item.  The bstClass code "expects" the "Key" method
to be there in the itemClass.  The bstClass relies on this method
to get the keys inside items whenever it needs to compare one key
with another.  This is the "technical reason" why we need to have
the itemClass with the "Key" method for this assignment, whereas
in the previous assignment we simply used strings as list
elements.

-------

Well.  This certainly has been fun.  I hope that you are as
thoroughly confused as I am now.  :-).  I'll see you on Thursday.

-- john sarraille