Binary Search Tree 

Due Time/Date: Monday July 9th., 2000 at 10:00 a.m. uhunix time. In the afternoon you can enjoy the fireworks !!! 
What to do: Write three classes: BSTree.java,TNode.java, and  Person.java .
How to turn in your homework?

Send e-mail to ics211-homework@hawaii.edu. attaching the three JAVA files. If you created any extra classes for this assignment, you must submit them as well. All these classes should be attached to your e-mail. The subject of your e-mail must be "Binary Search Tree".

The body of the e-mail is optional and since this is not an applet you don't need to add a link from your web page.

Please make sure that your program compiles/runs properly in uhunix.

Comments and ADT specs. Please comment all your classes and methods with pre-condition, post-condition and a brief description of what each method does. Remember that nothing is obvious.

Program requirements and structure

Your tree should store Person-objects into a binary search tree. In order to do this you should put the Person-object into a TNode and add the TNode to the BSTree object.  PLEASE make sure that your methods have the required parameters and also make sure that they return the required values. Remember that this project will be graded using code that Rui and I will write and we are NOT modifying our code to suit yours. Please follow instructions carefully.


The back end of this implementation is up to your design. However you must have the three specified classes:

This will be a binary search tree formed with nodes that contain person-objects.


Required Methods in your classes :

Person.java

Method

Conditions

public Person (String Name; int key) // constructor
  • the Name must be a non-empty String
  • The key must be an integer number between 1 and 200
  • Make sure that the parameters are in the order specified.
public void setName( ) This method will change the name of a person
public String getName( ) This method will return the name of a person
public int getKey ( ) This method will return the int value of a person's key.

Note: There is no setKey method. We don't want anybody to change the key of an element that is already part of the binary-search-tree, this will make it inconsistent.

TNode.java

Method

Conditions

public TNode( TNode lChild, Person p, TNode rChild) 
  • Person should be a non-null object. Please refer to the Person constructor.
  • lChild and rChild are TNodes. They may or not be null depending on your code implementation.
  • Make sure that the parameters are in the order specified.
public Person getPerson( ) This method will return a Person object from inside a given node
public TNode getLChild( ) This method will return the left child of a given node
public TNode getRChild( ) This method will return the right child of a given node
public void setLChild( TNode newLChild) This method will set newLNode as the left child of a node
public void setRChild( TNode newRChild) This method will set newRNode as the right child of a node

Note: There is no setPerson method. We don't want anybody to change the person out of a node  that is already part of the binary-search-tree, this will make it inconsistent. 

BSTree.java

public BSTree( ) Empty constructor, MUST be there
public String  insertTNode( String name, String skey)
  • This method will take the two arguments and create a Person-object. 
  • If the String  name is empty or if the key is not an int number in the range, the method will return an error message.
  • If the parameters are okay the method will return an empty String.
  • Here is where you will validate/filter  the information that the Person-object constructor method will receive. If this method detects any errors then the constructor for the Person-object and the constructor for the TNode object shouldn't be called.
public boolean findPerson(int key, string name)
  • This method will look for a person with the key given by the parameter.  
  • If the person is found the method will return "true". 
  • If the person is not found it will return false.
  • The string name will be used in case that the key is duplicated. This will be the way to find the person.
public void printBSTree(TNode root) public void printBSTree(TNode root){
      if(root != null){
       System.out.println("  " + root.getPerson( ).getName( ) + "  " +root.getPerson( ).getKey( ));
           printBSTree(root.getLChild());
           printBSTree(root.getRChild());
      }
}

This method assumes to get the root as a parameter and will help you roint your tree in preorder traversal.

public TNode getRoot( ) This method will return the root of the BSTree. This will be used so that we can call the printBSTree method recursively.

You must copy and paste the printBSTree method into your BSTree class.

Note that there is no nodeDelete method in the BSTree class. Don't worry about it, we haven't yet seen this procedure in class.

ADD the following method:
public TNode getRoot( )
This method returns the root of the BSTree. This will help to activete the printBSTree method.

You may create your own test classes and your own test class. Rui and I will have our own. Remember to follow instructions carefully and to place your parameters in the specified order.


      Why  do this assignment?

You will practice/learn:


If you have any more questions please e-mail blanca@hawaii.edu