Homework 6 - Binary Search Trees

Due: Wednesday May 23, 2012 at midnight

The purpose of this assignment is to implement and manipulate binary search trees using linked nodes.

As with the other linked data structures, you will need two structures to implement a binary search tree: one for the tree node and one for the tree. Your tree node structure will contain member variables for the element to store ("data"), the pointer to the right subtree ("right") and the pointer to the left subtree ("left"). The binary search tree structure will contain a member variable for the root node of the tree ("root").

You will implement the following functions for your binary search tree:

Your main function will implement the following menu:
             Binary Search Tree Menu
  ===============================================
  1. Read values from a file to insert into tree
  2. Search the tree for a value
  3. Delete a value from the tree
  4. Print the values of the tree in sorted order

  0. Exit
  ===============================================
Be sure to call initTree before entering the menu loop and to call deallocate after exiting the menu loop. Points will be deducted if this is not done. Also be sure to use robust input for the menu options (e.g. clear the input stream if a user types a letter instead of one of the option integers).

Option 1 should prompt the user for an input file, read the input file and insert the values one at a time into the tree in the order given in the input file. If the file open fails, return to the menu. Don't forget to close the file after reading the input. One of the following input files will be used to test your code (you can also generate additional input files using the code file_gen.cpp):

Option 2 should prompt the user for a value, even if the tree is empty, to be sure that the function properly handles all cases. It will call search and store the resulting pointer. If the result is NULL, print out an error message about the item not being found (there should be NO such error message in your tree functions). Otherwise say that the item was found in the tree.

Option 3 should likewise always prompt the user for a value, even if the tree is empty, to be sure that the function properly handles all cases. It will call remove and see if remove returns true or false. If remove returns true, say that the value was deleted. Otherwise, say that the value was not found in the tree.

Option 4 will call inorderPrint, even if the tree is empty. This will print all the values in sorted order.

Email the completed source code to me.