import std.stdio;
import std.conv;
import std.algorithm;
import std.range;
import std.string;
import std.typecons;
import std.math;
import std.random;
import std.range;
import std.functional;
import std.container;
class bt(T)
{
    t!T root;
    bool containsKey(T key)
    {
        return !!find(key);
    }
    void deleteKey(T key)
    {
        auto n = find(key);
        auto n2 = n;
        if (!n)
            return;
        if (n.left && n.right)
        {
            n = n.getSuccessor;
        }
        if (!n.left && !n.right)
        {
            n.parent.replaceChild(n, null);
        }
        else if (n.left && !n.right)
        {
            n.parent.replaceChild(n, n.left);
        }
        else if (!n.left && n.right)
        {
            n.parent.replaceChild(n, n.right);
        }
        n2.key = n.key;
    }
    private t!T find(T key)
    {
        return root ? root.find(key) : null;
    }
    void insert(T key)
    {
        t!T y = null;
        auto x = root;
        auto z = new t!T(key);
        while (x)
        {
            y = x;
            if (z.key < y.key)
            {
                x = x.left;
            }
            else
            {
                 x = x.right;
            }
        }
        z.parent = y;
        if (!y)
        {
            root = z;
        }
        else
        {
            if (z.key < y.key)
            {
                y.left = z;
            }
            else
            {
                y.right = z;
            }
        }
    }
}
class t(T)
{
    T key;
    this(T k)
    {
        key = k;
    }
    t left, right, parent;
    t!T getMin()
    {
        return this.left ? this.left.getMin : this;
    }
    t!T getSuccessor()
    {
        if (this.right)
            return this.right.getMin();
        auto x = this;
        auto y = parent;
        while (y && x == y.right)
        {
            x = y;
            y = y.parent;
        }
        return y;
    }
    void replaceChild(t!T c, t!T r)
    {
        if (left == c)
        {
            if (r)
                r.parent = this;
            left = r;
            return;
        }
        if (right == c)
        {
            if (r)
                r.parent = this;
            right = r;
            return;
        }
    }
    t!T find(T k)
    {
        if (key == k)
            return this;
        if (key > k)
            return left ? left.find(k) : null;
        else
            return right ? right.find(k) : null;
    }
}
void in_(T)(t!T t)
{
    if (!t)
        return;
    in_(t.left);
    write(" ", t.key);
    in_(t.right);
}
void pre(T)(t!T t)
{
    if (!t)
        return;
    write(" ", t.key);
    pre(t.left);
    pre(t.right);
}
void main()
{
    auto c = readln.strip.to!int;
    bt!int b = new bt!int;
    foreach (_; 0..c)
    {
        auto instr = readln.strip.split(" ");
        if (instr.length == 1)
        {
            in_(b.root);
            writeln();
            pre(b.root);
            writeln();
        }
        else
        {
            auto key = instr[1].to!int;
            if (instr[0] == "find")
            {
                writeln(b.containsKey(key) ? "yes" : "no");
            }
            else if (instr[0] == "delete")
            {
                b.deleteKey(key);
            }
            else
            {
                b.insert(key);
            }
        }
    }
}
