Language: C++

template<typename T>
class NodeAllocator {
public:
    NodeAllocator() = default;
    explicit NodeAllocator(int num): reserved_(num), assigned_(0) {
        buf_ = new T[num];
    };

    T* GetAlloc(int num) {
        if (assigned_ + num >= reserved_) {
            return nullptr;
        }
        int retp = assigned_;
        assigned_ += num;
        return buf_ + retp;
    }

    void ReAlloc(int num){
        DeleteAllAlloc();
        buf_ = new T[num];
        reserved_ = num;
    }

    ~NodeAllocator() {
        DeleteAllAlloc();
    }

//    void DeleteElem(T* e) {
//        return;
//    }

private:
    void DeleteAllAlloc() {
        if (reserved_ == 0) {
            return;
        }
        if (reserved_ > 1) {
            delete [] buf_;
        }else{
            delete buf_;
        }

        assigned_ = 0;
        reserved_ = 0;
    }

    int reserved_;
    int assigned_;
    T *buf_;
};

struct Node {
    int key;
    Node *p = nullptr;
    Node *l = nullptr;
    Node *r = nullptr;
};

NodeAllocator<Node> Alloc;

Node *root;

void Insert(int v) {
    Node *p = nullptr;
    Node *x = root;
    Node *k = Alloc.GetAlloc(1);
    k->key = v;
    while (x != nullptr) {
        p = x;
        if (k->key < x->key) {
            x = x->l;
        } else {
            x = x->r;
        }
    }
    k->p = p;
    if (p == nullptr) {
        root = k;
    } else if (k->key < p->key) {
        p->l = k;
    } else {
        p->r = k;
    }
}

Node *Find(Node *r, int key) {
    while (r != nullptr && r->key != key) {
        if (key < r->key) {
            r = r->l;
        } else {
            r = r->r;
        }
    }
    return r;
}

Node *GetMinimum(Node *r) {
    while (r->l != nullptr) {
        r = r->l;
    }
    return r;
}

Node *GetSuccessor(Node *r) {
    if (r->r != nullptr) {
        return GetMinimum(r->r);
    }
    Node* p = r->p;
    while ((p != nullptr) && (r == p->r)) {
        r = p;
        p = p->p;
    }
    return p;
}

bool Delete(Node *r, Node *tgt) {
    if (tgt == nullptr) {
        return false;
    }
    // Having no children
    if ((tgt->l == nullptr) && (tgt->r == nullptr)) {
        if (tgt->key < tgt->p->key) {
            tgt->p->l = nullptr;
        } else {
            tgt->p->r = nullptr;
        }
        tgt->key = 0;
        tgt->p = nullptr;
        tgt->l = nullptr;
        tgt->r = nullptr;
    }
        //Having two children.
    else if ((tgt->l != nullptr) && (tgt->r != nullptr)) {
        Node* next = GetSuccessor(tgt);
//        Node *next = tgt->r;
//        while (next->l != nullptr) {
//            next = next->l;
//        }
        tgt->key = next->key;
        Delete(r, next);
    }
        //Having one child.
    else {
        Node *c = nullptr;
        if (tgt->l != nullptr) {
            c = tgt->l;
        } else {
            c = tgt->r;
        }
        c->p = tgt->p;
        if (tgt->key < tgt->p->key) {
            tgt->p->l = c;
        } else {
            tgt->p->r = c;
        }
        tgt->key = 0;
        tgt->p = nullptr;
        tgt->l = nullptr;
        tgt->r = nullptr;
    }
    return true;
}


void Preorder(Node *tgt) {
    if (tgt == nullptr) {
        return;
    }
    std::cout << " " << tgt->key;
    Preorder(tgt->l);
    Preorder(tgt->r);
}

void Inorder(Node *tgt) {
    if (tgt == nullptr) {
        return;
    }
    Inorder(tgt->l);
    std::cout << " " << tgt->key;
    Inorder(tgt->r);
}

