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

#include <iostream>
#include <stdio.h>

using namespace std;

#define BUF_SIZE 22

struct Node {
  Node() {
    left_child = right_child = parent = 0;
    data = 0;
  }
  Node *left_child, *right_child, *parent;
  long long data;
};

class Tree {
public:
  Tree() {
    root = new Node();
    num_of_node = 0;
  }
  void insert(long long insert_data) {
    if (num_of_node == 0) {
      root->data = insert_data;
      num_of_node++;
    } else {
      Node *tmp = root;
      while (true) {
        if (tmp->data < insert_data) {
          if (tmp->right_child == 0) {
            tmp->right_child = new Node();
            tmp->right_child->data = insert_data;
            tmp->right_child->parent = tmp;
            num_of_node++;
            break;
          } else {
            tmp = tmp->right_child;
          }
        } else {
          if (tmp->left_child == 0) {
            tmp->left_child = new Node();
            tmp->left_child->data = insert_data;
            tmp->left_child->parent = tmp;
            num_of_node++;
            break;
          } else {
            tmp = tmp->left_child;
          }
        }
      }
    }
  }

  bool find(long long data) {
    if (num_of_node == 0) {
      return false;
    } else {
      Node *tmp = root;
      while (true) {
        if (tmp->data < data) {
          if (tmp->right_child == 0) {
            return false;
          } else {
            tmp = tmp->right_child;
          }
        } else if (tmp->data > data) {
          if (tmp->left_child == 0) {
            return false;
          } else {
            tmp = tmp->left_child;
          }
        } else {
          return true;
        }
      }
    }
  }

  void deleteNode(long long delete_data) {
    if (num_of_node == 0) {
      return;
    } else {
      doDELETE(root, delete_data);
    }
  }

  void print() {
    inORDER(root);
    printf("\\n");
    preORDER(root);
    printf("\\n");
  }

private:
  Node *root;
  int num_of_node;

private:
  Node *findNextNode(Node *node) {
    Node *tmp = node;
    while (tmp->left_child != 0)
      tmp = tmp->left_child;
    return tmp;
  }

  void doDELETE(Node *node, long long delete_data) {

    Node *tmp = node;
    while (true) {
      if (tmp->data < delete_data) {
        if (tmp->right_child == 0) {
          // Do nothing
          break;
        } else {
          tmp = tmp->right_child;
        }
      } else if (tmp->data > delete_data) {
        if (tmp->left_child == 0) {
          // Do nothing
          break;
        } else {
          tmp = tmp->left_child;
        }
      } else {
        if (tmp->left_child == 0 && tmp->right_child == 0) {
          if (tmp != root) {
            if (tmp->parent->data < tmp->data) {
              tmp->parent->right_child = 0;
            } else if (tmp->parent->data > tmp->data) {
              tmp->parent->left_child = 0;
            } else {
              tmp->parent->right_child = 0;
            }
          }
          delete tmp;
          num_of_node--;
          break;
        } else if (tmp->left_child == 0 && tmp->right_child != 0) {
          if (tmp != root) {
            if (tmp->parent->data < tmp->data) {
              tmp->parent->right_child = tmp->right_child;
            } else if (tmp->parent->data > tmp->data) {
              tmp->parent->left_child = tmp->right_child;
            } else {
              tmp->parent->right_child = 0;
            }
            tmp->right_child->parent = tmp->parent;
            delete tmp;
            num_of_node--;
          } else {
            printf("Minhi!\\n");
            // not defined
          }
          break;
        } else if (tmp->left_child != 0 && tmp->right_child == 0) {
          if (tmp != root) {
            if (tmp->parent->data < tmp->data) {
              tmp->parent->right_child = tmp->left_child;
            } else if (tmp->parent->data > tmp->data) {
              tmp->parent->left_child = tmp->left_child;
            } else {
              tmp->parent->right_child = 0;
            }
            tmp->left_child->parent = tmp->parent;
            delete tmp;
            num_of_node--;
          } else {
            printf("Minhi!\\n");
            // not defined
          }
          break;
        } else {
          Node *next_node = findNextNode(tmp->right_child);
          tmp->data = next_node->data;
          doDELETE(tmp->right_child, next_node->data);
          break;
        }
      }
    }
  }

  void preORDER(Node *node) {
    printf(" %lld", node->data);
    if (node->left_child != 0)
      preORDER(node->left_child);
    if (node->right_child != 0)
      preORDER(node->right_child);
  }

  void inORDER(Node *node) {
    if (node->left_child != 0)
      inORDER(node->left_child);
    printf(" %lld", node->data);
    if (node->right_child != 0)
      inORDER(node->right_child);
  }
};

int main() {
  int m;
  long long insert_data, search_data, delete_data;
  long long minusFLG;
  char buf[BUF_SIZE];
  Tree tree;

  scanf("%d", &m);
  getchar();

  for (int i = 0; i < m; i++) {
    minusFLG = 1;
    fgets(buf, BUF_SIZE, stdin);
    if (buf[0] == 'i') {
      insert_data = 0;
      for (int k = 0; buf[k] != '\\0'; k++) {
        if (buf[k] == '-')
          minusFLG = -1;
        if (buf[k] >= '0' && buf[k] <= '9') {
          insert_data = 10 * insert_data + (buf[k] - '0');
        }
      }
      tree.insert(minusFLG * insert_data);
    } else if (buf[0] == 'p') {
      tree.print();
    } else if (buf[0] == 'f') {
      search_data = 0;
      for (int k = 0; buf[k] != '\\0'; k++) {
        if (buf[k] == '-')
          minusFLG = -1;
        if (buf[k] >= '0' && buf[k] <= '9') {
          search_data = 10 * search_data + (buf[k] - '0');
        }
      }
      if (tree.find(minusFLG * search_data)) {
        printf("yes\\n");
      } else {
        printf("no\\n");
      }
    } else {
      delete_data = 0;
      for (int k = 0; buf[k] != '\\0'; k++) {
        if (buf[k] == '-')
          minusFLG = -1;
        if (buf[k] >= '0' && buf[k] <= '9') {
          delete_data = 10 * delete_data + (buf[k] - '0');
        }
      }
      tree.deleteNode(minusFLG * delete_data);
    }
  }
}

Error: 0

Fixed submission:

#include <iostream>
#include <stdio.h>

using namespace std;

#define BUF_SIZE 22

struct Node {
  Node() {
    left_child = right_child = parent = 0;
    data = 0;
  }
  Node *left_child, *right_child, *parent;
  long long data;
};

class Tree {
public:
  Tree() {
    root = new Node();
    num_of_node = 0;
  }
  void insert(long long insert_data) {
    if (num_of_node == 0) {
      root->data = insert_data;
      num_of_node++;
    } else {
      Node *tmp = root;
      while (true) {
        if (tmp->data < insert_data) {
          if (tmp->right_child == 0) {
            tmp->right_child = new Node();
            tmp->right_child->data = insert_data;
            tmp->right_child->parent = tmp;
            break;
          } else {
            tmp = tmp->right_child;
          }
        } else {
          if (tmp->left_child == 0) {
            tmp->left_child = new Node();
            tmp->left_child->data = insert_data;
            tmp->left_child->parent = tmp;
            break;
          } else {
            tmp = tmp->left_child;
          }
        }
      }
    }
  }

  bool find(long long data) {
    if (num_of_node == 0) {
      return false;
    } else {
      Node *tmp = root;
      while (true) {
        if (tmp->data < data) {
          if (tmp->right_child == 0) {
            return false;
          } else {
            tmp = tmp->right_child;
          }
        } else if (tmp->data > data) {
          if (tmp->left_child == 0) {
            return false;
          } else {
            tmp = tmp->left_child;
          }
        } else {
          return true;
        }
      }
    }
  }

  void deleteNode(long long delete_data) {
    if (num_of_node == 0) {
      return;
    } else {
      doDELETE(root, delete_data);
    }
  }

  void print() {
    inORDER(root);
    printf("\\n");
    preORDER(root);
    printf("\\n");
  }

private:
  Node *root;
  int num_of_node;

private:
  void doDELETE(Node *node, long long delete_data) {
    Node *tmp = node;
    while (true) {
      if (tmp->data < delete_data) {
        if (tmp->right_child == 0) {
          // Do nothing
          break;
        } else {
          tmp = tmp->right_child;
        }
      } else if (tmp->data > delete_data) {
        if (tmp->left_child == 0) {
          // Do nothing
          break;
        } else {
          tmp = tmp->left_child;
        }
      } else {
        if (tmp->left_child == 0 && tmp->right_child == 0) {
          if (tmp->parent->data < tmp->data) {
            tmp->parent->right_child = 0;
          } else {
            tmp->parent->left_child = 0;
          }
          delete tmp;
          break;
        } else if (tmp->left_child == 0 && tmp->right_child != 0) {
          if (tmp->parent->data < tmp->data) {
            tmp->parent->right_child = tmp->right_child;
          } else {
            tmp->parent->left_child = tmp->right_child;
          }
          tmp->right_child->parent = tmp->parent;
          delete tmp;
          break;
        } else if (tmp->left_child != 0 && tmp->right_child == 0) {
          if (tmp->parent->data < tmp->data) {
            tmp->parent->right_child = tmp->left_child;
          } else {
            tmp->parent->left_child = tmp->left_child;
          }
          tmp->left_child->parent = tmp->parent;
          delete tmp;
        } else {
          tmp->data = tmp->right_child->data;
          doDELETE(tmp->right_child, tmp->right_child->data);
          break;
        }
      }
    }
  }

  void preORDER(Node *node) {
    printf(" %lld", node->data);
    if (node->left_child != 0)
      preORDER(node->left_child);
    if (node->right_child != 0)
      preORDER(node->right_child);
  }

  void inORDER(Node *node) {
    if (node->left_child != 0)
      inORDER(node->left_child);
    printf(" %lld", node->data);
    if (node->right_child != 0)
      inORDER(node->right_child);
  }
};

int main() {
  int m;
  long long insert_data, search_data, delete_data;
  char buf[BUF_SIZE];
  Tree tree;

  scanf("%d", &m);
  getchar();

  for (int i = 0; i < m; i++) {
    fgets(buf, BUF_SIZE, stdin);
    if (buf[0] == 'i') {
      insert_data = 0;
      for (int k = 0; buf[k] != '\\0'; k++) {
        if (buf[k] >= '0' && buf[k] <= '9') {
          insert_data = 10 * insert_data + (buf[k] - '0');
        }
      }
      tree.insert(insert_data);
    } else if (buf[0] == 'p') {
      tree.print();
    } else if (buf[0] == 'f') {
      search_data = 0;
      for (int k = 0; buf[k] != '\\0'; k++) {
        if (buf[k] >= '0' && buf[k] <= '9') {
          search_data = 10 * search_data + (buf[k] - '0');
        }
      }
      if (tree.find(search_data)) {
        printf("yes\\n");
      } else {
        printf("no\\n");
      }
    } else {
      delete_data = 0;
      for (int k = 0; buf[k] != '\\0'; k++) {
        if (buf[k] >= '0' && buf[k] <= '9') {
          delete_data = 10 * delete_data + (buf[k] - '0');
        }
      }
      tree.deleteNode(delete_data);
    }
  }
}