import java.awt.*;
import Point;
class BSTdig
  {
    int N;
    Node head;
    int depth;

    int delay;
    Graphics page;

    void clear(Color c)
      { 
        int h = page.getClipRect().height;
        int w = page.getClipRect().width;
        page.setColor(c);
        page.fillRect(0, 0, w, h);
      }

    BSTdig(int N, int d, int sw, int delay, Graphics page)
      {
        this.page = page;
        this.head = null; this.depth = d; this.delay = delay;
        if (sw == 0)
          for (int i = 0; i < N; i++)
            this.head = insert(this.head, Math.random(), 1);
        if (sw == 1)
          for (int i = 0; i < N; i++)
            this.head = insert(this.head, ((double) i)/((double) N+1), 0);
        System.out.println(N + "-node tree built");
      }
    void drawVertex(Point p, Color c)
      { 
        int h = page.getClipRect().height-16;
        int w = page.getClipRect().width-16;
        if (c == Color.white)
             page.setColor(Color.black);
        else page.setColor(Color.lightGray);
        page.fillOval( (int) (w*p.x)+6, (int) (h*p.y)+6, 16, 16);
        page.setColor(c);
        for (int j = 0; j < delay; j++)
          {
            page.fillOval( (int) (w*p.x)+8, (int) (h*p.y)+8, 16, 16);
          }
      }
    void drawEdge(Point p, Point q, Color c)
      { 
        int h = page.getClipRect().height-16;
        int w = page.getClipRect().width-16;
        page.setColor(c);
        page.drawLine( (int) (w*p.x)+16, (int) (h*p.y)+16, 
                         (int) (w*q.x)+16, (int) (h*q.y)+16);
        if (c == Color.lightGray)
          {
            page.drawLine( (int) (w*p.x)+17, (int) (h*p.y)+17, 
                         (int) (w*q.x)+17, (int) (h*q.y)+17);
            page.drawLine( (int) (w*p.x)+15, (int) (h*p.y)+15, 
                         (int) (w*q.x)+15, (int) (h*q.y)+15);
          }
      }
    void drawTree(Node h, Node p)
      {
        if (h == null) return;
        drawEdge(p.pt, h.pt, Color.black);
        drawVertex(h.pt, Color.white);
        drawVertex(p.pt, Color.white);
        drawTree(h.l, h);
        drawTree(h.r, h);
      }
     int bit(double v, int d)
      { int j, k;
        for (k = 1; k < d; k++) 
          { v = 2*v; if (v >1) v = v-1; }
        v = 2*v;
        if (v > 1) return 1; else return 0;
      }        
    Node insert(Node h, double v, int d)
      {
        if (h == null) 
          {
            double sy = ((double) d)/((double) this.depth+1);
            Point s = new Point(v, sy);
            return new Node(v, d+1, s);
          }
        double sy = ((double) d+1)/((double) this.depth+1);
        Point s = new Point(v, sy);
        double my = ((double) d)/((double) this.depth+1);
        Point me = new Point(h.v, my);
        if ((h.l == null) && (bit(v, d) == 0))
          {
            drawEdge(s, me, Color.black);
            drawVertex(me, Color.white);
            drawVertex(s, Color.white);
          }
        if ((h.r == null) && (bit(v, d) == 1))
          {
            drawEdge(s, me, Color.black);
            drawVertex(me, Color.white);
            drawVertex(s, Color.white);
          }
        drawVertex(me, Color.red);
        if (bit(v, d) ==0) 
             h.l = insert(h.l, v, d+1);                
        else h.r = insert(h.r, v, d+1);
        drawVertex(me, Color.white);
        return h;
      }
  }










