CSCI 311 -- Program One - Spring 2009
AVL Trees Plus
Dr. Melody Stapleton
PLEASE
NOTE: SINCE YOUR TA HAD ALREADY WRITTEN HIS SOLUTION AND GENERATED TEST CASES WITH
A STANDING FOR “INSERT” AND “I” STANDING FOR INORDER TRAVERSAL, I AM SWITCHING
MY PROGRAM INPUT FILE CODE TO MATCH HIS.
SORRY FOR ANY INCONVENIENCE, BUT THIS SHOULD BE A SIMPLE CHANGE IN YOUR
CODE!
You are to implement the operations of an AVL tree using C++ classes. You are to structure your main program to accept and interpret file input with the following format. Listed below are the possibilities for the different categories of commands:
A INTEGER_KEY for Insert the integer to the AVL tree and do any necessary rebalancing
D INTEGER_KEY for Delete the integer from the AVL tree and do any necessary rebalancing
S INTEGER_KEY for Search the AVL tree for the integer key and for a successful search, return the pointer to the node in the tree
where the key is stored. For unsuccessful search, return a NULL pointer
P for Preorder Traversal
I for Inorder Traversal
R for Postorder Traversal
B for a report that gives the number of subtrees that are left heavy, right heavy and balanced (see more explanation below). You will also print out within this report the current cumulative statistic for deletions, as described below.
Special
criteria: The
commands may come in any order. Although insertions to the tree are
relatively straightforward, deletions of parent nodes that have two children
may result in ambiguities. You are to write two different versions of a
portion of your deletion function, each version reflecting a different approach
to deletion and you are going to gather statistics to measure which version is
more effective at balancing the tree with the least amount of “effort”.
Deletion algorithm
cases:
1.
If a parent node having two children is deleted, it is to be replaced
by the deeper (as in depth in the tree) of the inorder
predecessor versus the inorder successor. If
the inorder predecessor and the successor are the
same depth, use the inorder predecessor.
2. If a parent node having two children is deleted, it is to be replaced by the more shallow (as in depth in the tree) of the inorder predecessor versus the inorder successor. If the inorder predecessor and the successor are the same depth, use the inorder predecessor.
With each deletion
within each version you will collect the statistic for the number of rotations
that were necessary to put the tree back into balanced form as an AVL
tree. You will accumulate this statistic
over the entire run of the program with that version of deletion with each run
against an input file.
For the command "B" in the input file, you will print out a report. In this report you will consider each node in the tree. For each node in the tree you will assess whether the the subtree rooted at that node is left heavy, right heavy, or balanced. Each time the command "B" is issued you will generate a report of how many subtrees total in the current tree are left heavy, right heavy or balanced. Your instructor will provide numerous examples.
Send your output for this program to a file. For inserts it is an error to insert a duplicate value into the tree, be sure to notify the user of such an attempt. Tell the user if an operation is successful, as well. Be sure to give appropriate 'error' messages back to the user of your program. For example, if asked to Delete a value from the tree that was not found in the tree, you should reply that the value was not found in the tree and therefore, not deleted. For the Search operation, if the item that is being sought is found, print out a message that says you found it and what its value is. If the item is not found, print out a message to that effect. Of course, output the results of a traversal to this file, as well.
A good strategy for developing your program: Write pseudocode for your program that includes:
Take this link for a sample input file to run your program against. Your TA will provide final input files which are test cases to run your program against. Note: Your program should be *bullet-proof*, debugged and able to run correctly against *any file*!