using System;
using System.Collections.Generic;

namespace C
{
    public class Node
    {
        public int parent;
        public int left;
        public int right;
        public int value;
        public Tree tree;
    }

    public class Tree
    {
        List<Node> nodes;

        public Tree()
        {
            nodes = new List<Node>();
        }

        public void Insert(int x)
        {
            if (nodes.Count == 0) Insert(x, -1);
            else Insert(x, 0);
        }
        public bool Find(int x)
        {
            if (nodes.Count == 0) return false;
            else return Find(x, 0);
        }

        public void Delete(int x)
        {
            if (nodes.Count == 0) return;
            else Delete(x, 0);
        }

        public void Preorder(int node)
        {
            Console.Write(" " + nodes[node].value);
            if (nodes[node].left != -1) Preorder(nodes[node].left);
            if (nodes[node].right != -1) Preorder(nodes[node].right);
        }
        public void Inorder(int node)
        {
            if (nodes[node].left != -1) Inorder(nodes[node].left);
            Console.Write(" " + nodes[node].value);
            if (nodes[node].right != -1) Inorder(nodes[node].right);
        }
        private void Insert(int x, int parent)
        {
            if (parent == -1) nodes.Add(NewNode(x));
            else
            {
                if (nodes[parent].value > x)
                {
                    if (nodes[parent].left == -1)
                    {
                        var node = NewNode(x);
                        node.parent = parent;
                        nodes.Add(node);
                        nodes[parent].left = nodes.Count - 1;
                    }
                    else Insert(x, nodes[parent].left);
                }
                else
                {
                    if (nodes[parent].right == -1)
                    {
                        var node = NewNode(x);
                        node.parent = parent;
                        nodes.Add(node);
                        nodes[parent].right = nodes.Count - 1;
                    }
                    else Insert(x, nodes[parent].right);
                }
            }
        }
        private bool Find(int x, int node)
        {
            if (node == -1) return false;
            if (nodes[node].value == x) return true;
            else
            {
                if (nodes[node].value > x) return Find(x, nodes[node].left);
                else return Find(x, nodes[node].right);
            }
        }
        private Node NewNode(int value)
        {
            return new Node() { value = value, left = -1, right = -1, tree = this };
        }

        private void Delete(int x, int node)
        {
            if (node == -1) return;
            if (nodes[node].value == x)
            {
                if (nodes[node].left == -1 && nodes[node].right == -1)
                {
                    var parentNode = nodes[nodes[node].parent];
                    if (parentNode.left == node) parentNode.left = -1;
                    else parentNode.right = -1;
                }
                else if (nodes[node].left != -1 && nodes[node].right != -1)
                {
                    var next = NextNode(node);
                    nodes[node].value = nodes[next].value;
                    Delete(nodes[next].value, next);
                }
                else if (nodes[node].left == -1)
                {
                    var parentNode = nodes[nodes[node].parent];
                    if (parentNode.left == node) parentNode.left = nodes[node].right;
                    else parentNode.right = nodes[node].right;
                    nodes[nodes[node].right].parent = nodes[node].parent;
                }
                else
                {
                    var parentNode = nodes[nodes[node].parent];
                    if (parentNode.left == node) parentNode.left = nodes[node].left;
                    else parentNode.right = nodes[node].left;
                    nodes[nodes[node].left].parent = nodes[node].parent;
                }
            }
            else
            {
                if (nodes[node].value > x) Delete(x, nodes[node].left);
                else Delete(x, nodes[node].right);
            }
        }
        private int NextNode(int node)
        {
            node = nodes[node].right;
            while (nodes[node].left != -1) node = nodes[node].left;
            return node;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            var n = int.Parse(Console.ReadLine());
            var tree = new Tree();
            for (int i = 0; i < n; i++)
            {
                var strs = Console.ReadLine().Split(' ');
                if (strs[0][0] == 'i') tree.Insert(int.Parse(strs[1]));
                else if (strs[0][0] == 'p')
                {
                    tree.Inorder(0); Console.WriteLine();
                    tree.Preorder(0); Console.WriteLine();
                }
                else if (strs[0][0] == 'f') Console.WriteLine(tree.Find(int.Parse(strs[1])) ? "yes" : "no");
                else tree.Delete(int.Parse(strs[1]));
            }
        }
    }
}

Measured memory_bytes: 40756