Due Tuesday, November 9, 11:59 P.M.
Reading: Chapters 8, pages 317- 333 and 344-360, and chapter 10, pages 429 - 470
Please keep in mind the policy on collaboration.
The database will contain records which hold the following:
Each of these will be a character string. The records will be ordered by primary and secondary keys as in the phone book, i.e., to compare two records A and B to determine their relative order, you would first compare A.lastname and B.lastname using, say, strcmp() from the string library. If these names are not identical, then the ordering on the names determines the ordering on the records. If the names are identical, then you must look at the first names to try to order the records. This is just like the ordering in the phone book, except we will only use the first and second names. You will have to write a function recordCompare which takes pointers to two records and returns -1 if A < B, returns 1 if A > B, and returns 0 if the keys of the two records are equal.
When the program runs, it loads the data from a file into an S-Tree (described below). The root pointer to the tree should be private data in a class called STree, with the appropriate interface functions, as defined in the next section.
When you execute your program, it can either take its input from the terminal as you type it or you can input the commands from the datafile. In either case you should first use "add" commands to build your database and then use other commands to manipulate and print the database. Here is a sample datafile you can use. If you use this file to test out your program you should add commands at the end of the file to test the various data operations as described below. The last command should be "quit". The program accepts user commands to perform the following operations:
csa% a.out < hw4.datafile.txt Welcome to the Automated Phone Book System. Loading database from hw4.datafile.txt..... Enter one of the following commands: add <first name> <last name> <phone number> <email> - Add a new individual to the phone book remove <first name> <last name> - Remove the data for an individual print <first name> <last name> - Print out the phone number and address of an individual list - List the entire phone book in order quit - Quit the system Commands can be abbreviated by their first letter (e.g., ``a'' for ``add''). phonebook> p bill farmer // Note: the first three entries were already in the DB Name: bill farmer Phone: 8921 Email: farmer@cs phonebook> p stan sclaroff Name: stan sclaroff Phone: 8988 Email: sclaroff@cs phonebook> p glenn gould Name: glenn gould Phone: 8111 Email: ggould@cs phonebook> add wayne snyder 8925 snyder@cs Added wayne snyder. phonebook> a robin williams 534-8923 mork@universe.com Added robin williams. phonebook> a wayne snyder 3283 snyder@cs wayne snyder is already in the phone book. phonebook> a alan turing 1212 turing@heaven.com Added alan turing. phonebook> a donald knuth 9872 don@berkeley.edu Added donald knuth. phonebook> a jane snyder 9873 jsnyder@acs.bu.edu Added jane snyder. phonebook> s snyder wayne Unknown command ``s'', try again. phonebook> p snyder wayne snyder wayne is not in the phone book. phonebook> p wayne snyder Name: wayne snyder Phone: 8925 Email: snyder@cs phonebook> r stan sclaroff Removed stan sclaroff. phonebook> p stan sclaroff stan sclaroff is not in the phone book. phonebook> list ==================================================== | Phone Directory | ---------------------------------------------------- | Name | Phone | Email | ==================================================== | farmer, bill | 8921 | farmer@cs | ---------------------------------------------------- | gould, glenn | 8111 | ggould@cs.bu.edu | ---------------------------------------------------- | knuth, donald | 9872 | don@berkeley.edu | ---------------------------------------------------- | smith, jane | 9873 | jsmith@acs.bu.edu | ---------------------------------------------------- | turing, alan | 1212 | turing@heaven.com | ---------------------------------------------------- | williams, robin | 8923 | mork@universe.com | ==================================================== phonebook> q Thank you. (The whole list is printed here.) Goodbye. I enjoyed serving your every whim.
The user input will come from the keyboard (standard input), and output should go to the terminal screen (standard output).
The database items will be stored in a structure which has the data fields firstname, lastname, phonenumber and email.
The nodes of the S-tree will have three fields They are a leftptr, a rightptr, and an entry field which holds a whole record. Each S-tree has a pointer ROOT which points to the root of the tree.
The basic idea of S-trees is as follows. An S-tree is a binary tree that is ordered but possibly not balanced. It has the ordering property that, for any node N and any ancestor M of N, if M has a key less than N it is in N's left subtree, and if M has a key greater than N it is in N's right subtree. (Note: In our case the keys are strings and less than mean less than in alphabetic order. )
We implement our S-trees by writing an STree class. You will need to write the class headers and the class implementation in files called hw4.tree.cpp and hw4.tree.h which will be submitted.
(i) If N is a leaf, then just remove the node, and update the pointer of N's parent to null.
(ii) N has one child. Then that child replaces N. To do this you need to change the pointer in N's parent that pointed to N to point to N's child. (And to do this you need to be able to find N's parent when you find N !)
(iii) N has two children. Find the node M with the largest key in the left subtree of N. Replace N with M. Update all the pointers that need updating.
add glenn gould 8111 ggould@cs.bu.edu add bill farmer 8921 don@berkeley.edu add stan sclaroff 8988 sclaroff@cs
If you find it helpful, feel free to add a flag like "end" in a last line at the end of the data file just top make it easier to identify when you have finished with the file. The flag would tell the program to then go to the part of the program which prints out the welcome message.
At the end, when the user types quit, the program would write out to the terminal all records currently in the S-tree, in the format just specified. You can just use the "list" command to do this.
Note that the datafile just listed is not in alphabetical order. The reason for this is that it would be very inefficient to insert the names in alphabetic order as it would result in a very unbalanced tree.
Write a make file for this program that will generate the executable ``h4'' for the program above. This will be a smaller portion of your grade for this homework, but still must be done correctly to get full credit.
You should name your make file ``h4makefile'' and submit it as part of this homework. Since the utility make normally expects the makefile to be named ``Makefile'' or ``makefile'', you will have to run the make utility with:
make -f h4makefile
Use the make file from the discussion section page as a guide for this one.
Also provide a script file hw4.script.txt showing that your program compiles and was able to handle the actions shown in the sample session above. You should list the data file before and after the run as part of the script, to show that you accurately create the proper data file.
And finally you need to submit a makefile named hw4makefile.