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


class BinTreeProg {
    public static void main (String [] args) {
        try {
            BinTree root = new BinTree (args[0]);
            int i = 1;
            
            while (i < args.length) {
                root.insert(args[i]);
                i++;
            }
            
            root.print();
            
        }
        catch (ArrayIndexOutOfBoundsException e) {
            System.out.println ("Maybe you did not gave the necessary number of arguments");
        }
    }
    
    static class BinTree {
        BinTree left;
        BinTree right;
        String val;
        
        BinTree (String val) {
            this.val = val;
            this.left = null;
            this.right = null;
        }
        
        void insert (String val) {
            try {
                if (this.val.compareTo(val) <= 0) {
                    if (this.left == null) 
                        this.left = new BinTree (val);
                    else 
                        this.left.insert (val);
                }
                else {
                    if (this.right == null) 
                        this.right = new BinTree (val);
                    else 
                        this.right.insert (val);                
                }
            }
            catch (NullPointerException e) {
                System.out.println ("You haven't initialised the root");
            }
        }
        
        void print () {
            if (this.left != null)
                this.left.print();
            System.out.println (val);
            if (this.right != null)
                this.right.print();
        }
    }
}