using IOUtil;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using Tree;
#if !DEBUG
    using System.IO;
#endif


public class Program {
    public static void Main() {
        #region SetAutoFlushIsFalse
#if !DEBUG
        var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
        Console.SetOut(sw);
#endif
        #endregion

        var n = ReadLib.ReadAsInt();

        var tree = new BinarySearchTree();
        for (int i = 0; i < n; i++) {
            var arrs = Console.ReadLine().Split(' ');

            switch (arrs[0]) {
                case "insert":
                    tree.Insert(int.Parse(arrs[1]));
                    break;
                case "delete":
                    tree.Delete(int.Parse(arrs[1]));
                    break;
                case "find":
                    Console.WriteLine(tree.Find(int.Parse(arrs[1])) ? "yes" : "no");
                    break;
                default:
                    tree.GetInOrder().Select(d => $" {d}").ToList().ForEach(s => Console.Write(s));
                    Console.WriteLine();
                    tree.GetPreOrder().Select(d => $" {d}").ToList().ForEach(s => Console.Write(s));
                    Console.WriteLine();
                    break;
            }
        }

        #region Flush
#if !DEBUG
        Console.Out.Flush();
        sw.Close();
#endif
        #endregion
    }

}

static class Calculation {

}

namespace IOUtil {
    static class ReadLib {
        public static long ReadAsLong() {
            return long.Parse(Console.ReadLine());
        }

        public static int ReadAsInt() {
            return int.Parse(Console.ReadLine());
        }

        public static int[] ReadAsIntArray(int step = 0) {
            return Console.ReadLine().Split(' ').Select(s => int.Parse(s) + step).ToArray();
        }

        public static long[] ReadAsLongArray(int step = 0) {
            return Console.ReadLine().Split(' ').Select(s => long.Parse(s) + step).ToArray();
        }

        public static ulong[] ReadAsULongArray() {
            return Console.ReadLine().Split(' ').Select(s => ulong.Parse(s)).ToArray();
        }

        public static string[] ReadAsStringArray() {
            return Console.ReadLine().Split(' ');
        }

        public static int[] ReadNTimesAsInt(int n, int step = 0) {
            return Enumerable.Range(1, n).Select(s => int.Parse(Console.ReadLine()) + step).ToArray();
        }

        public static char[][] ReadNTimesAsCharArray(int n) {
            return Enumerable.Range(1, n).Select(i => Console.ReadLine().ToArray()).ToArray();
        }

        public static long[] ReadNTimesAsLong(int n, int step = 0) {
            return Enumerable.Range(1, n).Select(s => long.Parse(Console.ReadLine()) + step).ToArray();
        }

        public static int[][] ReadNTimesAsIntArray(int n, int step = 0) {
            return Enumerable.Range(1, n).Select(s => ReadAsIntArray(step)).ToArray();
        }

        public static string[] ReadNTimesAsString(int n) {
            return Enumerable.Range(1, n).Select(s => Console.ReadLine()).ToArray();
        }

    }
}

namespace Tree {
    /// <summary>UnionFind木</summary>
    public class UnionFind {
        Dictionary<int, int> nodeToRoot;
        Dictionary<int, int> rank; // その根が配下に何個nodeがぶら下がっているか

        public UnionFind(int n, int startAt = 0) {
            nodeToRoot = Enumerable.Range(startAt, n).ToDictionary(i => i, i => i);    // n個根を作る
            rank = Enumerable.Range(startAt, n).ToDictionary(i => i, i => 0);
        }

        /// <summary>根を得る</summary>
        public int GetRoot(int x) {
            if (nodeToRoot[x] == x) { // 渡されたノードが根
                return x;
            }
            nodeToRoot[x] = GetRoot(nodeToRoot[x]);  // parentの根を得てset。調べるたびに最適化。

            return nodeToRoot[x];
        }

        /// <summary>xとyの木を併合 => 根を同一にする</summary>
        public void Unite(int x, int y) {
            var rootX = GetRoot(x);
            var rootY = GetRoot(y);

            if (rootX == rootY) {
                return; // pass
            }

            if (rank[rootX] < rank[rootY]) {    // yをxへ併合
                nodeToRoot[rootX] = nodeToRoot[rootY];
            } else {
                nodeToRoot[rootX] = nodeToRoot[rootY];

                if (rank[rootX] == rank[rootY]) {
                    rank[rootX]++;  // yの下に配下が1つ追加される。
                }
            }
        }

