CMSC 341 Project 2
|Assigned||March 6, 2013|
- To use a binary search tree (BST) in a real application.
- To learn how to augment an existing data structure.
- To write non-trivial recursive code.
- To practice reading and modifying code written by someone else.
Often times a data structure supports almost all of the operations we need. When that's not the case, we can often augment the data strucutre by adding a data member or two and an additional operation or two.
In this project, you will augment the author's Binary Search Tree (BST) code to support these new operations. Method names below are merely suggestions.
AnyType nthElement(int n)-- returns the n-th element (starting from 1) of the in-order traversal of the BST.
int rank( AnyType x )-- returns the "rank" of x. The rank of an element is its position (starting with 1) in an in-order traversal.
AnyType median( )-- returns the median (middle) element in the BST. If the BST contains an even number of elements, returns the smaller of the two medians.
boolean isPerfect( )-- returns true if the BST is a perfect binary tree.
boolean isComplete( )-- returns true if the BST is a complete binary tree. This method is for extra credit.
String toString( int nrLevels )-- generates the level-order output described in the sample output below.
The command line for this project has two command line arguments. The first argument is the name of a data file which contains integers separated by whitespace. The second argument is the name of a command file.
Your program will read the integers from the data file and insert them into the BST. It will then read the command file and perform the requested operations and produce the specified output. The data file may contain duplicates. If so, do not insert the duplicate.
The Command File
Each line of the command file represents a single command. Blank lines may occur anywhere in the file and should be ignored. Lines that begin with the pound ('#') symbol are comments and should also be ignored. You may assume that valid commands are well-formed, but not all commands are valid. If you encounter an invalid command you should ignore that line in the file, print an appropriate error and continue to the next command. An appropriate message should be printed for any valid command that produces no results (i.e. finding the rank of a non-existent integer). The following commands will be found in the command file. Note that there is an opportunity for extra credit.
PRINT <number of levels>
Prints the specified number of levels of the BST according to a level-order traversal. See the sample output below for required formatting and data.
- RANK N
Prints the rank of the integer N. In the tree used for the sample output the rank of 30 is 4 and the rank of 70 is 18. If N is not found in the tree, print an appropriate message.
- NTH N
Prints the N-th integer as if an in-order traversal were performed. If there are less than N integers in the tree, an appropriate error message should be produced.
In the tree used for the sample output the command NTH 6 prints the value 37.
Prints the median value in the tree. In the tree used for the sample output the median value is 47.
- REMOVE N
Removes the specified integer, N, from the tree. If N is not in the tree, print an appropriate error message.
- PERFECT This command prints an appropriate message indicating whether or not your BST is a perfect binary tree.
This command is for extra credit. If implemented, this command prints an appropriate message indicating whether or not your BST is a complete binary tree. If not implemented, output a message to that effect.
# test the RANK funciton
#find the 33rd element
#is the tree perfect or complete?
#end of command file
- Checkout your Project 2 repository which will contain build.xml.
- Copy the /afs/umbc.edu/users/n/i/nicholas/pub/cs341s13/Proj2/BinarySearchTree.java to your local directory.
- Create your directory hierarchy suitable for Java and CVS.
- Augment the BinaryNode class by adding an integer to store the tree's size (including the root).
- Modify the author's BST code as necessary to maintain the tree's size.
- Write new BST methods to support the new operations.
- Write main( ) and helper functions in a separate file.
- Edit build.xml so that the usual ant commands run successfully.
Other Requirements, Notes and Hints
These items cover those topics not addressed elsewhere in the project description. (R) indicates a requirement, (H) indicates a hint, and (N) indicates a note.
- (R) Although we are only using the BST for integers, the BST must remain a generic class.
- (R) Duplicate values are not allowed. Attempts to insert a duplicate value should be ignored.
- (R) Attempts to remove non-existent values should be ignored.
- (R) From a coding perspective, the easiest way to avoid duplicate insertions or attempting to remove non-existent elements is to first call find( ). However, this technique is NOT permitted for two reasons.
Calling find( ) before each insert/remove doubles the running time for these operations and is therefore inefficient.
Recusively coding the insert/remove methods to handle these situations is a great learning experience.
- (R) Each command (and its arguments, if any) read from the file must be echoed as part of the output.
- (R) The level order print (PRINT command) outputs the value of each node, the size of the tree rooted at that node, and the value of the node's parent in that order, inside parenthesis, separated by commas. (node value, tree size, parent value). Since the root has no parent, print NULL for the root's parent value. For readability, separate the levels with a blank line and print no more than 6 node/size/parent values per line. If a level requires multiple lines, use consecutive lines (without a blank line between them).
- (R) Efficieny counts! Use the most efficient algorithm possible In particular, don't make multiple searches in the tree when one will do. (Yes, we know it doesn't matter from an asymptotic analysis point of view.)
- (H) Almost all of these operations should be written recursively, but not necessarily all of them.
- (N) Be sure to adhere to the defintions of "perfect binary tree" and "complete binary tree" as used in the text and the course notes. Other material on BSTs may use slightly different terminology.
- (N) As is customary, your BST class should not provide any methods for printing. However, the BST can provide a method that formats a string containing information that the user wishes to print. Overloading the toString( ) method is a common technique to achieve this kind of functionality.
- (N) The build.xml file provided with this project is new. It should allow you put your data files in your project directory just as Eclipse does (after you've editted it for this project of course).
This output is provided to show the format of the level-order print (5 levels).
This is the level-order print of the tree below.
(55, 21, NULL)
(40, 12, 55) (60, 8, 55)
(30, 6, 40) (45, 5, 40) (70, 7, 60)
(20, 3, 30) (35, 2, 30) (44, 2, 45) (47, 2, 45) (66, 3, 70) (80, 3, 70)
(3, 1, 20) (26, 1, 20) (37, 1, 35) (43, 1, 44) (48, 1, 47) (65, 1, 66)
(68, 1, 66) (77, 1, 80) (90, 1 ,80)
For 10 points of extra credit implement a method to determine if your binary search tree is a complete binary tree. Your program should output an appropriate message in response to the COMPLETE command in the command file. If you choose not to implement the extra credit, output a message to that effect when the COMPLETE command is read.
Project grading is described in the Project Policy handout. Please re-read the Project Policy handout for further details on honesty in doing projects for this course.
See the course policy on late projects.
Project functionality testing will fall into the following categories. The list below does not necessarily include all test possibilities.
- Basic Tests
A small tree with a odd number of integers inserted in random order with no duplciates and no deletion.
A small tree with an even number of integers inserted in random order with no duplicates and no deletions.
- More Complex Tests
A tree created by random insertions and deletions.
Attempting to insert duplicate values.
Attempting to remove non-existent values.
A tree created from integers inserted in sorted order with or without deletions.
- Atypical Tests
A tree with a single integer.
An empty tree.
One or more files which do not exist or cannot be opened.
A command file with no valid commands.
- Strees Tests
A very large tree built from a large number of random insertions.
A very large tree built from a large number of random insertions and deletions.
Project code will be evaluated for the following
- Appropriate OO design and implementation.
- Appropriate use of recursion.
- Approrpiate decomposition of the main class into methods.
Files To Be Submitted
Submit any files which you have written or modified -- source code and build.xml
Be sure to verify your projec submittal by using the cvs -n update command or the CVS utilities provided for that purpose. The repository location for this project is: