using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Node
    {
        internal int key;
        internal Node left, right, parent;

        public Node(int key)
        {
            this.key = key;
        }
    }

    class Program
    {
        static StringBuilder sb = new StringBuilder();
        static Node root;

        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());

            for(int i = 0; i < n; i++)
            {
                string[] input = Console.ReadLine().Split(' ');

                switch (input[0][0])
                {
                    case 'i':
                        Insert(int.Parse(input[1]));
                        break;
                    case 'f':
                        sb.AppendLine(find(root, int.Parse(input[1])) != null ? "yes" : "no");
                        break;
                    case 'd':
                        Delete(find(root, int.Parse(input[1])));
                        break;
                    default:
                        Inorder(root);
                        sb.AppendLine();
                        Preorder(root);
                        sb.AppendLine();
                        break;
                }
            }
            Console.Write(sb);
        }

        static void Delete(Node z)
        {
            Node x, y;

            if (z.left == null || z.right == null) y = z;
            else y = GetSuccessor(z);

            if (y.left != null) x = y.left;
            else x = y.right;

            if (x != null) x.parent = y.parent;

            if (y.parent == null) root = x;
            else if (y == y.parent.left) y.parent.left = x;
            else y.parent.right = x;

            if (y != z) z.key = y.key;
        }

        static Node GetSuccessor(Node x)
        {
            Node y;

            if (x.right != null)
            {
                y = x.right;

                while (y.left != null)
                {
                    y = y.left;
                }
                return y;
            }
            else
            {
                y = x.parent;

                while (y != null && x == y.right)
                {
                    x = y;
                    y = y.parent;
                }
                return y;
            }
        }

        static Node find(Node u,int k)
        {
            while (u != null && k != u.key)
            {
                u = k < u.key ? u.left : u.right;
            }
            return u;
        }
        
        static void Insert(int k)
        {
            Node x = root;
            Node y = null;
            Node z = new Node(k);

            while (x != null)
            {
                y = x;
                x = z.key < x.key ? x.left : x.right;
            }

            z.parent = y;

            if (y != null)
            {
                if (z.key < y.key) y.left = z;
                else y.right = z;
            }
            else root = z;
        }

        static void Inorder(Node u)
        {
            if (u == null) return;

            Inorder(u.left);
            sb.Append(" " + u.key);
            Inorder(u.right);
        }

        static void Preorder(Node u)
        {
            if (u == null) return;

            sb.Append(" " + u.key);
            Preorder(u.left);
            Preorder(u.right);
        }
    }
}

Measured memory_bytes: 82584