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

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

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

Node *root, *nil;

void insertNode(long k) {
  Node *y = nil;
  Node *x = root;

  Node *z;
  z = (Node *)malloc(sizeof(Node));
  z->key = k;
  z->parent = nil;
  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 in(Node *n) {
  if (n == nil)
    return;
  in(n->left);
  cout << ' ' << n->key;
  in(n->right);
}

void pre(Node *n) {
  if (n == nil)
    return;
  cout << ' ' << n->key;
  pre(n->left);
  pre(n->right);
}

Node *findNode(long k) {
  Node *n = root;

  while (n != nil && n->key != k) {
    if (k < n->key)
      n = n->left;
    else
      n = n->right;
  }

  return n;
}

Node *minNodeInRight(Node *n) {
  while (n->left != nil)
    n = n->left;
  return n;
}

Node *getSuccessor(Node *n) {
  if (n->right != nil)
    return minNodeInRight(n->right);

  Node *parentN = n->parent;

  while (parentN != nil && n == parentN->right) {
    n = parentN;
    parentN = parentN->parent;
  }

  return parentN;
}

void deleteNode(Node *n) {
  if (n == nil)
    return;

  Node *childN;
  Node *deleteN;

  if (n->left == nil || n->right == nil)
    deleteN = n;
  else
    deleteN = getSuccessor(n);

  // decide child
  if (deleteN->left != nil)
    childN = deleteN->left;
  else
    childN = deleteN->right;

  if (childN != nil)
    childN->parent = deleteN->parent;

  if (deleteN->parent == nil) {
    root = childN;
  } else {
    if (deleteN->parent->left == deleteN)
      deleteN->parent->left = childN;
    else
      deleteN->parent->right = childN;
  }

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

  free(deleteN);
}

void print() {
  in(root);
  cout << endl;
  pre(root);
  cout << endl;
}

int main() {

  long l;
  cin >> l;

  while (l--) {
    string s;
    cin >> s;

    if (s == "insert") {
      long k;
      cin >> k;
      insertNode(k);
    } else if (s == "find") {
      long k;
      cin >> k;
      if (findNode(k) == nil)
        cout << "no";
      else
        cout << "yes";
      cout << endl;
    } else if (s == "delete") {
      long k;
      cin >> k;
      deleteNode(findNode(k));
    } else {
      print();
    }
  }

  return 0;
}

Error: 0

Fixed submission:

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

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

Node *root, *nil;

void insertNode(long k) {
  Node *y = nil;
  Node *x = root;

  Node *z;
  z = (Node *)malloc(sizeof(Node));
  z->key = k;
  z->parent = nil;
  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 in(Node *n) {
  if (n == nil)
    return;
  in(n->left);
  cout << ' ' << n->key;
  in(n->right);
}

void pre(Node *n) {
  if (n == nil)
    return;
  cout << ' ' << n->key;
  pre(n->left);
  pre(n->right);
}

void findNode(long k) {
  Node *x = root;
  while (x != nil && x->key != k) {
    if (k < x->key)
      x = x->left;
    else
      x = x->right;
  }
  if (x == nil)
    cout << "no";
  else
    cout << "yes";
  cout << endl;
}

Node *getSuccessor(Node *n) {
  if (n == nil)
    return nil;

  Node *tmp;
  tmp->left = n->left;
  tmp->right = n->right;
  tmp->key = n->key;

  while (tmp->left != nil)
    tmp = tmp->left;
  if (tmp->key != n->key)
    return tmp;

  while (tmp->right != nil)
    tmp = tmp->left;
  if (tmp->key != n->key)
    return tmp;

  return nil;
}

void deleteNode(long k) {
  Node *x = root;

  while (x != nil && x->key != k) {
    if (k < x->key)
      x = x->left;
    else
      x = x->right;
  }

  if (x == nil)
    return;

  if (x->key == k) {
    if (x->left == nil && x->right == nil) {
      // no child
      if (x->parent->left == x)
        x->parent->left = nil;
      else
        x->parent->right = nil;
    } else if ((x->left == nil && x->right != nil) ||
               (x->left != nil && x->right == nil)) {
      // one child
      Node *xChild = (x->left == nil ? x->right : x->left);
      char xPos = (x->parent->left == x ? 'L' : 'R');

      xChild->parent = x->parent;

      if (xPos == 'L')
        x->parent->left = xChild;
      else
        x->parent->right = xChild;
    } else {
      // two children
      Node *sucNode = getSuccessor(x->right);
      x->key = sucNode->key;

      if (sucNode->left == nil && sucNode->right == nil) {
        // successor has no child
        if (sucNode->parent->left == x)
          sucNode->parent->left = nil;
        else
          sucNode->parent->right = nil;
      } else if ((sucNode->left == nil && sucNode->right != nil) ||
                 (sucNode->left != nil && sucNode->right == nil)) {
        // successor has one child
        Node *xChild = (sucNode->left == nil ? sucNode->right : sucNode->left);
        char xPos = (sucNode->parent->left == x ? 'L' : 'R');

        xChild->parent = sucNode->parent;

        if (xPos == 'L')
          sucNode->parent->left = xChild;
        else
          sucNode->parent->right = xChild;
      }
    }
  }
}

void print() {
  in(root);
  cout << endl;
  pre(root);
  cout << endl;
}

int main() {

  long l;
  cin >> l;

  while (l--) {
    string s;
    cin >> s;

    if (s == "insert") {
      long k;
      cin >> k;
      insertNode(k);
    } else if (s == "find") {
      long k;
      cin >> k;
      findNode(k);
    } else if (s == "delete") {
      long k;
      cin >> k;
      deleteNode(k);
    } else {
      print();
    }
  }

  return 0;
}