Make your own free website on Tripod.com

import java.io.*;

public class treetester
{
	public static input in = new input();


  void insert(TreeNode root, String n)
   {
     if (n.compareTo(root.getValue()) > 0 && root.getRight() ==null)
       {TreeNode temp = new TreeNode(n,null,null);
        root.setRight (temp);

        return;
	    }
	  else
	     if (n.compareTo(root.getValue()) < 0 && root.getLeft() == null)
		       {TreeNode temp = new TreeNode(n,null,null);
		        root.setLeft (temp);
		        return;
	           }
	      else
	        if (n.compareTo(root.getValue()) > 0)
	          insert(root.getRight(),n);
	         else
	           insert(root.getLeft(),n);
  }

   void print(TreeNode root)
	 {if (root != null)
     {print(root.getLeft());
      System.out.println(root.getValue());
      print(root.getRight());
     }
}


	public static void main(String[] args) throws IOException
	{
	treetester t = new treetester();

	 String n;
	 System.out.println("Enter a name or E to end ");
	 n = in.getString();
	 TreeNode root = new TreeNode(n,null,null);  // must initialize - root object not pointer
	  System.out.println("Enter a name or E to end ");
	 	 n = in.getString();

	  while (n.compareTo("E") != 0)
	   {t.insert(root,n);
	    System.out.println("Enter a name or E to end ");
	     n = in.getString();
	    }
	   t.print(root);
    }
}