public class BinTree {
public static void main (String [] args) {
int i;
if (args.length > 0) {
BinTreeNode root = new BinTreeNode (args[0]);
for (i = 1; i < args.length; i++)
root.insert (args[i]);
root.print();
System.out.println(root.find (args[0]));
System.out.println(root.find ("Hallo Welt"));
}
}
static class BinTreeNode {
BinTreeNode l;
BinTreeNode r;
String v;
BinTreeNode (String v) {
this.l = null;
this.r = null;
this.v = v;
}
void insert (String v) {
if (this.v.compareTo (v) <= 0) {
if (this.l == null)
this.l = new BinTreeNode (v);
else
this.l.insert (v);
}
else {
if (this.r == null)
this.r = new BinTreeNode (v);
else
this.r.insert (v);
}
}
void print () {
if (this.l != null)
this.l.print();
System.out.println (this.v);
if (this.r != null)
this.r.print();
}
boolean find (String key) {
boolean flag = false;
if (this.v.compareTo(key) == 0)
return true;
else if (this.v.compareTo(key) < 0) {
if (this.l == null)
flag = false;
else
flag = this.l.find (key);
}
else if (this.v.compareTo(key) > 0) {
if (this.r == null)
flag = false;
else
flag = this.r.find (key);
}
return flag;
}
}
}