import sys

def main():
    n = input()
    tree = MyBinaryTree(n)
    for i in range(n):
        cmd = map(str,raw_input().split())
        if cmd[0] == "insert":
            insert(tree,MyNode(int(cmd[1])))
        elif cmd[0] == "print":
            tree.inorder(tree.root)
            print ""
            tree.preorder(tree.root)
            print ""
        elif cmd[0] == "find":
            find(tree,int(cmd[1]))
        elif cmd[0] == "delete":
            delete(tree,int(cmd[1]))
    
    
class MyBinaryTree():
    def __init__(self,num_nodes):
        self.root = None

    def preorder(self,node):
        sys.stdout.write(" {}".format(node.key))
        l = node.left
        r = node.right
        if l is not None:
            self.preorder(l)
        if r is not None:
            self.preorder(r)    

    def inorder(self,node):
        l = node.left
        r = node.right
        if l is not None:
            self.inorder(l)
        sys.stdout.write(" {}".format(node.key))
        if r is not None:
            self.inorder(r)


def insert(T,z):
    y = None
    x = T.root
    while x is not None:
        y = x
        if z.key < x.key:
            x = x.left
        else:
            x = x.right
    z.parent = y

    if y is None:
        T.root = z
    elif z.key < y.key:
        y.left = z
    else:
        y.right = z


def find(T,key):
    x = T.root
    while x is not None and key != x.key:
        if key < x.key:
            x = x.left
        else:
            x = x.right

    if x is not None:
        print "yes"
    else:
        print "no"


def delete(T,key):
    x = T.root
    where = None
    while x is not None and key != x.key:
        if key < x.key:
            x = x.left
            where = 0
        else:
            x = x.right
            where = 1

    if x is not None:
        if x.left is None:
            if x.right is None:
                if where == 0:
                    x.parent.left = None
                else:
                    x.parent.right = None
            elif where == 0:
                x.parent.left  = x.right
                x.right.parent = x.parent
            else:
                x.parent.right = x.right
                x.right.parent = x.parent
            x = None
        elif x.rightt is None :
            if where == 0:
                x.parent.left = x.left
                x.left.parent = x.parent
            else:
                x.parent.right = x.left
                x.left.parent  = x.parent
            x = None
        else:
            y = x.right
            while y.left is not None:
                y = y.left

            x.key = y.key
            if y.right is not None:
                y.right.parent = y.parent
                y.parent.left  = y.right
            else:
                y.parent.left = None
            y = None
            
        
class MyNode():
    def __init__(self,key):
        self.key =key
        self.parent = None
        self.left = None
        self.right = None


if __name__ == "__main__":
    main()

### FIXED CODE ###

import sys

def main():
    n = input()
    tree = MyBinaryTree(n)
    for i in range(n):
        cmd = map(str,raw_input().split())
        if cmd[0] == "insert":
            insert(tree,MyNode(int(cmd[1])))
        elif cmd[0] == "print":
            tree.inorder(tree.root)
            print ""
            tree.preorder(tree.root)
            print ""
        elif cmd[0] == "find":
            find(tree,int(cmd[1]))
        elif cmd[0] == "delete":
            delete(tree,int(cmd[1]))
    
    
class MyBinaryTree():
    def __init__(self,num_nodes):
        self.root = None

    def preorder(self,node):
        sys.stdout.write(" {}".format(node.key))
        l = node.left
        r = node.right
        if l is not None:
            self.preorder(l)
        if r is not None:
            self.preorder(r)    

    def inorder(self,node):
        l = node.left
        r = node.right
        if l is not None:
            self.inorder(l)
        sys.stdout.write(" {}".format(node.key))
        if r is not None:
            self.inorder(r)


def insert(T,z):
    y = None
    x = T.root
    while x is not None:
        y = x
        if z.key < x.key:
            x = x.left
        else:
            x = x.right
    z.parent = y

    if y is None:
        T.root = z
    elif z.key < y.key:
        y.left = z
    else:
        y.right = z


def find(T,key):
    x = T.root
    while x is not None and key != x.key:
        if key < x.key:
            x = x.left
        else:
            x = x.right

    if x is not None:
        print "yes"
    else:
        print "no"


def delete(T,key):
    x = T.root
    where = None
    while x is not None and key != x.key:
        if key < x.key:
            x = x.left
            where = 0
        else:
            x = x.right
            where = 1

    if x is not None:
        if x.left is None:
            if x.right is None:
                if where == 0:
                    x.parent.left = None
                else:
                    x.parent.right = None
            elif where == 0:
                x.parent.left  = x.right
                x.right.parent = x.parent
            else:
                x.parent.right = x.right
                x.right.parent = x.parent
            x = None
        elif x.right is None :
            if where == 0:
                x.parent.left = x.left
                x.left.parent = x.parent
            else:
                x.parent.right = x.left
                x.left.parent  = x.parent
            x = None
        else:
            y = x.right
            where = 1
            while y.left is not None:
                y = y.left
                where = 0

            x.key = y.key
            if y.right is not None:
                y.right.parent = y.parent
                y.parent.left  = y.right
            else:
                if where == 0:
                    y.parent.left = None
                else:
                    y.parent.right = None
            y = None
            
        
class MyNode():
    def __init__(self,key):
        self.key =key
        self.parent = None
        self.left = None
        self.right = None


if __name__ == "__main__":
    main()