Problem p02285 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>

using namespace std;

struct Node {
public:
  Node *parent;
  Node *left;
  Node *right;
  int value;

  Node(Node *p = 0, Node *l = 0, Node *r = 0, int v = 0)
      : parent(p), left(l), right(r), value(v){};

private:
protected:
};

class Tree {
private:
  Node *root;
  enum ORDER { PREORDER = 0, INORDER, POSTORDER };

public:
  Tree() { root = 0; };

  void insert(long longz) {
    Node *z = new Node(0, 0, 0, longz);
    insert(z);
  };

  void insert(Node *z) {
    Node *y = 0;
    Node *x = root;
    while (x != 0) {
      y = x;
      if (z->value < x->value) {
        x = x->left;
      } else {
        x = x->right;
      }
    }
    z->parent = y;
    if (y == 0) {
      root = z;
    } else if (z->value < y->value) {
      y->left = z;
    } else {
      y->right = z;
    }
    return;
  };

  void deleteValue(long value) {
    Node *z = find(value);
    deleteValue(z);
  };

  void deleteValue(Node *z) {
    Node *y = 0;

    if (z->left == 0 || z->right == 0) {
      y = z;
    } else {
      if (z->right != 0) {
        y = minimum(z->right);
      } else {
        Node *buf = z->parent;
        while (buf != 0 && z == buf->right) {
          z = buf;
          buf = buf->parent;
        }
        y = buf;
      }
    }

    Node *x = 0;
    if (y->left != 0)
      x = y->left;
    else
      x = y->right;

    if (x != 0)
      x->parent = y->parent;
    if (y->parent == 0) {
      root = x;
    } else if (y == (y->parent)->left) {
      (y->parent)->left = x;
    } else {
      (y->parent)->right = x;
    }

    if (y != z)
      z->value = y->value;
  };

  Node *minimum(long value) {
    Node *z = new Node(0, 0, 0, value);
    return minimum(z);
  };

  Node *minimum(Node *z) {
    while (z->left != 0) {
      z = z->left;
    }
    return z;
  };

  Node *find(long value) {
    Node *findNode = new Node(0, 0, 0, value);
    return find(findNode);
  };

  Node *find(Node *z) {
    Node *x = root;

    while (x != 0) {
      if (z->value < x->value) {
        x = x->left;
      } else if (z->value > x->value) {
        x = x->right;
      } else {
        return x;
      }
    }
    return 0;
  };

  void print() {
    print(INORDER);
    print(PREORDER);
  };
  void print(ORDER order) {
    Node *cur = root;
    Node *pre = cur->parent;

    if (root == 0)
      return;

    while (cur) {
      if (pre == cur->parent) {
        if (order == PREORDER)
          cout << " " << cur->value;
        if (cur->left != 0) {
          pre = cur;
          cur = cur->left;
          continue;
        } else {
          pre = cur->left;
        }
      }

      if (pre == cur->left) {
        if (order == INORDER)
          cout << " " << cur->value;
        if (cur->right != 0) {
          pre = cur;
          cur = cur->right;
          continue;
        } else {
          pre = cur->right;
        }
      }

      if (pre == cur->right) {
        if (order == POSTORDER)
          cout << " " << cur->value;
        pre = cur;
        cur = cur->parent;
      }
    }
    cout << endl;
    return;
  };

protected:
};

