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

#include <cstdio>
#include <iostream>
#include <string>

using namespace std;

struct Node {
  int key;
  Node *parent;
  Node *left;
  Node *right;
  Node() { parent = left = right = NULL; }
};

class Tree {
  Node *root = NULL;

public:
  void insert(Node *n) {
    Node *y = NULL;
    Node *x = root;
    while (x != NULL) {
      y = x;
      if (n->key < x->key) {
        x = x->left;
      } else {
        x = x->right;
      }
    }
    if (y == NULL) {
      root = n;
    } else if (n->key < y->key) {
      y->left = n;
      n->parent = y;
    } else {
      y->right = n;
      n->parent = y;
    }
  }

  void print() {
    print_inorder(root);
    printf("\\n");
    print_preorder(root);
    printf("\\n");
  }

  void print_preorder(Node *n) {
    if (n == NULL)
      return;
    printf(" %d", n->key);
    print_preorder(n->left);
    print_preorder(n->right);
  }
  void print_inorder(Node *n) {
    if (n == NULL)
      return;
    print_inorder(n->left);
    printf(" %d", n->key);
    print_inorder(n->right);
  }

  bool has(int val) {
    if (find(val) != NULL) {
      return true;
    } else {
      return false;
    }
  }

  Node *find(int val) {
    Node *x;
    x = root;
    while (1) {
      if (x == NULL) {
        return NULL;
      } else if (x->key == val) {
        return x;
      } else if (val < x->key) {
        x = x->left;
      } else {
        x = x->right;
      }
    }
  }

  void del(Node *x) {
    if (x->left == NULL && x->right == NULL) {
      if (x->parent->left == x) {
        x->parent->left = NULL;
        delete x;
      } else {
        x->parent->right = NULL;
        delete x;
      }
    } else if (x->left == NULL) {
      if (x->parent->left == x) {
        x->parent->left = x->right;
        x->right->parent = x->parent;
        delete x;
      } else {
        x->parent->right = x->right;
        x->right->parent = x->parent;
        delete x;
      }
    } else if (x->right == NULL) {
      if (x->parent->left == x) {
        x->parent->left = x->left;
        x->left->parent = x->parent;
        delete x;
      } else {
        x->parent->right = x->left;
        x->left->parent = x->parent;
        delete x;
      }
    } else {
      Node *y = x->right;
      while (y->left != NULL) {
        y = y->left;
      }
      x->key = y->key;
      del(y);
    }
  }

  void del(int val) {
    Node *x = find(val);
    del(x);
  }
};

int main() {
  int n;
  cin >> n;
  Tree t;
  for (int i = 0; i < n; i++) {
    string ope;
    cin >> ope;
    if (ope == "insert") {
      int k;
      cin >> k;
      Node *node = new Node();
      node->key = k;
      t.insert(node);
    } else if (ope == "print") {
      t.print();
    } else if (ope == "find") {
      int v;
      cin >> v;
      if (t.find(v)) {
        cout << "yes" << endl;
      } else {
        cout << "no" << endl;
      }
    } else if (ope == "delete") {
      int v;
      cin >> v;
      t.del(v);
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>
#include <string>

using namespace std;

struct Node {
  int key;
  Node *parent;
  Node *left;
  Node *right;
  Node() { parent = left = right = NULL; }
};

class Tree {
  Node *root = NULL;

public:
  void insert(Node *n) {
    Node *y = NULL;
    Node *x = root;
    while (x != NULL) {
      y = x;
      if (n->key < x->key) {
        x = x->left;
      } else {
        x = x->right;
      }
    }
    if (y == NULL) {
      root = n;
    } else if (n->key < y->key) {
      y->left = n;
      n->parent = y;
    } else {
      y->right = n;
      n->parent = y;
    }
  }

  void print() {
    print_inorder(root);
    printf("\\n");
    print_preorder(root);
    printf("\\n");
  }

  void print_preorder(Node *n) {
    if (n == NULL)
      return;
    printf(" %d", n->key);
    print_preorder(n->left);
    print_preorder(n->right);
  }
  void print_inorder(Node *n) {
    if (n == NULL)
      return;
    print_inorder(n->left);
    printf(" %d", n->key);
    print_inorder(n->right);
  }

  bool has(int val) {
    if (find(val) != NULL) {
      return true;
    } else {
      return false;
    }
  }

  Node *find(int val) {
    Node *x;
    x = root;
    while (1) {
      if (x == NULL) {
        return NULL;
      } else if (x->key == val) {
        return x;
      } else if (val < x->key) {
        x = x->left;
      } else {
        x = x->right;
      }
    }
  }

  void del(Node *x) {
    if (x->left == NULL && x->right == NULL) {
      if (x->parent->left == x) {
        x->parent->left = NULL;
        delete x;
      } else {
        x->parent->right = NULL;
        delete x;
      }
    } else if (x->left == NULL) {
      if (x->parent->left == x) {
        x->parent->left = x->right;
        x->left->parent = x->parent;
        delete x;
      } else {
        x->parent->right = x->right;
        x->right->parent = x->parent;
        delete x;
      }
    } else if (x->right == NULL) {
      if (x->parent->left == x) {
        x->parent->left = x->left;
        x->left->parent = x->parent;
        delete x;
      } else {
        x->parent->right = x->left;
        x->right->parent = x->parent;
        delete x;
      }
    } else {
      if (x->left != NULL) {
        x->key = x->left->key;
        del(x->left);
      } else {
        x->key = x->right->key;
        del(x->right);
      }
    }
  }

  void del(int val) {
    Node *x = find(val);
    del(x);
  }
};

int main() {
  int n;
  cin >> n;
  Tree t;
  for (int i = 0; i < n; i++) {
    string ope;
    cin >> ope;
    if (ope == "insert") {
      int k;
      cin >> k;
      Node *node = new Node();
      node->key = k;
      t.insert(node);
    } else if (ope == "print") {
      t.print();
    } else if (ope == "find") {
      int v;
      cin >> v;
      if (t.find(v)) {
        cout << "yes" << endl;
      } else {
        cout << "no" << endl;
      }
    } else if (ope == "delete") {
      int v;
      cin >> v;
      t.del(v);
    }
  }
  return 0;
}