using System;
using System.Text;

namespace ALDS1_8_C {
    class Node {
        public int Key { get; set; }
        public Node Parent { get; set; }
        public Node Left { get; set; }
        public Node Right { get; set; }

        public Node(int key = default(int), Node parent = null) {
            Key = key;
            Parent = parent;
        }

        public Node Min {
            get {
                Node n = this;
                while (n.Left != null) {
                    n = n.Left;
                }
                return n;
            }
        }
    }

    class Problem {
        private Node _root;
        private StringBuilder _sb;

        public void Run() {
            _sb = new StringBuilder();
            int n = int.Parse(Console.ReadLine());
            SetRoot();
            for (int i = 1; i < n; i++) {
                string[] input = Console.ReadLine().Split(' ');

                switch (input[0]) {
                    case "insert":
                        Insert(int.Parse(input[1]));
                        break;
                    case "print":
                        Inorder(_root);
                        _sb.AppendLine();
                        Preorder(_root);
                        _sb.AppendLine();
                        break;
                    case "find":
                        _sb.AppendLine(Find(int.Parse(input[1])) != null ? "yes" : "no");
                        break;
                    case "delete":
                        Delete(int.Parse(input[1]));
                        break;
                }
            }
            Console.Write(_sb);
        }

        private void Delete(int key) {
            Delete(Find(key));
        }

        private void Delete(Node n) {
            if (n == null)
                return;

            if (n.Left == null)
                Replace(n, n.Right);
            else if (n.Right == null)
                Replace(n, n.Left);
            else {
                Node m = n.Right.Min;
                n.Key = m.Key;
                Replace(m, m.Right);
            }
        }

        private void Replace(Node n, Node m) {
            Node p = n.Parent;
            if (m != null)
                m.Parent = p;

            if (n == _root)
                _root = m;
            else if (p.Left == n)
                p.Left = m;
            else
                p.Right = m;
        }
        
        private Node Find(int key) {
            Node ret = _root;
            while (ret != null && key != ret.Key) {
                ret = key < ret.Key ? ret.Left : ret.Right;
            }
            return ret;
        }

        private void Insert(int key) {
            Node x = _root;
            Node y = null;
            while (x != null) {
                y = x;
                x = key < x.Key ? x.Left : x.Right;
            }

            if (key < y.Key)
                y.Left = new Node(key, parent: y);
            else
                y.Right = new Node(key, parent: y);
        }

        private void Inorder(Node node) {
            if (node == null)
                return;
            Inorder(node.Left);
            _sb.Append(" " + node.Key);
            Inorder(node.Right);
        }

        private void Preorder(Node node) {
            if (node == null)
                return;
            _sb.Append(" " + node.Key);
            Preorder(node.Left);
            Preorder(node.Right);
        }

        private void SetRoot() {
            string[] input = Console.ReadLine().Split(' ');
            _root = new Node(int.Parse(input[1]));
        }
    }

    class Program {
        static void Main(string[] args) {
            Problem p = new Problem();
            p.Run();
        }
    }
}

Measured memory_bytes: 82476