/*
 * $ java-algs4 RandomOps 888 127 | java-algs4 RBClient
 * $ java-algs4 RandomOps 888 127 200 | java-algs4 RBClient
 * $ java-algs4 RandomOps 888 127 -200 | java-algs4 RBClient
 */

public class RBClient
{
    public static void main(String[] args)
    {
	RedBlackBSTDraw<Integer, Integer> st = new RedBlackBSTDraw<>();

	st.drawPrepare(800);
	StdDraw.enableDoubleBuffering();	
	while (!StdIn.isEmpty()) {
	    int x = StdIn.readInt();
	    if (x > 0)
		st.put(x, 0);
	    else
		st.delete(-x);
	    st.draw(.95, .003, .004);
	    StdDraw.setPenColor(StdDraw.BLACK);
	    int N = st.size();
	    StdDraw.textLeft(0.02, 0.33, "N = " + N);
	    StdDraw.textLeft(0.02, 0.28,
			     String.format("2-3 height = %d  (best possible = %d)\n",
					   st.height23P(), RBBSTDraw.log3ceil(N) - 1));
	    StdDraw.textLeft(0.02, 0.23,
			     String.format("Plain height = %d  (best possible = %d)",
					   st.height(), RBBSTDraw.lgfloor(N)
					   ));
	    int ipl = st.iplP();
	    StdDraw.textLeft(0.02, 0.18, "IPL = " + ipl);
	    StdDraw.textLeft(0.02, 0.13,
			     String.format("Average depth = %.4f  (best possible = %.4f)",
					   (double)ipl / N,
					   (double)RBBSTDraw.bestIPL(N) / N
					   ));
	    int reds = st.sizeRP();
	    StdDraw.textLeft(0.02, 0.08,
			     String.format("No. red links = %d  (%.2f%% of the nodes are red)\n",
					   reds, 100.0 * reds / N)
			     );
	    StdDraw.textLeft(0.02, 0.03,
			     String.format("Average no. of red links to an external node = %.4f",
					   st.aveRedLinks())
			     );
	    StdDraw.show();
	    StdDraw.pause(100);		
	}
    }
}
