/media/sda-magnetic/david/Dok-15-2023-11-27/fernuni-hagen/cs-i-ii/old-cs-2-03/java-new/2020-11-30/BinaryTreeProg.java


public class BinaryTreeProg {
    public static void main (String [] args) {
        try {
            BinaryTree root = new BinaryTree (args[0]);
            int i;
            
            for (i = 1;  i < args.length;  i++)
                root.insertBinaryTree (args[i]);
            root.traverseBinaryTree ();
        }
        catch (ArrayIndexOutOfBoundsException e) {
            System.out.println ("You have to give one argument at least");
        }
    }
    
    public static class BinaryTree {
        BinaryTree l;
        BinaryTree r;
        String v;
        
        BinaryTree (String v) {
            this.v = v;
            this.l = null;
            this.r = null;
        }
        
        void insertBinaryTree (String 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);
            if (this.r != null)
                this.r.traverseBinaryTree();
        }
    }
}