public class BinaryTreeParamProg {
public static void main (String [] args) {
try {
BinaryTree <Str> root = new BinaryTree <Str> (new Str(args[0]));
int i;
for (i = 1; i < args.length; i++)
root.insertBinaryTree (new Str(args[i]));
root.traverseBinaryTree ();
}
catch (ArrayIndexOutOfBoundsException e) {
System.out.println ("You have to give one argument at least");
}
}
public static class Str implements Comparable {
String src;
Str (String src) {
this.src = src;
}
public int compareTo (Object o) {
Str des = (Str) o;
return this.src.compareTo (des.src);
}
public String toString () {
return this.src;
}
}
public static class BinaryTree <P extends Comparable>{
BinaryTree l;
BinaryTree r;
P v;
BinaryTree (P v) {
this.v = v;
this.l = null;
this.r = null;
}
void insertBinaryTree (P v) {
if (this.v.compareTo (v) > 0) {
if (this.l == null)
this.l = new BinaryTree (v);
else
this.l.insertBinaryTree (v);
}
else {
if (this.r == null)
this.r = new BinaryTree (v);
else
this.r.insertBinaryTree (v);
}
}
void traverseBinaryTree () {
if (this.l != null)
this.l.traverseBinaryTree();
System.out.println (this.v.toString());
if (this.r != null)
this.r.traverseBinaryTree();
}
}
}