int main() {
  Tree tree;
  int n = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    string str = "";
    cin >> str;

    if (str == "insert") {
      long x = 0;
      cin >> x;
      tree.insert(x);
    } else if (str == "find") {
      long value = 0;
      cin >> value;
      Node *f = tree.find(value);
      if (f != 0)
        cout << "yes" << endl;
      else
        cout << "no" << endl;
    } else if (str == "delete") {
      long value = 0;
      cin >> value;
      tree.deleteValue(value);
    } else if (str == "print") {
      tree.print();
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>

using namespace std;

struct Node {
public:
  Node *parent;
  Node *left;
  Node *right;
  int value;

  Node(Node *p = 0, Node *l = 0, Node *r = 0, int v = 0)
      : parent(p), left(l), right(r), value(v){};

private:
protected:
};

class Tree {
private:
  Node *root;
  enum ORDER { PREORDER = 0, INORDER, POSTORDER };

public:
  Tree() { root = 0; };

  void insert(long longz) {
    Node *z = new Node(0, 0, 0, longz);
    insert(z);
  };

  void insert(Node *z) {
    Node *y = 0;
    Node *x = root;
    while (x != 0) {
      y = x;
      if (z->value < x->value) {
        x = x->left;
      } else {
        x = x->right;
      }
    }
    z->parent = y;
    if (y == 0) {
      root = z;
    } else if (z->value < y->value) {
      y->left = z;
    } else {
      y->right = z;
    }
    return;
  };

  void deleteValue(long value) {
    Node *z = find(value);
    deleteValue(z);
  };

  void deleteValue(Node *z) {
    Node *y = 0;

    if (z->left == 0 || z->right == 0) {
      y = z;
    } else {
      if (z->right != 0)
        y = minimum(z->right);
      else {
        Node *buf = z->parent;
        while (buf != 0 && z == buf->right) {
          z = buf;
          buf = buf->parent;
        }
        y = buf->parent;
      }
    }

    Node *x = 0;
    if (y->left != 0)
      x = y->left;
    else
      x = y->right;

    if (x != 0)
      x->parent = y->parent;
    if (y->parent == 0) {
      root = x;
    } else if (y == y->parent->left) {
      y->parent->left = x;
    } else {
      y->parent->right = x;
    }

    if (y != z)
      z->value = y->value;
  };

  Node *minimum(long value) {
    Node *z = new Node(0, 0, 0, value);
    return minimum(z);
  };

  Node *minimum(Node *z) {
    Node *x = root;
    while (x != 0) {
      if (x->left != 0) {
        x = x->left;
      }
    }
    return x;
  };

  Node *find(long value) {
    Node *findNode = new Node(0, 0, 0, value);
    return find(findNode);
  };

  Node *find(Node *z) {
    Node *x = root;

    while (x != 0) {
      if (z->value < x->value) {
        x = x->left;
      } else if (z->value > x->value) {
        x = x->right;
      } else {
        return x;
      }
    }
    return 0;
  };

  void print() {
    print(INORDER);
    print(PREORDER);
  };
  void print(ORDER order) {
    Node *cur = root;
    Node *pre = cur->parent;

    if (root == 0)
      return;

    while (cur) {
      if (pre == cur->parent) {
        if (order == PREORDER)
          cout << " " << cur->value;
        if (cur->left != 0) {
          pre = cur;
          cur = cur->left;
          continue;
        } else {
          pre = cur->left;
        }
      }

      if (pre == cur->left) {
        if (order == INORDER)
          cout << " " << cur->value;
        if (cur->right != 0) {
          pre = cur;
          cur = cur->right;
          continue;
        } else {
          pre = cur->right;
        }
      }

      if (pre == cur->right) {
        if (order == POSTORDER)
          cout << " " << cur->value;
        pre = cur;
        cur = cur->parent;
      }
    }
    cout << endl;
    return;
  };

protected:
};

int main() {
  Tree tree;
  int n = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    string str = "";
    cin >> str;

    if (str == "insert") {
      long x = 0;
      cin >> x;
      tree.insert(x);
    } else if (str == "find") {
      long value = 0;
      cin >> value;
      Node *f = tree.find(value);
      if (f != 0)
        cout << "yes" << endl;
      else
        cout << "no" << endl;
    } else if (str == "delete") {
      long value = 0;
      cin >> value;
      tree.deleteValue(value);
    } else if (str == "print") {
      tree.print();
    }
  }

  return 0;
}