int main() {
    int n;
    std::cin >> n;
    Alloc.ReAlloc(n);
    for (int i = 0; i < n; i++) {
        std::string str;
        std::cin >> str;
        if (str == "print") {
            Inorder(root);
            std::cout << std::endl;
            Preorder(root);
            std::cout << std::endl;
            continue;
        }
        int k;
        std::cin >> k;
        if (str == "insert") {
            Insert(k);
        } else if (str == "find") {
            if (Find(root, k) != nullptr) {
                std::cout << "yes" << std::endl;
            } else {
                std::cout << "no" << std::endl;
            }
        } else if (str == "delete") {
            Node *tgt = Find(root, k);
            Delete(root, tgt);
        }
    }
}


Compiler output:
a.cc: In function 'void Preorder(Node*)':
a.cc:169:10: error: 'cout' is not a member of 'std'
  169 |     std::cout << " " << tgt->key;
      |          ^~~~
a.cc:1:1: note: 'std::cout' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
  +++ |+#include <iostream>
    1 | template<typename T>
a.cc: In function 'void Inorder(Node*)':
a.cc:179:10: error: 'cout' is not a member of 'std'
  179 |     std::cout << " " << tgt->key;
      |          ^~~~
a.cc:179:10: note: 'std::cout' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
a.cc: In function 'int main()':
a.cc:185:10: error: 'cin' is not a member of 'std'
  185 |     std::cin >> n;
      |          ^~~
a.cc:185:10: note: 'std::cin' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
a.cc:188:14: error: 'string' is not a member of 'std'
  188 |         std::string str;
      |              ^~~~~~
a.cc:1:1: note: 'std::string' is defined in header '<string>'; this is probably fixable by adding '#include <string>'
  +++ |+#include <string>
    1 | template<typename T>
a.cc:189:14: error: 'cin' is not a member of 'std'
  189 |         std::cin >> str;
      |              ^~~
a.cc:189:14: note: 'std::cin' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
a.cc:189:21: error: 'str' was not declared in this scope; did you mean 'std'?
  189 |         std::cin >> str;
      |                     ^~~
      |                     std
a.cc:192:18: error: 'cout' is not a member of 'std'
  192 |             std::cout << std::endl;
      |                  ^~~~
a.cc:192:18: note: 'std::cout' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
a.cc:192:31: error: 'endl' is not a member of 'std'
  192 |             std::cout << std::endl;
      |                               ^~~~
a.cc:1:1: note: 'std::endl' is defined in header '<ostream>'; this is probably fixable by adding '#include <ostream>'
  +++ |+#include <ostream>
    1 | template<typename T>
a.cc:194:18: error: 'cout' is not a member of 'std'
  194 |             std::cout << std::endl;
      |                  ^~~~
a.cc:194:18: note: 'std::cout' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
a.cc:194:31: error: 'endl' is not a member of 'std'
  194 |             std::cout << std::endl;
      |                               ^~~~
a.cc:194:31: note: 'std::endl' is defined in header '<ostream>'; this is probably fixable by adding '#include <ostream>'
a.cc:198:14: error: 'cin' is not a member of 'std'
  198 |         std::cin >> k;
      |              ^~~
a.cc:198:14: note: 'std::cin' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
a.cc:203:22: error: 'cout' is not a member of 'std'
  203 |                 std::cout << "yes" << std::endl;
      |                      ^~~~
a.cc:203:22: note: 'std::cout' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
a.cc:203:44: error: 'endl' is not a member of 'std'
  203 |                 std::cout << "yes" << std::endl;
      |                                            ^~~~
a.cc:203:44: note: 'std::endl' is defined in header '<ostream>'; this is probably fixable by adding '#include <ostream>'
a.cc:205:22: error: 'cout' is not a member of 'std'
  205 |                 std::cout << "no" << std::endl;
      |                      ^~~~
a.cc:205:22: note: 'std::cout' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
a.cc:205:43: error: 'endl' is not a member of 'std'
  205 |                 std::cout << "no" << std::endl;
      |                                           ^~~~
a.cc:205:43: note: 'std::endl' is defined in header '<ostream>'; this is probably fixable by adding '#include <ostream>'