public class BinTreeProg {
public static void main (String [] args) {
try {
BinTree root = new BinTree (args [0]);
int i;
for (i = 1; i < args.length; i++)
root.insert (args [i]);
root.traverse ();
}
catch (ArrayIndexOutOfBoundsException e) {
System.out.println ("Too few arguments");
}
}
public static class BinTree {
BinTree l = this;
BinTree r = this;
String v = null;
BinTree (String v) {
this.v = v;
this.l = this;
this.r = this;
}
public void insert (String v) {
if (this.v.compareTo (this.v) < 0) {
if (this.l == this)
this.l = new BinTree (v);
else
this.l.insert (v);
}
else {
if (this.r == this)
this.r = new BinTree (v);
else
this.r.insert (v);
}
}
public void traverse () {
if (this.l != this)
this.l.traverse ();
System.out.println (this.v);
if (this.r != this)
this.r.traverse ();
}
}
}