private void insertNoEmpty(KdNode tree, Point2D p, boolean vertical) {
   if (p.equals(tree.p)) return;
   double keyintree = vertical ? tree.p.y() : tree.p.x();
   double keytocmp = vertical ? p.y() : p.x();
   if (keytocmp < keyintree) {
     if (tree.l == null) {
       size++;
       tree.l = new KdNode(p);
     } else insertNoEmpty(tree.l, p, !vertical);
   } else {
     if (tree.r == null) {
       size++;
       tree.r = new KdNode(p);
     } else insertNoEmpty(tree.r, p, !vertical);
   }
 }
  public void insert(Point2D p) { // add the point p to the set (if it is not already in the set)

    if (root == null) {
      root = new KdNode(p);
      size++;
    } else {
      insertNoEmpty(root, p, true);
    }
  }