Due: Thursday, May 2nd at 11:59pm Goals - to use the binary tree node class - BTnode<E> - to build a binary search tree class - to gain more recursion experience Overview
For this assignment, you will write the insert and remove method for a program that builds a binary search tree of characters. It will read in two lines of characters. It will put the first line in a binary search tree and then print the tree's contents both sideways graphically and in inorder fashion. It will then read in the second line of input removing the characters of the second line from the binary search tree (if they are there). Finally, it will print the modified tree's contents both sideways and in inorder fashion. In both cases, there may be duplicate characters in the lines. This is okay. Store all copies of a character in separate nodes. If there are three copies of a character in the first line, and two are removed because they are in the second line, one copy remains in the tree. |
Example Input (Bold) and Output
Enter a line of input to insert: marauder Tree after input. u r r m e d ~ a a Inorder traversal: aademrru Enter a line of input to remove: rummage Tree after trimming. r d a Inorder traversal: adr |
Specification of the Overall Program
You are writing the insert and remove methods plus two supporting methods for remove.
The program obtains two lines of input from the user.
It uses the first line to create a sorted binary search tree of characters.
When the characters are all in the tree, it prints the tree sideways
so that we can see where the nodes are.
It also print the characters in sorted order on one line by traversing the tree in inorder fashion.
It then uses the second line of input by deleting the characters
in the second line from the tree.
When all the characters from the second
line have been deleted from the tree, The remaining tree is printed.
Note that spaces and tabs are character inputs that will be put into the tree although you don't see them printed.
They are allowable input, but beware having them in your test cases
because you'll have difficulty seeing them in the sideways tree.
You must use the BTnode<E> class we provide. You must implement the insert and remove methods recursively.
Steps to Start
To create a directory for your tree lab,
copy the ~rogers162/labs/BinaryTree
directory into your project.
That directory contains the btree package as well as a start
to this program in BStree.java.
The btree package contains BTnode.java and Btree.java
which contain the BTnode<E> class and binary tree methods.
The TreeLines.java file contains the main method that reads the two lines, asks to insert and remove, and prints.
The author discusses the insert and remove methods for binary search trees starting on page 505. His discussion of insert doesn't use recursion although our lectures do. The textbook might be helpful, but it is easier to write these recursively. To get full credit on this assignment, you MUST write them recursively.
Binary Search Tree Storage Rules
(p. 499 of text plus our header node constraint) |
---|
In a binary search tree, the entries of the nodes can be compared
with a total order semantics.
These two rules are followed for every node n:
|
Solving the Problem
Your code should be appropriately commented and indented. Remember that each method should have a good one-line comment. What we are giving you has comments. Be sure your name is at the top after mine. Compile again immediately before submitting your code to be sure that you didn't accidentally type something the compiler doesn't recognize. We will be accepting only the BStree.java file.
Hints for the methods - their implementations must be recursive.
All connections are made from above. Draw pictures to help you understand.
Submit
When you are satisfied that your program works as expected,
submit your directory as BStree.
Only your BStree.java files will be submitted.