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

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
struct shu {
  int key;
  shu *left, *right, *parent;
};
shu *nil;
shu *root;
char s[100];
void charu(int k) {
  shu *y = nil;
  shu *x = root;
  shu *z;
  z = new shu;
  z->key = k;
  z->left = nil;
  z->right = nil;
  while (x != nil) {
    y = x;
    if (z->key < x->key) {
      x = x->left;
    } else
      x = x->right;
  }
  z->parent = y;
  if (y == nil)
    root = z;
  else {
    if (z->key < y->key) {
      y->left = z;
    } else
      y->right = z;
  }
}
void qianxu(shu *u) {
  if (u == nil)
    return;
  printf(" %d", u->key);
  qianxu(u->left);
  qianxu(u->right);
}
void zhongxu(shu *u) {
  if (u == nil)
    return;
  zhongxu(u->left);
  printf(" %d", u->key);
  zhongxu(u->right);
}
shu *sousuo(int k, shu *u) {
  while (u != nil && k != u->key) {
    if (k < u->key)
      u = u->left;
    else
      u = u->right;
  }
  return u;
}
shu *zuixiao(shu *u) {
  while (u->left != nil) {
    u = u->left;
  }
  return u;
}

shu *hou(shu *x) {
  if (x->right != nil)
    return zuixiao(x->right);
  shu *y = x->parent;
  while (y != nil && x == y->right) {
    x = y;
    y = y->parent;
  }
  return y;
}

void shanchu(shu *u) {
  shu *x;
  shu *y;
  if (u->left == nil || u->right == nil) {
    y = u;
  } else
    y = hou(u);
  if (y->left != nil) {
    x = y->left;
  } else
    x = y->right;
  if (x != nil) {
    x->parent = y->parent;
  }
  if (y->parent == nil)
    root = x;
  else {
    if (y == y->parent->left) {
      y->parent->left = x;
    } else
      y->parent->right = x;
  }
  if (y != u) {
    u->key = y->key;
  }
  free(y);
}
int main() {
  int n;
  int m;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%s", s);
    if (s[0] == 'i') {
      scanf("%d", &m);
      charu(m);
    } else if (s[0] == 'p') {
      zhongxu(root);
      printf("\\n");
      qianxu(root);
      printf("\\n");
    } else if (s[0] == 'f') {
      scanf("%d", &m);
      shu *x = sousuo(m, root);
      if (x != nil)
        printf("yes\\n");
      else
        printf("no\\n");
    } else if (s[0] == 'd') {
      scanf("%d", &m);
      shanchu(sousuo(m, root));
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
struct shu {
  int key;
  shu *left, *right, *parent;
};
shu *nil;
shu *root;
char s[100];
void charu(int k) {
  shu *y = nil;
  shu *x = root;
  shu *z;
  z = new shu;
  z->key = k;
  z->left = nil;
  z->right = nil;
  while (x != nil) {
    y = x;
    if (z->key < x->key) {
      x = x->left;
    } else
      x = x->right;
  }
  z->parent = y;
  if (y == nil)
    root = z;
  else {
    if (z->key < y->key) {
      y->left = z;
    } else
      y->right = z;
  }
}
void qianxu(shu *u) {
  if (u == nil)
    return;
  printf(" %d", u->key);
  qianxu(u->left);
  qianxu(u->right);
}
void zhongxu(shu *u) {
  if (u == nil)
    return;
  zhongxu(u->left);
  printf(" %d", u->key);
  zhongxu(u->right);
}
shu *sousuo(int k, shu *u) {
  while (u != nil && k != u->key) {
    if (k < u->key)
      u = u->left;
    else
      u = u->right;
  }
  return u;
}
shu *zuixiao(shu *u) {
  while (u->left != nil) {
    u = u->left;
  }
  return u;
}

shu *hou(shu *u) {
  if (u->right != nil)
    return zuixiao(u->right);
  shu *x = u->parent;
  while (x != nil && x == u->right) {
    x = u;
    u = u->parent;
  }
  return u;
}

void shanchu(shu *u) {
  shu *x;
  shu *y;
  if (u->left != nil || u->right != nil) {
    y = u;
  } else
    y = hou(u);
  if (y->left != nil) {
    x = y->left;
  } else
    x = y->right;
  if (x != nil) {
    x->parent = y->parent;
  }
  if (y->parent == nil)
    root = x;
  else {
    if (y == y->parent->left) {
      y->parent->left = x;
    } else
      y->parent->right = x;
  }
  if (y != u) {
    y->key = u->key;
  }
  free(y);
}
int main() {
  int n;
  int m;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%s", s);
    if (s[0] == 'i') {
      scanf("%d", &m);
      charu(m);
    } else if (s[0] == 'p') {
      zhongxu(root);
      printf("\\n");
      qianxu(root);
      printf("\\n");
    } else if (s[0] == 'f') {
      scanf("%d", &m);
      shu *x = sousuo(m, root);
      if (x != nil)
        printf("yes\\n");
      else
        printf("no\\n");
    } else if (s[0] == 'd') {
      scanf("%d", &m);
      shanchu(sousuo(m, root));
    }
  }
  return 0;
}