/media/sda-magnetic/david/Dokumente-15/fernuni-hagen/cs-i-ii/old-cs-2-03/java-new/2021-01-28/TreeThread.java


public class TreeThread {
    public static void main (String [] args) {
        Thread myThreadA = new TreeTestA ();
        Thread myThreadB = new TreeTestB ();
        myThreadA.start();
        myThreadB.start();
    }
    
    public static class BinTree {
        BinTree l = null;
        BinTree r = null;
        String v = null;
        
        BinTree (String v) {
            this.v = v;
        }
        
        public void insert (String v) {
            if (this.v.compareTo (v) < 0) {
                if (this.l == null)
                    this.l = new BinTree (v);
                else
                    this.l.insert (v);
            }
            else {
                if (this.r == null)
                    this.r = new BinTree (v);
                else
                    this.r.insert (v);
            }
        }
        
        public void traverse () {
            if (this.r != null)
                this.r.traverse ();
            System.out.println (this.v);
            if (this.l != null)
                this.l.traverse ();
        }
    }
    
    public static class TreeTestA extends Thread {
        public void do_it () {
            int i = 0;
            BinTree root = new BinTree (Integer.toString (i));
            
            for (i = i +1;  i < 8192;  i++)
                root.insert (Integer.toString(i));
            root.traverse ();
        }
        
        public void run () {
            do_it ();
        }
    }

    public static class TreeTestB extends Thread {
        public void do_it () {
            int i = 4096;
            BinTree root = new BinTree (Integer.toString (i));
            
            for (i = i +1;  i < 8192;  i++)
                root.insert (Integer.toString(i));
            root.traverse ();
        }
        
        public void run () {
            do_it ();
        }
    }

}