if (p->bst_link[1] == NULL)
  q->bst_link[dir] = p->bst_link[0];
else
  {
    struct bst_node *r = p->bst_link[1];
    if (r->bst_link[0] == NULL)
      {
        r->bst_link[0] = p->bst_link[0];
        q->bst_link[dir] = r;
      }
    else
      {
        struct bst_node *s;
        for (;;)
          {
            s = r->bst_link[0];
            if (s->bst_link[0] == NULL)
              break;

            r = s;
          }
        r->bst_link[0] = s->bst_link[1];
        s->bst_link[0] = p->bst_link[0];
        s->bst_link[1] = p->bst_link[1];
        q->bst_link[dir] = s;
      }
  }