        public bool AreSameRoot(int x, int y) {
            return GetRoot(x) == GetRoot(y);
        }
    }


    /// <summary>根付き木</summary>
    public class RootedTree {
        readonly Dictionary<int, Node> nodes;
        const int NIL = -1;
        int startAt;
        int nNode;

        class Node {
            /// <summary>親のID</summary>
            public int Parent { get; set; }
            /// <summary>子の最左端</summary>
            public int LeftChild { get; set; }
            /// <summary>右の兄弟</summary>
            public int RightBrother { get; set; }
            /// <summary>深さ</summary>
            public int Depth { get; set; }

            public Node() {
                Parent = NIL;
                LeftChild = NIL;
                RightBrother = NIL;
                Depth = 0;
            }
        }

        /// <summary>根付き木</summary>
        public RootedTree(int n, int startAt = 0) {
            this.nodes = Enumerable.Range(startAt, n).ToDictionary(i => i, i => new Node());
            this.startAt = startAt;
            this.nNode = n;
        }

        /// <summary>Nodeに子を追加</summary>
        public void SetChilds(int id, int[] childs) {
            if (childs.Length == 0) {
                return;
            }

            nodes[id].LeftChild = childs.First();

            for (int i = 0; i < childs.Length - 1; i++) {
                int childId = childs[i];
                nodes[childId].Parent = id;
                nodes[childId].RightBrother = childs[i + 1];
            }

            nodes[childs.Last()].Parent = id;
        }

        /// <summary>親Idを取得</summary>
        public int GetParentId(int id) {
            return nodes[id].Parent;
        }

        /// <summary>子のidを取得</summary>
        public int[] GetChilds(int id) {
            var childs = new List<int>();

            var child = nodes[id].LeftChild;
            while (child != NIL) {
                childs.Add(child);
                child = nodes[child].RightBrother;
            }

            return childs.ToArray();
        }

        int Root = NIL;
        public int GetRoot() {
            if (Root == NIL) {
                var current = nodes.First().Key;  // 何でもいい
                var parent = nodes[current].Parent;

                while (parent != NIL) {
                    current = parent;
                    parent = nodes[current].Parent;
                }

                Root = current;
            }
            return Root;
        }

        bool isSetedDepth = false;
        /// <summary>深さを取得</summary>
        public int GetDepth(int id) {
            if (!isSetedDepth) {
                var root = GetRoot();
                SetDepth(root, depth: 0);
                isSetedDepth = true;
            }

            var current = id;
            var parent = nodes[id].Parent;
            var depth = 0;
            while (parent != NIL) {
                current = parent;
                parent = nodes[current].Parent;
                depth++;
            }

            return depth;
        }

        private void SetDepth(int id, int depth) {
            var node = nodes[id];
            node.Depth = depth;

            if (node.RightBrother != NIL) {
                SetDepth(node.RightBrother, depth);
            }

            if (node.LeftChild != NIL) {
                SetDepth(node.LeftChild, depth + 1);
            }

        }

        public int NodesCount() {
            return nodes.Count();
        }
    }

    /// <summary>二分木</summary>
    public class BinaryTree {
        const int NIL = int.MinValue;
        readonly int startAt;
        readonly int nNode;
        class Node {
            public int Parent { get; set; }
            public int LeftChild { get; set; }
            public int RightChild { get; set; }

            public int Depth { get; set; }
            public int Height { get; set; }
            /// <summary>唯一の兄弟</summary>
            public int Sibling { get; set; }

            public Node() {
                Parent = NIL;
                LeftChild = NIL;
                RightChild = NIL;
                Sibling = NIL;

                Depth = 0;
                Height = 0;
            }
        }


        Dictionary<int, Node> nodes;
        public BinaryTree(int n, int startAt) {
            this.nodes = Enumerable.Range(startAt, n).ToDictionary(i => i, i => new Node());
            this.startAt = startAt;
            this.nNode = n;
        }

        /// <summary>
        /// 子をセットする
        /// </summary>
        /// <param name="id">ノードのID</param>
        /// <param name="left">左子ID。存在しない場合は-1</param>
        /// <param name="right">右子ID。存在しない場合は-1</param>
        public void SetChilds(int id, int left, int right) {
            nodes[id].LeftChild = left;
            nodes[id].RightChild = right;

            if (left != NIL) {
                nodes[left].Parent = id;
                nodes[left].Sibling = right;
            }

            if (right != NIL) {
                nodes[right].Parent = id;
                nodes[right].Sibling = left;
            }
        }

