Problem p02285 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
struct node {
  int key;
  node *left = NULL;
  node *right = NULL;
  node *parent = NULL;
  node(int k, node *l = NULL, node *r = NULL, node *p = NULL)
      : key(k), left(l), right(r), parent(p) {}
};
node *Tree;

void insert(int z) {
  node *y = NULL;
  node *x = Tree;
  while (x != NULL) {
    y = x;
    if (z < x->key)
      x = x->left;
    else
      x = x->right;
  }
  if (y == NULL)
    Tree = new node(z);
  else if (z < y->key)
    y->left = new node(z, NULL, NULL, y);
  else
    y->right = new node(z, NULL, NULL, y);
}

void midprint(node *root) {
  if (root == NULL)
    return;
  midprint(root->left);
  cout << " " << root->key;
  midprint(root->right);
}

void preprint(node *root) {
  if (root == NULL)
    return;
  cout << " " << root->key;
  preprint(root->left);
  preprint(root->right);
}

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

node *TreeSuccessor(node *x) {

  x = x->right;
  node *y = x;
  while (x != NULL && x->left == NULL)
    x = x->right;
  while (x != NULL && x->left != NULL)
    x = x->left;
  return x;
}

void del(int k) {
  node *z = find(k);
  if (z == NULL)
    return;

  node *y = NULL;
  if (z->left == NULL || z->right == NULL)
    y = z;
  else
    y = TreeSuccessor(z);

  node *x;
  if (y->left != NULL)
    x = y->left;
  else
    x = y->right;

  if (x != NULL)
    x->parent = y->parent;

  if (y->parent == NULL)
    Tree = x;
  else {
    if (y == y->parent->left)
      y->parent->left = x;
    else
      y->parent->right = x;
  }
  if (y != z)
    z->key = y->key;
  delete y;
}

void print(node *root) {
  midprint(root);
  cout << endl;
  preprint(root);
  cout << endl;
}

int main() {
  int n;
  cin >> n;
  while (n--) {
    string s;
    cin >> s;
    if (s[0] == 'i') {
      int k;
      cin >> k;
      insert(k);
    } else if (s[0] == 'p')
      print(Tree);
    else if (s[0] == 'f') {
      int k;
      cin >> k;
      node *ans = find(k);
      if (ans == NULL)
        cout << "no" << endl;
      else
        cout << "yes" << endl;
    } else if (s[0] == 'd') {
      int k;
      cin >> k;
      del(k);
    }
  }
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
struct node {
  int key;
  node *left = NULL;
  node *right = NULL;
  node *parent = NULL;
  node(int k, node *l = NULL, node *r = NULL, node *p = NULL)
      : key(k), left(l), right(r), parent(p) {}
};
node *Tree;

void insert(int z) {
  node *y = NULL;
  node *x = Tree;
  while (x != NULL) {
    y = x;
    if (z < x->key)
      x = x->left;
    else
      x = x->right;
  }
  if (y == NULL)
    Tree = new node(z);
  else if (z < y->key)
    y->left = new node(z, NULL, NULL, y);
  else
    y->right = new node(z, NULL, NULL, y);
}

void midprint(node *root) {
  if (root == NULL)
    return;
  midprint(root->left);
  cout << " " << root->key;
  midprint(root->right);
}

void preprint(node *root) {
  if (root == NULL)
    return;
  cout << " " << root->key;
  preprint(root->left);
  preprint(root->right);
}

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

node *TreeSuccessor(node *x) {

  x = x->right;
  while (x != NULL && x->left != NULL)
    x = x->left;
  return x;
}

void del(int k) {
  node *z = find(k);
  if (z == NULL)
    return;

  node *y = NULL;
  if (z->left == NULL || z->right == NULL)
    y = z;
  else
    y = TreeSuccessor(z);

  node *x;
  if (y->left != NULL)
    x = y->left;
  else
    x = y->right;

  if (x != NULL)
    x->parent = y->parent;

  if (y->parent == NULL)
    Tree = x;
  else {
    if (y == y->parent->left)
      y->parent->left = x;
    else
      y->parent->right = x;
  }
  if (y != z)
    z->key = y->key;
  delete y;
}

void print(node *root) {
  midprint(root);
  cout << endl;
  preprint(root);
  cout << endl;
}

int main() {
  int n;
  cin >> n;
  while (n--) {
    string s;
    cin >> s;
    if (s[0] == 'i') {
      int k;
      cin >> k;
      insert(k);
    } else if (s[0] == 'p')
      print(Tree);
    else if (s[0] == 'f') {
      int k;
      cin >> k;
      node *ans = find(k);
      if (ans == NULL)
        cout << "no" << endl;
      else
        cout << "yes" << endl;
    } else if (s[0] == 'd') {
      int k;
      cin >> k;
      del(k);
    }
  }
  return 0;
}