Note that this program must be compiled and executed under Linux.
For this assignment, you should create a binary search tree in which each node contains a separate data member for the key and the data being stored. The key should be a string (i.e. char *) and the data should be a pointer to anything (i.e. void *).
Following are the methods your class will need:
The method should make sure neither parameter are NULL; if either are, it should do nothing but return false (to indicate an error). Also, if allocating a new node fails, false should be returned.
To compare two string keys, use the int strcmp( char *str1, char *str2 ) function. It returns a value < 0 if str1 is less than str2, a value > 0 if str1 is greater than str2, or 0 if the strings are equal.
The method should return true if the new node is created and inserted into the tree successfully.
The method should search the tree for a node with a key that matches the passed in value. If it finds a match, it should remove the node and return the data from the removed node. If no match is found, the method should return NULL.
Note that the binary search tree will not and can not know what type of data you are inserting into it. All it knows is that the keys are strings and the data is a pointer to a void *. Therefore, your program must keep track of this and do the proper typecasts. Have your main program use integers as the data being inserted into the tree.
Following is sample code showing how an integer value can be added and removed from a tree:
int *data = new int;
*data = 10;
BST.insert( "integer_data", (void *) data );
int *removed_data = (int *) BST.remove( "integer_data" );
if ( removed_data == NULL )
cout << "Tree was empty\n";
else
cout << "Removed " << *removed_data << " from the tree\n";
Your main program, however, should provide a menu of options to the user,
allowing him/her to insert, remove, etc. Note that when inserting an integer,
you will have to ask the user to enter in two pieces of information: the key
value (a string) and the data (an integer). Be sure to dynamically allocate
the integer that is passed to the insert method.
Note: In past assignments, some students have included .cpp (or .cc) files within other .cpp( or .cc) files. Do not do this. Only header (.h) files should be included in a file.
Include a makefile to compile your program.