Binary Search Tree (tree)
CS 162 - Spring 2008


Due: Wednesday, April 30 at 10pm
Worth: 100 points
Goals
- to use the binary tree node class - BTnode<E>
- to build a binary search tree class
- to gain more recursion experience

Overview
You MUST use binary trees as developed in class for this assignment. The approach differs from the one in the text, but refer to the text for examples and a related explanation. If there is a conflict, what we've done in class is right. As always, ask for help if you are confused.

For this assignment, you will write 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
Your program will obtain two lines of input from the user. Use the first line to create a sorted binary search tree of characters. When the characters are all in the tree, print the tree sideways so that we can see where the nodes are. Also print the characters in sorted order by traversing the tree in inorder fashion with all the characters on one line. Then use 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, print the remaining characters in the tree as above. Label your output appropriately. Do not put spaces or commas between tree elements when doing an inorder traversal. 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 binarytree directory from the link162 directory. That directory contains the btree package as well as a start to this program in BStree.java. There are some examples that might come in handy. The btree package contains BTnode.java and Btree.java which contain the BTnode<E> class and binary tree methods.

The author discusses the insert and remove methods for binary search trees starting on page 488. 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. 489 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:
  1. Every element in n's left subtree is less than or equal to the element in node n.
  2. Every element in n's right subtree is greater than the element in node n.
The tree has a header node containing null as its element. By the above rules, the tree's contents will be stored in the right subtree of that header node.

Solving the Problem

Hints for the methods - their implementations must be recursive.
All connections are made from above. Draw pictures to help you understand.