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

namespace COJ1_7
{
    public class Node
    {
        public int value;
        public Node parent;
        public Node left;
        public Node right;
    }
    class Program
    {

        static void Main(string[] args)
        {
            Node root = new Node();
            int cnt = int.Parse(Console.ReadLine());

            while (cnt-- > 0)
            {
                var command = Console.ReadLine().Split(' ');
                switch (command[0])
                {
                    case "insert": Insert(root, int.Parse(command[1])); break;
                    case "find": Find(root, int.Parse(command[1]));break;
                    case "delete": Delete(root, int.Parse(command[1]));break;
                    default: Print(root); break;
                }
            }
        }

        public static void Insert(Node node, int key)
        {
            Node x = node;
            Node y = null;
            while (x != null&&x.value!=0)
            {
                y = x;
                if (key < x.value)
                    x = x.left;
                else x = x.right;
            }
            Node tmp = new Node();
            tmp.value = key;
            tmp.parent = y;

            if (y==null) 
            {
                node.value = key;
            }
            else if (key < y.value)
            {
                y.left = tmp;
            }
            else
            {
                y.right = tmp;
            }
        }

        public static void Find(Node node, int key)
        {
            Node x = node;
            while (x != null)
            {
                if (key < x.value) x = x.left;
                else if(key>x.value) x = x.right;
                else
                {
                    Console.WriteLine("yes");
                    return;
                }
            }
            Console.WriteLine("no");
        }
        public static void Delete(Node node, int key)
        {
            Node x = node;
            while (x != null)
            {
                if (key < x.value) x = x.left;
                else if (key > x.value) x = x.right;
                else break;
            }
            if (x.value != key) return;
            DeleteNode(node, x);            
        }

        public static void DeleteNode(Node node, Node x)
        {
            if (x.left == null && x.right == null)
            {
                if (x.parent != null && x.parent.left != null && x.parent.left.value == x.value)
                    x.parent.left = null;
                else if (x.parent != null && x.parent.right != null && x.parent.right.value == x.value)
                    x.parent.right = null;
                else if (x.parent == null)
                    x = null;
            }
            else if (x.left != null ^ x.right != null)
            {
                Node tmp;
                if (x.left != null)
                {
                    tmp = x.left;
                }
                else tmp = x.right;

                if (x.parent != null && x.parent.left != null && x.parent.left.value == x.value)
                { x.parent.left = tmp; tmp.parent = x.parent; }
                else if (x.parent != null && x.parent.right != null && x.parent.right.value == x.value)
                { x.parent.right = tmp; tmp.parent = x.parent; }
                else { node = tmp; tmp.parent = null; }
            }
            else
            {
                Node next = GetNext(x.right);
                x.value = next.value;
                DeleteNode(node,next);
            }
        }
        public static Node GetNext(Node x)
        {
            while (x.left != null)
            {
                x = x.left;
            }
            return x;
        }
        public static void Print(Node node)
        {
            StringBuilder builder = new StringBuilder();
            InOrder(node, builder);
            builder.Append("\n");
            PreOrder(node, builder);
            builder.Append("\n");
            Console.Write(builder.ToString());
        }
        public static void InOrder(Node node, StringBuilder builder)
        {
            if (node == null) return;
            InOrder(node.left, builder);
            builder.Append(" ").Append(node.value);
            InOrder(node.right, builder);
        }
        public static void PreOrder(Node node, StringBuilder builder)
        {
            if (node == null) return;
            builder.Append(" ").Append(node.value);
            PreOrder(node.left, builder);
            PreOrder(node.right, builder);
        }
    }
}

Measured memory_bytes: 70396