public class BinTreeParamProg {
public static void main (String [] args) {
try {
int i;
BinTree <Person> root = new BinTree <Person> (new Person (args[0], args[1]));
for (i = 2; i < args.length; i += 2)
root.insertBinTree (new Person (args[i], args[i+1]));
root.traverseBinTree();
}
catch (ArrayIndexOutOfBoundsException e) {
System.out.println ("You have to give on argument");
}
}
public static class Person implements Comparable {
String firstname;
String lastname;
public Person (String firstname, String lastname) {
this.firstname = firstname;
this.lastname = lastname;
}
public String toString () {
return lastname + ", " + firstname;
}
public int compareTo (Object o) {
Person p = (Person) o;
return toString().compareTo (p.toString());
}
}
static public class BinTree <P extends Comparable> {
BinTree l;
BinTree r;
P v;
BinTree (P v) {
this.l = null;
this.r = null;
this.v = v;
}
void insertBinTree (P 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);
}
}
void traverseBinTree () {
if (this.l != null)
this.l.traverseBinTree();
System.out.println (this.v.toString());
if (this.r != null)
this.r.traverseBinTree();
}
}
}