/media/sda-magnetic/david/Dok-15-2023-11-27/fernuni-hagen/cs-i-ii/old-cs-2-03/java-new/2021-01-05/BinTreeParamProg.java


public class BinTreeParamProg {
    public static void main (String [] args) {
        try {
            BinTree <Str> root = new BinTree <Str> (new Str(args [0]));
            int i;
            
            for (i = 1;  i < args.length;  i++)
                root.insertBinTree (new Str(args[i]));
                
            root.traverseBinTree ();
        
        }
        catch (ArrayIndexOutOfBoundsException e) {
            System.out.println ("To few arguments");
        }
    }
    
    public static class Str implements Comparable {
        String v = null;
        
        Str (String v) {
            this.v = v;
        }
        
        @Override 
        public int compareTo (Object o) {
            Str des = (Str) o;
            return this.v.compareTo (des.v);
        }
        
        @Override 
        public String toString () {
            return this.v;
        }
    }
    
    public static class BinTree <Param extends Comparable> {
        BinTree l = null;
        BinTree r = null;
        Param v = null;
        
        BinTree (Param v) {
            this.l = null;
            this.r = null;
            this.v = v;
        }
        
        public void insertBinTree (Param v) {
            if (this.v.compareTo (v) < 0) {
                if (this.l == null)
                    this.l = new BinTree (v);
                else
                    this.l.insertBinTree (v);
            }
            else {
                if (this.r == null)
                    this.r = new BinTree (v);
                else
                    this.r.insertBinTree (v);
            }
        }
        
        public void traverseBinTree () {
            if (this.r != null)
                this.r.traverseBinTree ();
            System.out.println (this.v.toString());
            if (this.l != null)
                this.l.traverseBinTree ();
        }
    }
}