        /// <summary>Root</summary>
        int Root = NIL;
        public int GetRoot() {
            if (Root == NIL) {
                var current = nodes.First().Key;
                var parent = nodes[current].Parent;

                while (parent != NIL) {
                    current = parent;
                    parent = nodes[current].Parent;
                }

                Root = current;
            }

            return Root;
        }

        #region Depth
        bool isSetDepth = false;
        public int GetDepth(int id) {
            if (!isSetDepth) {
                var root = GetRoot();
                SetDepth(root, 0);
                isSetDepth = true;
            }

            return nodes[id].Depth;
        }

        private void SetDepth(int id, int depth) {
            nodes[id].Depth = depth;

            if (nodes[id].LeftChild != NIL) {
                SetDepth(nodes[id].LeftChild, depth + 1);
            }

            if (nodes[id].RightChild != NIL) {
                SetDepth(nodes[id].RightChild, depth + 1);
            }
        }
        #endregion

        #region Height
        bool isSetHeight = false;
        /// <summary>高さを得る</summary>
        public int GetHeight(int id) {
            if (!isSetHeight) {
                var root = GetRoot();
                SetHeight(root);
                isSetHeight = true;
            }

            return nodes[id].Height;
        }

        public int SetHeight(int id) {
            var node = nodes[id];
            int hLeft = 0;
            int hRight = 0;

            if (node.LeftChild != NIL) {
                hLeft = SetHeight(node.LeftChild) + 1;
            }

            if (node.RightChild != NIL) {
                hRight = SetHeight(node.RightChild) + 1;
            }

            node.Height = Math.Max(hLeft, hRight);
            return node.Height;
        }
        #endregion

        /// <summary>兄弟を得る</summary>
        public int GetSibling(int id) {
            return nodes[id].Sibling;
        }

        /// <summary>親IDを得る</summary>
        public int GetParent(int id) {
            return nodes[id].Parent;
        }

        public int[] GetChilds(int id) {
            var ids = new List<int>();
            if (nodes[id].LeftChild != NIL) {
                ids.Add(nodes[id].LeftChild);
            }

            if (nodes[id].RightChild != NIL) {
                ids.Add(nodes[id].RightChild);
            }

            return ids.ToArray();
        }

        /// <summary>左子を取得。存在しない時はint.MinValue</summary>
        public int GetLeft(int id) {
            return nodes[id].LeftChild;
        }

        /// <summary>右子を取得。存在しない時はint.MinValue</summary>
        public int GetRight(int id) {
            return nodes[id].RightChild;
        }

        public int NodesCount() {
            return nodes.Count();
        }

        #region 巡回
        private enum OrderType {
            Pre, In, Post
        }

        /// <summary>先行順巡回のNodeIdを取得</summary>
        public IEnumerable<int> GetPreOrder() {
            return Process(OrderType.Pre);
        }

        /// <summary>中間順巡回のNodeIdを取得</summary>
        public IEnumerable<int> GetInOrder() {
            return Process(OrderType.In);
        }

        /// <summary>後方順巡回のNodeIdを取得</summary>
        public IEnumerable<int> GetPostOrder() {
            return Process(OrderType.Post);
        }

        /// <summary>巡回を出力。O(N)</summary>
        private IEnumerable<int> Process(OrderType orderType) {
            var order = new List<int>();
            var stack = new Stack<int>();
            var passed = Enumerable.Range(startAt, nNode).ToDictionary(i => i, i => false); // 巡回済
            var orderAdded = Enumerable.Range(startAt, nNode).ToDictionary(i => i, i => false); // 処理済

            Action<int> addOrder = id => {
                if (!orderAdded[id]) {
                    order.Add(id);
                    orderAdded[id] = true;
                }
            };

            stack.Push(GetRoot());

            while (stack.Count > 0) {
                var current = stack.First();
                passed[current] = true;

                if (orderType == OrderType.Pre) {
                    addOrder(current); // 先行順巡回
                }

                int left = GetLeft(current);
                if (left != -1 && !passed[left]) {
                    stack.Push(left);
                    continue;
                }

                if (orderType == OrderType.In) {
                    addOrder(current); // 中間順巡回
                }

                int right = GetRight(current);
                if (right != -1 && !passed[right]) {
                    stack.Push(right);
                    continue;
                }

                if (orderType == OrderType.Post) {
                    addOrder(current);　 // 後方順巡回
                }

                stack.Pop();
            }

            return order;
        }
        #endregion
    }

