/media/sda-magnetic/david/Dok-15-2023-11-27/fernuni-hagen/cs-i-ii/old-cs-2-03/java-new/bintree-2020-11-10-01/BinTreeProg.java


public class BinTreeProg {
    
    public static void main (String [] args) {
        try {
            int i;
            BinTree root = new BinTree(args[0]);
            
            for (i = 1;  i < args.length-2;  i++)
                root.binTreeInsert (args[i]);
            root.binTreeTraverse();
            
            System.out.println (root.binTreeExists (args[args.length-1]));
            System.out.println (root.binTreeCountOccurance (args[args.length-1]));
            System.out.println (root.binTreeBalancedCounter (args[args.length-1]));
        }
        catch (ArrayIndexOutOfBoundsException e) {
            System.out.println ("Usage: BinTreeProg Data1 Data2 Data3 ...Keyword");
            System.exit (-1);
        }
    }
    
    static class BinTree {
        String v;
        BinTree l;
        BinTree r;
        
        BinTree (String val) {
            this.v = val;
            this.l = null;
            this.r = null;
        }
        void binTreeInsert (String val) {
            if (this.v.compareTo (val) < 0) {
                if (this.l == null) 
                    this.l = new BinTree (val);
                else
                    this.l.binTreeInsert (val);
            }
            else {
                if (this.r == null) 
                    this.r = new BinTree (val);
                else
                    this.r.binTreeInsert (val);
            }
        }
        void binTreeTraverse () {
            if (this.l != null)
                this.l.binTreeTraverse();
            System.out.println (this.v);
            if (this.r != null)
                this.r.binTreeTraverse();
        }
        boolean binTreeExists (String val) {
            boolean flag;
            
            if (this.v.compareTo (val) == 0)
                flag = true;
            else if (this.v.compareTo (val) < 0) {
                if (this.l == null)
                    flag = false;
                else
                    flag = this.l.binTreeExists (val);
            }
            else {
                if (this.r == null)
                    flag = false;
                else
                    flag = this.r.binTreeExists (val);
            }
            return flag;
        }
        int binTreeCountOccurance (String val) {
            int count = 0;
            
            if (this.v.compareTo (val) == 0) {
                if (this.l != null)
                    count = 1 + this.l.binTreeCountOccurance(val);
                else 
                    count = 0;
                if (this.r != null)
                    count = 1 + this.r.binTreeCountOccurance(val);
            }
            else if (this.v.compareTo (val) < 0) {
                if (this.l == null)
                    count = 0;
                else
                    count = this.l.binTreeCountOccurance (val);
            }
            else {
                if (this.r == null)
                    count = 0;
                else
                    count = this.r.binTreeCountOccurance (val);
            }
            return count;
        }
        boolean binisTreeBalanced (String val) {
            if (binTreeBalancedCounter (val) != 0)
                return false;
            return true;
        }
        int binTreeBalancedCounter (String val) {
            int retval = 0;
            
            if (this.l != null) {
                if (this.l.v.compareTo (val) == 0)
                    retval += (1+this.l.binTreeBalancedCounter (val));
                else
                    retval += this.l.binTreeBalancedCounter (val);
            }
            else
                retval += 0;            
            if (this.r != null) {
                if (this.r.v.compareTo (val) == 0)
                    retval += (-1+this.r.binTreeBalancedCounter (val));
                else
                    retval += this.r.binTreeBalancedCounter (val);
            }
            else 
                retval += 0;
            return retval;
        }
    }
}