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

#include <bits/stdc++.h>
#define INF 2000000001
using namespace std;

typedef struct node {
  node *p, *l, *r;
  int key;
} Node;

typedef Node *Nodepointer;

Nodepointer T;

void insert(int z) {
  Nodepointer t = (Node *)malloc(sizeof(Node));
  t->l = NULL;
  t->r = NULL;
  t->key = z;
  if (T->key == INF) {
    T->key = z;
    return;
  }
  Nodepointer y = NULL, x = T;
  while (x != NULL) {
    y = x;
    if (t->key < x->key)
      x = x->l;
    else
      x = x->r;
  }
  t->p = y;
  if (y == NULL)
    T = t;
  else if (t->key < y->key)
    y->l = t;
  else
    y->r = t;
}

bool find(int key) {
  Nodepointer x = T;
  while (x != NULL) {
    if (x->key == key)
      return true;
    if (key < x->key)
      x = x->l;
    else
      x = x->r;
  }
  return false;
}

void print(Nodepointer x, int y) {
  if (y)
    cout << ' ' << x->key;
  if (x->l != NULL)
    print(x->l, y);
  if (!y)
    cout << ' ' << x->key;
  if (x->r != NULL)
    print(x->r, y);
}

Nodepointer treeMinimum(Nodepointer x) {
  while (x->l != NULL)
    x = x->l;
  return x;
}

Nodepointer treeSuccessor(Nodepointer x) {
  if (x->r != NULL)
    return treeMinimum(x->r);
  Nodepointer y = x->p;
  while (y != NULL && x == y->r) {
    x = y;
    y = y->p;
  }
  return y;
}

void delNode(int key) {
  Nodepointer z;
  Nodepointer x = T, y;
  while (x != NULL) {
    if (x->key == key)
      break;
    if (key < x->key)
      x = x->l;
    else
      x = x->r;
  }
  if (x->key != key)
    return;
  z = x;
  if (z->l == NULL || z->r == NULL)
    y = z;
  else
    y = treeSuccessor(z);
  if (y->l != NULL)
    x = y->l;
  else
    x = y->r;
  if (x != NULL)
    x->p = y->p;
  if (y->p == NULL)
    T = x;
  else if (y == y->p->l)
    y->p->l = x;
  else
    y->p->r = x;
  if (y != z)
    z->key = y->key;
}

int main() {
  string s;
  int n, z;
  cin >> n;
  T = (Node *)malloc(sizeof(Node));
  T->p = NULL;
  T->l = NULL;
  T->r = NULL;
  T->key = INF;
  while (n--) {
    cin >> s;
    if (s == "insert")
      cin >> z, insert(z);
    else if (T->key != INF && s == "delete")
      cin >> z, delNode(z);
    else if (T->key != INF && s == "find") {
      cin >> z;
      if (find(z))
        cout << "yes" << endl;
      else
        cout << "no" << endl;
    } else if (T->key != INF) {
      print(T, 0);
      cout << endl;
      print(T, 1);
      cout << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define INF 2000000001
using namespace std;

typedef struct node {
  node *p, *l, *r;
  int key;
} Node;

typedef Node *Nodepointer;

Nodepointer T;

void insert(int z) {
  Nodepointer t = (Node *)malloc(sizeof(Node));
  t->l = NULL;
  t->r = NULL;
  t->key = z;
  if (T->key == INF) {
    T->key = z;
    return;
  }
  Nodepointer y = NULL, x = T;
  while (x != NULL) {
    y = x;
    if (t->key < x->key)
      x = x->l;
    else
      x = x->r;
  }
  t->p = y;
  if (y == NULL)
    T = t;
  else if (t->key < y->key)
    y->l = t;
  else
    y->r = t;
}

bool find(int key) {
  Nodepointer x = T;
  while (x != NULL) {
    if (x->key == key)
      return true;
    if (key < x->key)
      x = x->l;
    else
      x = x->r;
  }
  return false;
}

void print(Nodepointer x, int y) {
  if (y)
    cout << ' ' << x->key;
  if (x->l != NULL)
    print(x->l, y);
  if (!y)
    cout << ' ' << x->key;
  if (x->r != NULL)
    print(x->r, y);
}

Nodepointer treeMinimum(Nodepointer x) {
  while (x->l != NULL)
    x = x->l;
  return x;
}

Nodepointer treeSuccessor(Nodepointer x) {
  if (x->r != NULL)
    return treeMinimum(x->r);
  Nodepointer y = x->p;
  while (y != NULL && x == y->r) {
    x = y;
    y = y->p;
  }
  return y;
}

void delNode(int key) {
  Nodepointer x = T;
  while (x != NULL) {
    if (x->key == key)
      break;
    if (key < x->key)
      x = x->l;
    else
      x = x->r;
  }
  if (x->key != key)
    return;
  if (x->l == NULL && x->r == NULL) {
    if (x->p->l == x)
      x->p->l = NULL;
    if (x->p->r == x)
      x->p->r = NULL;
  } else if (x->l != NULL && x->r != NULL) {
    Nodepointer y = treeSuccessor(x);
    x->key = y->key;
    if (x->p->l == x) {
      x->p->l = x->l;
      x->l->p = x->p;
    }
    if (x->p->r == x) {
      x->p->r = x->l;
      x->l->p = x->p;
    }
  } else if (x->l != NULL) {
    if (x->p->l == x) {
      x->p->l = x->l;
      x->l->p = x->p;
    }
    if (x->p->r == x) {
      x->p->r = x->l;
      x->l->p = x->p;
    }
  } else {
    if (x->p->l == x) {
      x->p->l = x->r;
      x->r->p = x->p;
    }
    if (x->p->r == x) {
      x->p->r = x->r;
      x->r->p = x->p;
    }
  }
}

int main() {
  string s;
  int n, z;
  cin >> n;
  T = (Node *)malloc(sizeof(Node));
  T->p = NULL;
  T->l = NULL;
  T->r = NULL;
  T->key = INF;
  while (n--) {
    cin >> s;
    if (s == "insert")
      cin >> z, insert(z);
    else if (T->key != INF && s == "delete")
      cin >> z, delNode(z);
    else if (T->key != INF && s == "find") {
      cin >> z;
      if (find(z))
        cout << "yes" << endl;
      else
        cout << "no" << endl;
    } else if (T->key != INF) {
      print(T, 0);
      cout << endl;
      print(T, 1);
      cout << endl;
    }
  }
  return 0;
}