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

#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

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

class Tree {
  Node *root;

public:
  Tree() { init(); }

  Node *find(long long key) { return find(root, key); }

  Node *find(Node *t, int key) {
    if (t == NULL)
      return NULL;

    if (t->key == key) {
      return t;
    }

    if (key < t->key) {
      if (t->left != NULL) {
        return find(t->left, key);
      }
    } else {
      if (t->right != NULL) {
        return find(t->right, key);
      }
    }

    return NULL;
  }

  void insertToRoot(long long key) { insert(root, key); }

  void insert(Node *t, long long key) {
    Node *node = (Node *)malloc(sizeof(Node));
    node->key = key;
    node->parent = NULL;
    node->left = NULL;
    node->right = NULL;

    if (t == NULL) {
      if (root == NULL) {
        root = node;
        return;
      } else {
        t = root;
      }
    }

    Node *target = root;

    while (target != NULL) {
      if (node->key < target->key) {
        if (target->left == NULL) {
          target->left = node;
          node->parent = target;

          target = NULL;
        } else {
          target = target->left;
        }
      } else {
        if (target->right == NULL) {
          target->right = node;
          node->parent = target;

          target = NULL;
        } else {
          target = target->right;
        }
      }
    }
  }

  void deleteNode(long long key) {
    Node *node = find(key);

    if (node == NULL) {
      return;
    }

    if (node->left == NULL && node->right == NULL) {
      if (node->parent == NULL) {
      } else if (node->parent->key > node->key) {
        node->parent->left = NULL;
      } else {
        node->parent->right = NULL;
      }
      free(node);
    } else if (node->left != NULL && node->right == NULL) {
      if (node->parent == NULL) {
        root = node->left;
      } else if (node->parent->key > node->key) {
        node->parent->left = node->left;
      } else {
        node->parent->right = node->left;
      }

      node->left->parent = node->parent;
      free(node);
    } else if (node->left == NULL && node->right != NULL) {
      if (node->parent == NULL) {
        root = node->right;
      } else if (node->parent->key > node->key) {
        node->parent->left = node->right;
      } else {
        node->parent->right = node->right;
      }

      node->right->parent = node->parent;
      free(node);
    } else {
      Node *swapNode = node->right;

      while (swapNode->left != NULL) {
        swapNode = swapNode->left;
      }

      node->key = swapNode->key;

      if (swapNode->right != NULL) {
        swapNode->right->parent = swapNode->parent->left;
        swapNode->parent->left = swapNode->right;
      } else {
        if (swapNode->parent->key > swapNode->key) {
          swapNode->parent->left = NULL;
        } else {
          swapNode->parent->right = NULL;
        }
      }
      free(swapNode);
    }
  }

  void init() { root = NULL; }

  void print() {
    printInorder(root);
    cout << endl;
    printPreorder(root);
    cout << endl;
  }

  void printPreorder(Node *u) {
    if (u == NULL)
      return;

    cout << " " << u->key;

    if (u->left != NULL)
      printPreorder(u->left);

    if (u->right != NULL)
      printPreorder(u->right);
  }

  void printInorder(Node *u) {
    if (u == NULL)
      return;

    if (u->left != NULL)
      printInorder(u->left);

    cout << " " << u->key;

    if (u->right != NULL)
      printInorder(u->right);
  }
};

int main() {
  int M;

  cin >> M;
  Tree *tree = new Tree();

  for (int i = 0; i < M; i++) {
    string message;
    cin >> message;

    if (message == "insert") {
      long long key;
      cin >> key;
      tree->insertToRoot(key);
    } else if (message == "print") {
      tree->print();
    } else if (message == "find") {
      long long key = 0;
      cin >> key;

      if (tree->find(key)) {
        cout << "yes" << endl;
      } else {
        cout << "no" << endl;
      }
    } else if (message == "delete") {
      long long key = 0;
      cin >> key;
      tree->deleteNode(key);
    }
  }

  /*
          Node *n = tree->find(1);
          cout << n->key;
          cout << n->parent->key;
          cout << n->parent->right->key;
  */
  return 0;
}


