/* Ich habe zufaelligerweise gerade einen Binaerbaum programmiert - weil es Spass macht -
sie finden den auch auf http://www.ituenix.de/php-test/java/BinTree.java.txt
Ich bin so frech und gebe ihnen einfach meine Version, die ich schon bereits selbstaendig geschrieben habe */
/* Ersetzen Sie, inorder durch print oder anders herum */
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.contains (args[0]));
if (args.length > 3)
System.out.println (root.contains (args[2]));
System.out.println (root.contains ("Hallo Welt"));
}
}
public static class BinTreeNode {
BinTreeNode l;
BinTreeNode r;
String v;
BinTreeNode (String v) {
this.l = null;
this.r = null;
this.v = v;
}
public void insert (String v) {
BinTreeNode ptr = this;
if ((ptr.v.compareTo (v)) <= 0) {
if (ptr.l == null) {
BinTreeNode newNode = new BinTreeNode (v);
ptr.l = newNode;
}
else
ptr.l.insert (v);
}
else {
if (ptr.r == null) {
BinTreeNode newNode = new BinTreeNode (v);
ptr.r = newNode;
}
else
ptr.r.insert (v);
}
}
public void print () {
if (this.l != null)
this.l.print();
System.out.println (this.v);
if (this.r != null)
this.r.print();
}
public boolean contains (String k) {
boolean flag = false;
if ((this.v.compareTo (k)) == 0)
return true;
else if ((this.v.compareTo (k)) < 0) {
if (this.l == null)
return false;
flag = this.l.contains (k);
}
else if ((this.v.compareTo (k)) > 0) {
if (this.r == null)
return false;
flag = this.r.contains (k);
}
return flag;
}
}
}