import java.awt.*;
import java.util.Enumeration;
import java.applet.*;
import java.io.*;

public class Test extends Applet
 { private SearchTree theTree = SearchTree.empty;
   private int travSwitch = 2;

   public void init() { theTree = SearchTree.empty; } // see java.applet.Applet
   public void destroy() { theTree = null; }     // last action, free resources
   public void paint(Graphics g)
    { g.drawString( traverse(travSwitch, theTree), 10, 20); }

   public void insert(String s) { theTree = theTree.insert(s); repaint(); }
   public void deleteT(String s) { theTree = theTree.delete(s); repaint(); }
   public String toString() { return theTree.toString(); }
   public String toString(int i) { return theTree.toString(i); } // other style
   public void traverseT(int i)  { travSwitch = i; repaint(); }

// be a stand alone application (main) program.
   public static void main( String[] args ) throws IOException
    { System.out.println(" --- Tree Test, L. Allison ---");

// BinaryTree

      BinaryTree t1 =
	 new BinaryTree("anne",
	    new BinaryTree("bill"),
	    new BinaryTree("carol",
	       new BinaryTree("david",
		  new BinaryTree("dave"),
		  new BinaryTree("davina")),
	       new BinaryTree("edwina")));

      System.out.println( t1.toString() );
      System.out.println( t1.toString(1) );
      System.out.println(traverse(1, t1));
      System.out.println(traverse(2, t1));
      System.out.println(traverse(3, t1));
      System.out.println();



// SearchTree

      SearchTree t2 = SearchTree.empty;

      System.out.println("type   i name   or type   d name     ^D to end");
      //try
       //{
	 byte[]b = new byte[1];
	 while(System.in.read(b) >= 0)
          { char opnCh = (char)b[0];  char ch; String id = "";
	    while( System.in.read(b) >= 0 && (ch = (char)b[0]) != '\n' )
	       if( ch != ' ' ) id = id + ch;
	    if( opnCh == 'i' )      t2 = t2.insert(id);
	    else if( opnCh == 'd' ) t2 = t2.delete(id);
	    System.out.println("SearchTree = " + t2.toString() );
          }
       //} catch(Exception e)
       //{ System.out.println("exception" + e.getMessage()); }

      System.out.println(traverse(1, t2));
      System.out.println(traverse(2, t2));
      System.out.println(traverse(3, t2));

    }//main(...)


   static String traverse(int i, BinaryTree t)
    { switch(i)
       { case 0: return t.toString();
	 case 1: return traverse("prefix:",  t.prefix());
	 case 2: return traverse("infix:",   t.infix());
	 default:return traverse("postfix:", t.postfix());
    }  }

   static String traverse(String s, Enumeration e)
    { StringBuffer strB = new StringBuffer();
      strB.append(s);
      while(e.hasMoreElements())
       { Object o = e.nextElement();
	 if(o != null) strB.append( " " + o.toString() );
       }
      return new String(strB);
    }

 }//TreeTest

// Lloyd Allison.