Error: 0

Fixed submission:

#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

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

class Tree {
  Node *root;

public:
  Tree() { init(); }

  Node *find(int key) { return find(root, key); }

  Node *find(Node *t, int key) {
    if (t == NULL)
      return NULL;

    if (t->key == key) {
      return t;
    }

    if (key < t->key) {
      if (t->left != NULL) {
        return find(t->left, key);
      }
    } else {
      if (t->right != NULL) {
        return find(t->right, key);
      }
    }

    return NULL;
  }

  void insertToRoot(int key) { insert(root, key); }

  void insert(Node *t, int key) {
    Node *node = (Node *)malloc(sizeof(Node));
    node->key = key;
    node->parent = NULL;
    node->left = NULL;
    node->right = NULL;

    if (t == NULL) {
      if (root == NULL) {
        root = node;
        return;
      } else {
        t = root;
      }
    }

    Node *target = root;

    while (target != NULL) {
      if (node->key < target->key) {
        if (target->left == NULL) {
          target->left = node;
          node->parent = target;

          target = NULL;
        } else {
          target = target->left;
        }
      } else {
        if (target->right == NULL) {
          target->right = node;
          node->parent = target;

          target = NULL;
        } else {
          target = target->right;
        }
      }
    }
  }

  void deleteNode(int key) {
    Node *node = find(key);

    if (node == NULL) {
      return;
    }

    if (node->left == NULL && node->right == NULL) {
      if (node->parent == NULL) {
      } else if (node->parent->key > node->key) {
        node->parent->left = NULL;
      } else {
        node->parent->right = NULL;
      }
      free(node);
    } else if (node->left != NULL && node->right == NULL) {
      if (node->parent == NULL) {
        root = node->left;
      } else if (node->parent->key > node->key) {
        node->parent->left = node->left;
      } else {
        node->parent->right = node->left;
      }

      node->left->parent = node->parent;
      free(node);
    } else if (node->left == NULL && node->right != NULL) {
      if (node->parent == NULL) {
        root = node->right;
      } else if (node->parent->key > node->key) {
        node->parent->left = node->right;
      } else {
        node->parent->right = node->right;
      }

      node->right->parent = node->parent;
      free(node);
    } else {
      Node *swapNode = node->right;

      while (swapNode->left != NULL) {
        swapNode = swapNode->left;
      }

      node->key = swapNode->key;

      if (swapNode->right != NULL) {
        swapNode->right->parent = swapNode->parent->left;
        swapNode->parent->left = swapNode->right;
      } else {
        swapNode->parent->left = NULL;
      }
      free(swapNode);
    }
  }

  void init() { root = NULL; }

  void print() {
    printInorder(root);
    cout << endl;
    printPreorder(root);
    cout << endl;
  }

  void printPreorder(Node *u) {
    if (u == NULL)
      return;

    cout << " " << u->key;

    if (u->left != NULL)
      printPreorder(u->left);

    if (u->right != NULL)
      printPreorder(u->right);
  }

  void printInorder(Node *u) {
    if (u == NULL)
      return;

    if (u->left != NULL)
      printInorder(u->left);

    cout << " " << u->key;

    if (u->right != NULL)
      printInorder(u->right);
  }
};

int main() {
  int M;

  cin >> M;
  Tree *tree = new Tree();

  for (int i = 0; i < M; i++) {
    string message;
    cin >> message;

    if (message == "insert") {
      int key;
      cin >> key;
      tree->insertToRoot(key);
    } else if (message == "print") {
      tree->print();
    } else if (message == "find") {
      int key = 0;
      cin >> key;

      if (tree->find(key)) {
        cout << "yes" << endl;
      } else {
        cout << "no" << endl;
      }
    } else if (message == "delete") {
      int key = 0;
      cin >> key;
      tree->deleteNode(key);
    }
  }
  return 0;
}