(Latest Revision: Tue Nov 23 2004)
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. ------- 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.