    /// <summary>二分探索木</summary>
    public class BinarySearchTree {
        static readonly Node NIL = new Node(int.MinValue);
        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) {
                this.Key = key;
                this.Parent = NIL;
                this.Left = NIL;
                this.Right = NIL;
            }
        }

        Node root = NIL;

        #region Operate
        public void Insert(int key) {
            var z = new Node(key);

            var y = NIL;    // xの親
            var x = root;   // Treeの根

            while (x != NIL) {
                y = x;
                if (z.Key < x.Key) {
                    x = x.Left;
                } else {
                    x = x.Right;
                }
            }

            z.Parent = y;

            if (y == NIL) {
                root = z;
            } else if (z.Key < y.Key) {
                y.Left = z;
            } else {
                y.Right = z;
            }
        }
        public bool Find(int key) {
            var nearLeaf = GetNearLeaf(key);
            return nearLeaf.Key == key;
        }

        private Node GetNearLeaf(int key) {
            var x = this.root;
            while (x != NIL && x.Key != key) {
                if (x.Key < key) {
                    x = x.Right;
                } else {
                    x = x.Left;
                }
            }

            return x;
        }

        public void Delete(int key) {
            var cand = GetNearLeaf(key);
            if (cand.Key != key) {
                throw new KeyNotFoundException();
            }

            DeleteNode(cand);
        }

        private void DeleteNode(Node target) { 
            // 削除対象ノードを選択
            Node delTarget;
            if (target.Left == NIL || target.Right == NIL) {
                delTarget = target;
            } else {
                delTarget = NextInOrder(target);
            }

            // DelTargetの親の新規子を決める
            Node newChild; 
            if (delTarget.Left != NIL) {
                newChild = delTarget.Left;
            } else {
                newChild = delTarget.Right;
            }

            // 新規子 => 親をつなぐ
            if (newChild != NIL) {
                newChild.Parent = delTarget.Parent;
            }

            // 親 => 新規子をつなぐ
            var parent = delTarget.Parent;
            if (parent == NIL) {
                root = newChild;
            } else if (parent.Left == delTarget) {
                parent.Left = newChild;
            } else if (parent.Right == delTarget) {
                parent.Right = newChild;
            }

            // 消したノードの値を渡されたNodeに入れる
            if (delTarget != target) {
                target.Key = delTarget.Key;
            }
        }

        /// <summary>次の中間順巡回のNodeを取得</summary>
        /// <returns>NILの時は木が空</returns>
        private Node NextInOrder(Node node) {
            if (node.Right == NIL) {
                var current = node;
                var parent = node.Parent;

                // 親をたどっていき、初めて左子となっているNodeを取得
                while (parent != NIL && current == parent.Right) {
                    current = parent;
                    parent = current.Parent;
                }

                return node.Parent;
            }

            // 右部分木より最左を取得
            var root = node.Right;
            while (root.Left != NIL) {
                root = root.Left;
            }

            return root;
        }

        /// <summary>
        /// targetの親の子を、新しい子につなぎ替える
        /// </summary>
        /// <param name="target">対象のTarget</param>
        /// <param name="newChild">つなぎ変えた先の子</param>
        private static void ChangeParentOfChild(Node target, Node newChild) {
            var parent = target.Parent;
            if (parent.Left == target) {
                parent.Left = newChild;
            } else if (parent.Right == target) {
                parent.Right = newChild;
            } else {
                throw new KeyNotFoundException();
            }
        }

        #endregion

        #region Order
        public IEnumerable<int> GetInOrder() {
            var orderGetter = new OrderGetter();
            return orderGetter.GetInOrder(root);
        }

        public IEnumerable<int> GetPreOrder() {
            var orderGetter = new OrderGetter();
            return orderGetter.GetPreOrder(root);
        }

        class OrderGetter {
            private List<int> process = new List<int>();

            public IEnumerable<int> GetInOrder(Node root) {
                SetInOrder(root);
                return process;
            }

            public IEnumerable<int> GetPreOrder(Node root) {
                SetPreOrder(root);
                return process;
            }

            private void SetInOrder(Node node) {
                if (node == NIL) {
                    return;
                }

                SetInOrder(node.Left);
                process.Add(node.Key);
                SetInOrder(node.Right);
            }

            private void SetPreOrder(Node node) {
                if (node == NIL) {
                    return;
                }
                process.Add(node.Key);
                SetPreOrder(node.Left);
                SetPreOrder(node.Right);
            }
        }
        #endregion
    }
}


Measured memory_bytes: 68036