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

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <string.h>
#include <string>
#include <vector>
#define _USE_MATH_DEFINES
#include <math.h>

using namespace std;

struct node {
  int key;
  node *parent, *left, *right;
};

node *root, *NIL;

void insert(int k) {
  node *y = NIL;
  node *x = root;
  node *z;

  z = (node *)malloc(sizeof(node));
  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;
  }
}

node *find(int k) {
  node *x = root;
  node *y = NIL;
  while (x != NIL && k != x->key) {
    if (k < x->key)
      x = x->left;
    else if (k > x->key)
      x = x->right;
  }
  return x;
}

node *mini(node *u) {
  while (u->left != NIL) {
    u = u->left;
  }
  return u;
}

node *Successor(node *u) {
  if (u->right != NIL)
    return mini(u->right);
  node *y = u->parent;
  while (y != NIL && u == y->right) {
    u = y;
    y = y->parent;
  }
  return y;
}

void Delete(node *z) {
  node *y;
  node *x;
  if (z->left == NIL || z->right == NIL)
    y = z;
  else
    y = Successor(z);
  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 != z) {
    z->key = y->key;
  }
  free(y);
}

void Preorder(node *t) {
  if (t == NIL)
    return;
  printf(" %d", t->key);
  Preorder(t->left);
  Preorder(t->right);
}

void Inorder(node *t) {
  if (t == NIL)
    return;
  Inorder(t->left);
  printf(" %d", t->key);
  Inorder(t->right);
}

void Postorder(node *t) {
  if (t == NIL)
    return;
  Postorder(t->left);
  Postorder(t->right);
  printf(" %d", t->key);
}

int main() {
  int x, n;
  string st;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> st;
    if (st[0] == 'i') {
      cin >> x;
      insert(x);
    } else if (st[0] == 'p') {
      Inorder(root);
      cout << endl;
      Preorder(root);
      cout << endl;
    } else if (st[0] == 'f') {
      cin >> x;
      node *t = find(x);
      if (t == NIL)
        cout << "no";
      else
        cout << "yes";
      cout << endl;
    } else {
      cin >> x;
      Delete(find(x));
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <string.h>
#include <string>
#include <vector>
#define _USE_MATH_DEFINES
#include <math.h>

using namespace std;

struct node {
  int key;
  node *parent, *left, *right;
};

node *root, *NIL;

void insert(int k) {
  node *y = NIL;
  node *x = root;
  node *z;

  z = (node *)malloc(sizeof(node));
  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;
  }
}

node *find(int k) {
  node *x = root;
  node *y = NIL;
  while (x != NIL && k != x->key) {
    if (k < x->key)
      x = x->left;
    else if (k > x->key)
      x = x->right;
  }
  return x;
}

node *mini(node *u) {
  while (u != NIL) {
    u = u->left;
  }
  return u;
}

node *Successor(node *u) {
  if (u->right != NIL)
    return mini(u->right);
  node *y = u->parent;
  while (y != NIL && u == y->right) {
    u = y;
    y = y->parent;
  }
  return y;
}

void Delete(node *z) {
  node *y;
  node *x;
  if (z->left == NIL || z->right == NIL)
    y = z;
  else
    y = Successor(z);
  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 != z) {
    z->key = y->key;
  }
  free(y);
}

void Preorder(node *t) {
  if (t == NIL)
    return;
  printf(" %d", t->key);
  Preorder(t->left);
  Preorder(t->right);
}

void Inorder(node *t) {
  if (t == NIL)
    return;
  Inorder(t->left);
  printf(" %d", t->key);
  Inorder(t->right);
}

void Postorder(node *t) {
  if (t == NIL)
    return;
  Postorder(t->left);
  Postorder(t->right);
  printf(" %d", t->key);
}

int main() {
  int x, n;
  string st;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> st;
    if (st[0] == 'i') {
      cin >> x;
      insert(x);
    } else if (st[0] == 'p') {
      Inorder(root);
      cout << endl;
      Preorder(root);
      cout << endl;
    } else if (st[0] == 'f') {
      cin >> x;
      node *t = find(x);
      if (t == NIL)
        cout << "no";
      else
        cout << "yes";
      cout << endl;
    } else {
      cin >> x;
      Delete(find(x));
    }
  }
  return 0;
}