CSCI 311 -- Program One - Fall 2008
AVL Trees – Focus on Recursion and Simulation of Recursion
Dr. Melody Stapleton
Program
Due Date: Friday, October 3, 2008, Uploaded by midnight
into VISTA.
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 Add (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
Assignment criteria:
1.
The commands may come in any order. Although
additions (insertions) to the tree are relatively straightforward, deletions of
parent nodes that have two children might be handled more than one way. To
avoid this, we will have the rule that when such a parent node is deleted, it
is to be replaced by the inorder predecessor (NOT the inorder
successor). You are to use the code from
your textbook by Kruse, modifying it to remove templates. For the Insert routine you will need to write
the missing components, which include rotate_right and left_balance
2.
Your TA will provide only partial code for the
deletion function. You will write this
routine using recursion. The pseudocode for the deletion algorithm appears on pages
484-485 of your text book, and a diagram showing the cases
appears on page 486.
3.
Send your output for this program to a file. For
Inserts (Additions), 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.
4.
You are to remove the recursive calls in your Insert
algorithm by using the method described by taking this link
to the Removal of Recursion Methodology. To test the non-recursive version
of your Insert algorithm, you will replace your recursive version with your
non-recursive version, run the complete AVL code against the same input file
and compare results to ensure they execute identically. Your TA will also provide other files that
test that you are building the stack correctly when your non-recursive insert
is executing.
5.
For 10
points extra credit: Once you get all 4
items above completed, you can earn an extra 10 points on the program by
applying the same methodology you applied to the Insert routine now to the
Deletion function. That is, you will
remove the recursion from the Deletion function. Your TA will also provide files to test that
the stack is being built correctly when the non-recursive version of Delete is
called.
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/test cases to run your program against. Note: Your program should be *bullet-proof*, debugged and able to run correctly against *any file*!