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:
deallocateSubtree
to accomplish this.
insertSubtree
to insert the given value while preserving
the BST property. The function should return false if allocation fails
or if the given value is already in the tree. It may need to relay the
return value from the helper function to accomplish this.
insertSubtree
on that subtree. This function will return false
if the allocation fails or if the value is already in the tree. Otherwise,
it will return true. It will need to return the value of its recursive
calls to accomplish this.
searchSubtree
.
inorderSubtree
.
inorderSubtree
on the left subtree,
then printing the data in the current node, then recursively calling
inorderSubtree
on the right subtree.
search
to see if the value is in the
tree and return false if it is not. Once you have found the node containing
the value, perform a second search for the node containing the next
in-order value in the tree (the replacement value) and perform the
deletion procedure. Return true when the deletion works. Note that you
will need to do an iterative search for the replacement value so you can
update its parent node accordingly.
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):
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.