Problem p02285 (C++)

Buggy submission (status: Runtime Error):
// Ref : https://book.mynavi.jp/ec/products/detail/id=35408

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

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

// These pointer adress is NULL until first store (root == NIL).
// NIL adress is permanently NULL for defining empty node.
Node *root, *NIL;

void myInsert(int k) {
  Node *y = NIL;  // parent
  Node *x = root; // comparison
  Node *z;        // insertion

  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;
    }
  }
}

void inOrderPrint(Node *n) {
  if (n == NIL)
    return;
  inOrderPrint(n->left);
  cout << " " << n->key;
  inOrderPrint(n->right);
}

void preOrderPrint(Node *n) {
  if (n == NIL)
    return;
  cout << " " << n->key;
  preOrderPrint(n->left);
  preOrderPrint(n->right);
}

Node *myFind(Node *n, int k) {
  while (n != NIL && n->key != k) {
    if (k > n->key)
      n = n->right;
    else
      n = n->left;
  }
  return n;
}

void myDelete(Node *n) {
  Node *deletion, *children, *parent;

  if (n->left == NIL ||
      n->right == NIL) { // deleted node has 0 or 1 children node.
    deletion = n;
  } else { // deleted node has 2 children node;
    deletion = n->right;
    while (deletion->left != NIL) {
      deletion = deletion->left;
    }
  }

  if (deletion->left != NIL)
    children = deletion->left;
  else
    children = deletion->right;

  parent = deletion->parent;
  if (parent == NIL)
    root = n;
  else if (parent->left == deletion)
    parent->left = children;
  else
    parent->right = children;

  if (children != NIL)
    children->parent = parent;

  if (deletion != n)
    n->key = deletion->key;

  free(n);
}

int main() {
  int n, x;
  char com[7];
  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> com;
    if (com[0] == 'i') {
      cin >> x;
      myInsert(x);
    } else if (com[0] == 'p') {
      inOrderPrint(root);
      cout << endl;
      preOrderPrint(root);
      cout << endl;
    } else if (com[0] == 'f') {
      cin >> x;
      if (myFind(root, x) == NIL)
        cout << "no";
      else
        cout << "yes";
      cout << endl;
    } else if (com[0] == 'd') {
      cin >> x;
      myDelete(myFind(root, x));
    }
  }

  return 0;
}


Fixed submission:
// Ref : https://book.mynavi.jp/ec/products/detail/id=35408

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

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

// These pointer adress is NULL until first store (root == NIL).
// NIL adress is permanently NULL for defining empty node.
Node *root, *NIL;

void myInsert(int k) {
  Node *y = NIL;  // parent
  Node *x = root; // comparison
  Node *z;        // insertion

  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;
    }
  }
}

void inOrderPrint(Node *n) {
  if (n == NIL)
    return;
  inOrderPrint(n->left);
  cout << " " << n->key;
  inOrderPrint(n->right);
}

void preOrderPrint(Node *n) {
  if (n == NIL)
    return;
  cout << " " << n->key;
  preOrderPrint(n->left);
  preOrderPrint(n->right);
}

Node *myFind(Node *n, int k) {
  while (n != NIL && n->key != k) {
    if (k > n->key)
      n = n->right;
    else
      n = n->left;
  }
  return n;
}

void myDelete(Node *n) {
  Node *deletion, *children, *parent;

  if (n->left == NIL ||
      n->right == NIL) { // deleted node has 0 or 1 children node.
    deletion = n;
  } else { // deleted node has 2 children node;
    deletion = n->right;
    while (deletion->left != NIL) {
      deletion = deletion->left;
    }
  }

  if (deletion->left != NIL)
    children = deletion->left;
  else
    children = deletion->right;

  parent = deletion->parent;
  if (parent == NIL)
    root = n;
  else if (parent->left == deletion)
    parent->left = children;
  else
    parent->right = children;

  if (children != NIL)
    children->parent = parent;

  if (deletion != n)
    n->key = deletion->key;

  free(deletion);
}

int main() {
  int n, x;
  char com[7];
  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> com;
    if (com[0] == 'i') {
      cin >> x;
      myInsert(x);
    } else if (com[0] == 'p') {
      inOrderPrint(root);
      cout << endl;
      preOrderPrint(root);
      cout << endl;
    } else if (com[0] == 'f') {
      cin >> x;
      if (myFind(root, x) == NIL)
        cout << "no";
      else
        cout << "yes";
      cout << endl;
    } else if (com[0] == 'd') {
      cin >> x;
      myDelete(myFind(root, x));
    }
  }

  return 0;
}