public class BinTreeParamProg {
public static void main (String [] args) {
try {
BinTree <Str> root = new BinTree <Str> (new Str(args [0]));
int i;
for (i = 1; i < args.length; i++)
root.insert (new Str(args [i]));
root.traverse ();
}
catch (ArrayIndexOutOfBoundsException e) {
BinTree root = new BinTree (new Str("Emil"));
root.insert (new Str("Dora"));
root.insert (new Str("Anton"));
root.insert (new Str("Berta"));
root.insert (new Str("Caesar"));
root.traverse ();
}
}
public static class Str implements Comparable {
String v = null;
Str (String v) {
this.v = v;
}
Str () {
this.v = "Anton";
}
@Override
public int compareTo (Object o) {
Str des = (Str) o;
return this.v.compareTo (des.toString ());
}
@Override
public String toString () {
return this.v;
}
}
public static class BinTree <Param extends Comparable> {
BinTree l = null;
BinTree r = null;
Param v = null;
BinTree (Param v) {
this.v = v;
this.l = null;
this.r = null;
}
public void insert (Param v) {
if (this.v.compareTo (v) > 0) {
if (this.l == null)
this.l = new BinTree (v);
else
this.l.insert (v);
}
else {
if (this.r == null)
this.r = new BinTree (v);
else
this.r.insert (v);
}
}
public void traverse () {
if (this.l != null)
this.l.traverse ();
System.out.println (this.v.toString ());
if (this.r != null)
this.r.traverse ();
}
}
}