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

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

using namespace std;
struct Node {
  int value;
  Node *left;
  Node *right;
};

struct Node *NIL;

class BinarySearchTree {
private:
  Node *root;

public:
  BinarySearchTree() { root = NIL; }
  void insert(int value) {
    Node *node = (Node *)malloc(sizeof(Node));
    node->value = value;
    node->right = NIL;
    node->left = NIL;
    Node *x = root;
    Node *y = NIL;
    while (x != NIL) {
      y = x;
      if (node->value < x->value) {
        x = x->left;
      } else {
        x = x->right;
      }
    }
    if (y == NIL)
      root = node;
    else if (node->value < y->value)
      y->left = node;
    else
      y->right = node;
  }
  bool find(int value) {
    Node *node = root;
    while (node != NIL) {
      if (node->value == value)
        return true;
      else if (value < node->value)
        node = node->left;
      else
        node = node->right;
    }
    return false;
  }
  void print() {
    inorderWalk(root);
    cout << endl;
    preorderWalk(root);
    cout << endl;
  }
  void inorderWalk(Node *node) {
    if (node == NIL)
      return;
    inorderWalk(node->left);
    cout << ' ' << node->value;
    inorderWalk(node->right);
  }
  void preorderWalk(Node *node) {
    if (node == NIL)
      return;
    cout << ' ' << node->value;
    preorderWalk(node->left);
    preorderWalk(node->right);
  }
};

int main() {
  int n;
  cin >> n;
  string com;
  BinarySearchTree T;
  for (int i = 0; i < n; i++) {
    cin >> com;
    if (com == "insert") {
      int num;
      cin >> num;
      T.insert(num);
    } else if (com == "print") {
      T.print();
    } else if (com == "find") {
      int num;
      cin >> num;
      cout << (T.find(num) ? "yes" : "no") << endl;
    }
  }
}


Error: 0

Fixed submission:

#include <iostream>
#include <string>
#define NIL -1

using namespace std;

int x = NIL, root_id = NIL;
struct Node {
  int parent = -1, left = -1, right = -1;
};

Node T[500009];

void inorder(int id) {
  if (id == -1)
    return;
  inorder(T[id].left);
  cout << " " << id;
  inorder(T[id].right);
}

void preorder(int id) {
  if (id == -1)
    return;
  cout << " " << id;
  preorder(T[id].left);
  preorder(T[id].right);
}

void print(int root_id) {
  inorder(root_id);
  cout << endl;
  preorder(root_id);
  cout << endl;
}

void insert(int z) {
  int y = NIL;
  x = root_id;
  while (x != NIL) {
    y = x;
    if (z < x)
      x = T[x].left;
    else
      x = T[x].right;
  }
  T[z].parent = y;

  if (y == NIL)
    root_id = z;
  else if (z < y)
    T[y].left = z;
  else
    T[y].right = z;
}

void find(int id) {
  x = root_id;
  while (1) {
    if (x == id) {
      cout << "yes" << endl;
      break;
    }
    if (x == NIL) {
      cout << "no" << endl;
      break;
    } else if (id > x) {
      x = T[x].right;
    } else if (id < x) {
      x = T[x].left;
    }
  }
}

int main() {
  int n, ins_num;
  string ope;
  cin >> n;
  for (int i = 0; i <= n - 1; i++) {
    cin >> ope;
    if (ope == "print")
      print(root_id);
    else if (ope == "insert") {
      cin >> ins_num;
      insert(ins_num);
    } else if (ope == "find") {
      cin >> ins_num;
      find(ins_num);
    }
  }
}