using System;

namespace ConsoleApp1
{
    public class Node {
        public int Parent { get; set; } = -1;
        public Node(int value) { this.Value = value; }
        public int Value { get; set; } = 0;
        public Node Left { get; set; } = null;
        public Node Right { get; set; } = null;
    }

    class Program
    {
        public static Node root;
        static void Main()
        {
            int n = int.Parse(Console.ReadLine());
            for (int i = 0; i < n; i++)
            {
                string[] currentString = Console.ReadLine().Split(' ');

                if (currentString[0].Equals("insert")) 
                { Insert(root, int.Parse(currentString[1])); }
                else if (currentString[0].Equals("find")) 
                { Console.WriteLine(Find(root, int.Parse(currentString[1]))); }
                else if (currentString[0].Equals("delete")) 
                { Delete(int.Parse(currentString[1])); }
                else if (currentString[0].Equals("print")) 
                { InOrder(root); Console.WriteLine(); PreOrder(root); Console.WriteLine(); }
                else { throw new ArgumentException("currentString must be insert, find, delete, print but is " + currentString); }

            }
        }

        private static String Find(Node node, int value)
        {
            if (node == null) { return "no"; }
            else if (node.Value == value) { return "yes"; }
            else if (value < node.Value) { return Find(node.Left, value); }
            else { return Find(node.Right, value); }
        }

        private static void Insert(Node node, int value) 
        {
            if (node == null) 
            {
                root = new Node(value);
                return;
            }
            while (true) 
            {
                if (value > node.Value) { 
                    if (node.Right == null) 
                    {
                        node.Right = new Node(value); 
                        return; 
                    } 
                    else { node = node.Right; } }
                else 
                { 
                    if (node.Left == null) 
                    { 
                        node.Left = new Node(value);
                        return;
                    } 
                    else { node = node.Left; } 
                }
            }
        }

        private static void Delete(int value)
        {
            Node parent = root; Node z = root;
            while (z.Value != value) 
            {
                if (value > parent.Value) { if (parent.Right.Value == value) z = parent.Right; else parent = parent.Right; }
                else { if (parent.Left.Value == value) z = parent.Left; else parent = parent.Left; }
            }
            if (z.Left == null && z.Right == null) if (parent?.Left?.Value == value) parent.Left = null; else parent.Right = null;
            else if (z.Left == null || z.Right == null)
                if (z.Left == null)
                    if (parent?.Left?.Value == value) parent.Left = z.Right; else parent.Right = z.Right;
                else
                    if (parent?.Left?.Value == value) parent.Left = z.Left; else parent.Right = z.Left;
            else 
            {
                Node y = z.Right; Node yParent = z;
                while (y.Left != null) { yParent = y; y = y.Left; }
                z.Value = y.Value;
                if (z.Right.Left == null) z.Right = null; else yParent.Left = null;
            }
        }

        private static void PostOrder(Node node) 
        {
            if (node.Left != null){ PostOrder(node.Left); }
            if (node.Right != null) { PostOrder(node.Right); }
            Console.Write(" " + node.Value);
        }
        private static void InOrder(Node node)
        {
            if (node.Left != null) { InOrder(node.Left); }
            Console.Write(" " + node.Value);
            if (node.Right != null) { InOrder(node.Right); }
        }
        private static void PreOrder(Node node)
        {
            Console.Write(" " + node.Value);
            if (node.Left != null) { PreOrder(node.Left); }
            if (node.Right != null) { PreOrder(node.Right); }
        }
    }
}



Measured memory_bytes: 34088