Problem p02285 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#define MAXN 500000
struct node {
  int key;
  node *ch[2], *fa;
};
node tree[MAXN + 5];
node *Root, *ncnt, *NIL;
void Init() {
  NIL = &tree[0];
  NIL->ch[0] = NIL->ch[1] = NIL->fa = NIL;
  ncnt = &tree[1];
  Root = NIL;
}
node *Find(node *rt, int val) {
  if (rt == NIL)
    return NIL;
  if (rt->key == val)
    return rt;
  int d = (val >= rt->key);
  return Find(rt->ch[d], val);
}
node *FindNext(node *rt) {
  if (rt == NIL)
    return NIL;
  node *y = rt->ch[1];
  while (y->ch[0] != NIL)
    y = y->ch[0];
  return y;
}
void Delete(node *&rt, int val) {
  node *x, *y, *z = Find(rt, val);
  if (z == NIL)
    return;
  if (z->ch[0] == NIL || z->ch[1] == NIL)
    y = z;
  else
    y = FindNext(z);
  if (y->ch[0] != NIL)
    x = y->ch[0];
  else
    x = y->ch[1];
  if (x != NIL)
    x->fa = y->fa;
  if (y->fa == NIL)
    Root = x;
  else {
    int d = (y == y->fa->ch[1]);
    y->fa->ch[d] = x;
  }
  if (y != z)
    z->key = y->key;
}
inline node *NewNode(int val) {
  node *p = ++ncnt;
  p->key = val;
  p->ch[0] = p->ch[1] = NIL;
  return p;
}
void Insert(node *&rt, node *fa, int val) {
  if (rt == NIL) {
    rt = NewNode(val);
    rt->fa = fa;
    return;
  }
  int d = (val >= rt->key);
  Insert(rt->ch[d], rt, val);
}
void Middle(node *rt) {
  if (rt == NIL)
    return;
  Middle(rt->ch[0]);
  printf(" %d", rt->key);
  Middle(rt->ch[1]);
}
void First(node *rt) {
  if (rt == NIL)
    return;
  printf(" %d", rt->key);
  First(rt->ch[0]);
  First(rt->ch[1]);
}
int main() {
  int n, x;
  char op[100];
  Init();
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%s", op);
    if (op[0] == 'i') {
      scanf("%d", &x);
      Insert(Root, NIL, x);
    } else if (op[0] == 'f') {
      scanf("%d", &x);
      node *p = Find(Root, x);
      if (p == NIL)
        puts("no");
      else
        puts("yes");
    } else if (op[0] == 'd') {
      scanf("%d", &x);
      Delete(Root, x);
    } else {
      Middle(Root);
      puts("");
      First(Root);
      puts("");
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#define MAXN 500001
struct node {
  int v;
  node *ch[2], *fa;
} tree[MAXN + 5];
node *root, *ncnt, *NIL;
void init() {
  NIL = &tree[0];
  NIL->fa = NIL->ch[0] = NIL->ch[1] = NIL;
  ncnt = &tree[1];
  root = NIL;
}
inline node *newnode(int v) {
  node *p = ++ncnt;
  p->v = v;
  p->ch[0] = p->ch[1] = NIL;
}
void insert(node *&rt, node *fa, int v) {
  if (rt == NIL) {
    rt = newnode(v);
    rt->fa = fa;
    return;
  }
  int d = (v >= rt->v);
  insert(rt->ch[d], rt, v);
}
node *find(node *rt, int v) {
  if (rt == NIL)
    return NIL;
  if (rt->v == v)
    return rt;
  int d = (v > rt->v);
  return find(rt->ch[d], v);
}
void inorder(node *rt) {
  if (rt == NIL)
    return;
  inorder(rt->ch[0]);
  printf(" %d", rt->v);
  inorder(rt->ch[1]);
}
void preorder(node *rt) {
  if (rt == NIL)
    return;
  printf(" %d", rt->v);
  preorder(rt->ch[0]);
  preorder(rt->ch[1]);
}
node *findnext(node *rt) {
  if (rt == NIL)
    return NIL;
  node *y = rt->ch[1];
  while (y->ch[0] != NIL)
    y = y->ch[0];
  return y;
}
void Delete(node *rt, int v) {
  node *x, *y, *z = find(rt, v);
  if (z == NIL)
    return;
  if (z->ch[0] == NIL || z->ch[1] == NIL)
    y = z;
  else
    y = findnext(z);
  if (y->ch[0] != NIL)
    x = y->ch[0];
  else
    x = y->ch[1];
  if (x != NIL)
    x->fa = y->fa;
  if (y->fa == NIL)
    root = x;
  else if (y->fa->ch[0] == y)
    y->fa->ch[0] = x;
  else
    y->fa->ch[1] = x;
  if (z != y)
    z->v = y->v;
}
int main() {
  int n, a;
  scanf("%d", &n);
  init();
  for (int i = 0; i < n; i++) {
    char x[20];
    scanf("%s", x);
    if (x[0] == 'i') {
      scanf("%d", &a);
      insert(root, NIL, a);
    } else if (x[0] == 'f') {
      scanf("%d", &a);
      if (find(root, a) == NIL)
        printf("no\\n");
      else
        printf("yes\\n");
    } else if (x[0] == 'p') {
      inorder(root);
      printf("\\n");
      preorder(root);
      printf("\\n");
    } else {
      scanf("%d", &a);
      Delete(root, a);
    }
  }
}