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

#include <iostream>
#include <set>
#include <vector>
using namespace std;

typedef long long ll;

struct Node {
  int k;
  Node *r, *l, *p;
};

set<int> d_s;

Node *T;
Node *NIL;

void insert(int k) {

  Node *x = T;
  Node *y = NIL;
  Node *z;
  z = new Node;
  z->k = k;
  z->l = z->r = NIL;

  while (x != NIL) {
    y = x;
    if (z->k > x->k) {
      x = x->r;
    } else if (z->k < x->k) {
      x = x->l;
    }
  }
  z->p = y;

  if (y == NIL) {
    T = z;
  } else if (z->k < y->k) {
    y->l = z;
  } else if (z->k > y->k) {
    y->r = z;
  }
}

void in_print(Node *r) {
  if (r == NIL)
    return;
  set<int>::iterator iti = d_s.find(r->k);
  in_print(r->l);
  cout << " " << r->k;
  in_print(r->r);
}

void pre_print(Node *r) {
  if (r == NIL)
    return;
  cout << " " << r->k;
  pre_print(r->l);
  pre_print(r->r);
}

Node *treeMinimum(Node *x) {
  while (x->l != NIL)
    x = x->l;
  return x;
}

Node *treeSuccessor(Node *x) {
  if (x->r != NIL)
    return treeMinimum(x->r);
  Node *y = x->p;
  while (y != NIL && x == y->r) {
    x = y;
    y = y->p;
  }
  return y;
}

void treeDelete(Node *z) {
  Node *y;
  Node *x;

  if (z->l == NIL || z->r == NIL)
    y = z;
  else
    y = treeSuccessor(z);

  if (y->l != NIL) {
    x = y->l;
  } else {
    x = y->r;
  }
  if (x != NIL)
    x->p = y->p;

  if (y->p == NIL) {
    T = x;
  } else {
    if (y == y->p->l)
      y->p->l = x;
    else
      y->p->r = x;
  }

  if (y != z)
    z->k = y->k;

  delete y;
}

Node *find(Node *u, int k) {
  while (u != NIL && k != u->k) {
    if (k < u->k)
      u = u->l;
    else
      u = u->r;
  }
  return u;
}

int main() {

  int n;
  set<int> s;
  cin >> n;

  for (int i = 0; i < n; i++) {
    string str;
    int k;
    cin >> str;

    if (str == "insert") {
      cin >> k;
      s.insert(k);
      insert(k);
    } else if (str == "print") {
      in_print(T);
      cout << endl;
      pre_print(T);
      cout << endl;
    } else if (str == "find") {
      cin >> k;
      set<int>::iterator iti = s.find(k);
      if (iti == s.end())
        cout << "no" << endl;
      else
        cout << "yes" << endl;
    } else if (str == "delete") {
      cin >> k;
      set<int>::iterator iti = s.find(k);
      if (iti != s.end())
        treeDelete(find(T, k));
      s.erase(k);
    }
  }
}

Error: -11

Fixed submission:

#include <iostream>
#include <set>
#include <vector>
using namespace std;

typedef long long ll;

struct Node {
  int k;
  Node *r, *l, *p;
};

set<int> d_s;

Node *T;
Node *NIL;

void insert(int key) {

  Node *x = T;
  Node *y = NIL;
  Node *z;
  z = new Node;
  z->k = key;
  z->l = z->r = NIL;

  while (x != NIL) {
    y = x;
    if (z->k > x->k) {
      x = x->r;
    } else if (z->k < x->k) {
      x = x->l;
    }
  }
  z->p = y;

  if (y == NIL) {
    T = z;
  } else if (z->k < y->k) {
    y->l = z;
  } else if (z->k > y->k) {
    y->r = z;
  }
}

void in_print(Node *r) {
  set<int>::iterator iti = d_s.find(r->k);
  if (r == NIL)
    return;
  in_print(r->l);
  if (iti == d_s.end())
    cout << " " << r->k;
  in_print(r->r);
}

void pre_print(Node *r) {
  set<int>::iterator iti = d_s.find(r->k);
  if (r == NIL)
    return;
  if (iti == d_s.end())
    cout << " " << r->k;
  pre_print(r->l);
  pre_print(r->r);
}

int main() {

  int n;
  set<int> s;
  cin >> n;

  for (int i = 0; i < n; i++) {
    string str;
    int k;
    cin >> str;

    if (str == "insert") {
      cin >> k;
      s.insert(k);
      insert(k);
      set<int>::iterator iti = d_s.find(k);
      if (d_s.end() != iti) {
        d_s.erase(k);
      }
    } else if (str == "print") {
      in_print(T);
      cout << endl;
      pre_print(T);
      cout << endl;
    } else if (str == "find") {
      cin >> k;
      set<int>::iterator iti = s.find(k);
      if (iti == s.end())
        cout << "no" << endl;
      else
        cout << "yes" << endl;
    } else if (str == "delete") {
      cin >> k;
      set<int>::iterator it = s.find(k);
      if (it != s.end())
        d_s.insert(k);
      s.erase(k);
    }
  }
}