import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.Stack;

public class BinaryTreeInOrderIterable<Key extends Comparable<Key>> implements Iterable<Key> {
    
    private class Node {
        private Key key;
        private Node left;
        private Node right;   
        
        public Node(Key key) {
            this.key = key;
        }
    }
    
    private Node root;
    private int modCount;
    
    public void put(Key key) {
        root = putR(root, key);
        modCount++;
    }
    
    private Node putR(Node n, Key key) {
        if (n == null)  {                         
            Node newNode = new Node(key);                       
            return newNode;
        }
        
        int cmp = key.compareTo(n.key); 
        if (cmp < 0)        n.left = putR(n.left, key);
        else if (cmp > 0)   n.right = putR(n.right, key);
        
        return n;   
    }
    
    public String toString() {
        String s = "";
        for (Key key : this) {
            s += key + ",";
        }
        return s;
    }
    
    public Iterator<Key> iterator() {
        return new Iterator<Key>() {
            
            ///////////////////////////////////////////
            // INSERT ANY DESIRED INSTANCE VARIABLES //
            // FOR THE ANONYMOUS INNER CLASS HERE    //
            ///////////////////////////////////////////

            public boolean hasNext() {

                ////////////////////// 
                // INSERT CODE HERE //
                ////////////////////// 
            }

            public Key next() {

                ////////////////////// 
                // INSERT CODE HERE //
                ////////////////////// 
            }
        };
    }
    
    public static void main(String[] args) {
        
        BinaryTreeInOrderIterable<Character> tree = new BinaryTreeInOrderIterable<Character>();
        String charsToPut = "thequickbrownfoxjumpedoverthelazydog";
        for (int i = 0; i < charsToPut.length(); i++) {
            tree.put(charsToPut.charAt(i));
        }
        
        System.out.println(tree);  // As the sentence "The quick brown fox jumped over the lazy dog"
                                   // includes every letter of the alphabet, the output 
                                   // should be "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,t,u,v,w,x,y,z,"
        
        // UNCOMMENT BELOW CODE TO TEST IDENTIFICATION OF CONCURRENT MODIFICATION 
        // (Should throw a ConcurrentModificationException...)
        /*  
        String s = "";
        int cnt = 0;
        for (Character key : tree) {
            s += key + ",";
            cnt++;
            if (cnt == 10) tree.put('X');
        }
        System.out.println(s);
        */
    }
}


