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.insertBinTree (args[i]);
root.traverseBinTree ();
}
catch (ArrayIndexOutOfBoundsException e) {
System.out.println ("usage: java BinTreeProg arg1 ...");
}
}
public static class BinTree {
BinTree l;
BinTree r;
String v;
BinTree (String v) {
this.l = null;
this.r = null;
this.v = v;
}
void insertBinTree (String 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.r != null)
this.r.traverseBinTree ();
System.out.println (this.v);
if (this.l != null)
this.l.traverseBinTree ();
}
}
}