public static Tree buildTree(int n, boolean dir, double min_x,
			       double max_x, double min_y, double max_y) 
  {
    if (n==0) return null;

    Tree left, right;
    double x, y;
    if (dir) {
      dir = !dir;
      double med = median(min_x,max_x,n);
      left = buildTree(n/2, dir, min_x, med, min_y, max_y);
      right = buildTree(n/2, dir, med, max_x, min_y, max_y);
      x = med;
      y = uniform(min_y, max_y);
    } else {
      dir = !dir;
      double med = median(min_y,max_y,n);
      left = buildTree(n/2, dir, min_x, max_x, min_y, med);
      right = buildTree(n/2, dir, min_x, max_x, med, max_y);
      y = med;
      x = uniform(min_x, max_x);
    }
    return new Tree(n, x, y, left, right);
  }