public class BinTreeParamProg {
public static void main (String [] args) {
try {
BinTree <Person> root = new BinTree <Person> (new Person(args[0]));
int i;
for (i = 1; i < args.length; i++)
root.insertBinTree (new Person(args[i]));
root.traverseBinTree ();
}
catch (IndexOutOfBoundsException e) {
System.out.println ("Usage: java BinTreeProg arg1 ...");
}
}
public static class Person implements Comparable {
String name;
Person (String name) {
this.name = name;
}
@Override
public int compareTo (Object o) {
Person des = (Person) o;
return this.name.compareTo (des.toString());
}
@Override
public String toString () {
return this.name;
}
}
public static class BinTree <Dat extends Comparable> {
BinTree l;
BinTree r;
Dat v;
BinTree (Dat v) {
this.v = v;
this.r = null;
this.l = null;
}
void insertBinTree (Dat v) {
if (this.v.compareTo (v) < 0) {
if (this.l == null)
this.l = new BinTree (v);
else
this.l.insertBinTree (v);
}
else {
if (this.r == null)
this.r = new BinTree (v);
else
this.r.insertBinTree (v);
}
}
public void traverseBinTree () {
if (this.r != null)
this.r.traverseBinTree ();
System.out.println (this.v.toString());
if (this.l != null)
this.l.traverseBinTree ();
}
}
}