// Do not edit this file for the homework.
// This is our "main" entry point, for some testing.

public class Driver
{
    public static void main(String[] args)
    {
        String typename = "PRedBlackBST"; // default
        PBST<String,Integer> first = null;
        if (args.length==1) typename = args[0];
        if (typename.equals("PBST"))
            first = new PBST<String,Integer>();
        if (typename.equals("PRedBlackBST"))
            first = new PRedBlackBST<String,Integer>();
        if (first == null) {    // usage message, give up
            System.out.println("Usage: java Driver [TYPENAME]");
            System.out.println("TYPENAME should be PBST or PRedBlackBST,");
            System.out.println("it defaults to PRedBlackBST.");
            return;
        }
        // Create a history of PBST objects, check() on each.
        String input = "SEARCHEXAMPLE";
        int N = input.length();
        PBST<String, Integer>[] h = new PBST[N+1];
        h[0] = first;
        System.out.println("Phase 1: book 'SEARCHEXAMPLE' input");
        System.out.println();
        System.out.println("h[0] = new " + typename + "();");
        for (int val=0; val<N; ++val) {
            String key = input.substring(val,val+1);
            System.out.printf("h[%d] = h[%d].put(\"%s\", %d);%n",
                               val+1, val, key, val);
            h[val+1] = h[val].put(key, val);
            h[val+1].check();
        }
        System.out.printf("%nAll these trees are still usable:%n%n");
        for (int val=0; val <= N; ++val) {
            System.out.printf("h[%d] is %s%n", val, h[val]);
            System.out.printf("h[%d].rank(\"S\") == %d, ", val,
                              h[val].rank("S"));
            System.out.printf("h[%d].get(\"E\") == %s%n", val,
                              h[val].get("E"));
        }
        int nodes1 = PBST.nodes();
        System.out.printf("%nCreated %d nodes so far.%n%n", nodes1);

        // Note we start over at 'first' again.
        System.out.println("Phase 2: starting with h[0], inserting " +
                           "100 keys in increasing order");
        PBST t = first;
        for (int i=100; i<200; ++i)
            t = t.put((""+i).substring(1), i-100);
        int nodes2 = PBST.nodes()-nodes1;
        System.out.printf("Created %d nodes in Phase 2.%n%n", nodes2);
        if (t.check())
            System.out.println("final check() succeeded");
        else
            System.out.println("final check() FAILED!");


    }

}
