#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_8_A&lang=jp
# cat alds1_8_c.txt | ./alds1_8_c.py

import sys


class Node(object):
    def __init__(self, key=None, parent=None):
        self.key = key
        self.parent = parent
        self.left = None
        self.right = None


def insert(key, node):
    """
    ??????????????????????????????, ???????????°???????????? node.left ????????? node.right ?????????????????????
    """
    if key < node.key:
        if node.left is None:
            left_node = Node(key, node)
            node.left = left_node
        else:
            insert(key, node.left)
    else:
        if node.right is None:
            right_node = Node(key, node)
            node.right = right_node
        else:
            insert(key, node.right)


def find(key, node):
    """
    ??????????????????????????????, ?????????????????¨????????°?????????????????????????????´?????????
    ????????????????????§??????????????????????????????????????¨??????????????° None ????????????
    """
    if key == node.key:
        return node
    elif key < node.key:
        if node.left is None:
            return None
        else:
            return find(key, node.left)
    else:
        if node.right is None:
            return None
        else:
            return find(key, node.right)


def print_preorder(node):
    print(" %d" % node.key, end="")
    if node.left is not None:
        print_preorder(node.left)
    if node.right is not None:
        print_preorder(node.right)


def print_inorder(node):
    if node.left is not None:
        print_inorder(node.left)
    print(" %d" % node.key, end="")
    if node.right is not None:
        print_inorder(node.right)


def search_minimum_node(node):
    """
    node ???????????¨??????(??¨???)??¨??????????°????????????????????????????????????????
    ?????? node ??????????????±????????´??? leaf ?????¢?´¢??????????????§?????????
    """
    if node.left is not None:
        return search_minimum_node(node.left)
    else:
        return node


def delete(node):
    if node.left is None and node.right is None:
        # ?????? node ??????????????§, ?????? parent ???????????§??§????????????????????????
        if node.parent.left is node:
            node.parent.left = None
        else:
            node.parent.right = None
    elif node.left is None and node.right is not None:
        # node ??????????????????????????§, node ????????? node ????????????????????§?????????????????????
        # node ??????????????? node ???????????????????????¨???????????§?????????????????????
        if node is node.parent.left:
            # node ?????????????????????
            node.parent.left = node.right
        else:
            # node ?????????????????????
            node.parent.right = node.right
        node.right.parent = node.parent
    elif node.left is not None and node.right is None:
        # node ??????????????????????????§, node ????????? node ????????????????????§?????????????????????
        # node ??????????????? node ???????????????????????¨???????????§?????????????????????
        if node is node.parent.left:
            # node ?????????????????????
            node.parent.left = node.left
        else:
            # node ?????????????????????
            node.parent.right = node.left
        node.leftt.parent = node.parent
    else:
        # ????????????????????????????????????
        # ?¬??????? (successor) ????¨??????????
        successor = search_minimum_node(node.right)
        if successor.right is not None:
            # ??????????¬???????????????´??? node ???????????´???, ?????????????????????????????????
            successor.right.parent = successor.parent
            successor.parent.left = successor.right
        else:
            # ?¬???????????????´??? node ???????????´???, ?¬????????????????????????????????????????
            successor.parent.right = None
        # node ??????????¬???????????????§???????????????
        node.key = successor.key


def main():
    lines = sys.stdin.readlines()
    root = Node()

    for order in lines[1:]:
        if order.startswith("insert"):
            _, key = order.strip().split(" ")
            key = int(key)
            if root.key is None:
                root.key = key
            else:
                insert(key, root)
        elif order.startswith("find"):
            _, key = order.strip().split(" ")
            fild_result = "no" if find(int(key), root) is None else "yes"
            print(fild_result)
        elif order.startswith("print"):
            print_inorder(root)
            print("")
            print_preorder(root)
            print("")
        elif order.startswith("delete"):
            _, key = order.strip().split(" ")
            node = find(int(key), root)  # ????????§ node ???????????????????????£?????¨???????????????????§???§????????????
            delete(node)


if __name__ == "__main__":
    main()

### FIXED CODE ###

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_8_A&lang=jp
# cat alds1_8_c.txt | ./alds1_8_c.py

import sys


class Node(object):
    def __init__(self, key=None, parent=None):
        self.key = key
        self.parent = parent
        self.left = None
        self.right = None


def insert(key, node):
    """
    ??????????????????????????????, ???????????°???????????? node.left ????????? node.right ?????????????????????
    """
    if key < node.key:
        if node.left is None:
            left_node = Node(key, node)
            node.left = left_node
        else:
            insert(key, node.left)
    else:
        if node.right is None:
            right_node = Node(key, node)
            node.right = right_node
        else:
            insert(key, node.right)


def find(key, node):
    """
    ??????????????????????????????, ?????????????????¨????????°?????????????????????????????´?????????
    ????????????????????§??????????????????????????????????????¨??????????????° None ????????????
    """
    if key == node.key:
        return node
    elif key < node.key:
        if node.left is None:
            return None
        else:
            return find(key, node.left)
    else:
        if node.right is None:
            return None
        else:
            return find(key, node.right)


def print_preorder(node):
    print(" %d" % node.key, end="")
    if node.left is not None:
        print_preorder(node.left)
    if node.right is not None:
        print_preorder(node.right)


def print_inorder(node):
    if node.left is not None:
        print_inorder(node.left)
    print(" %d" % node.key, end="")
    if node.right is not None:
        print_inorder(node.right)


def search_minimum_node(node):
    """
    node ???????????¨??????(??¨???)??¨??????????°????????????????????????????????????????
    ?????? node ??????????????±????????´??? leaf ?????¢?´¢??????????????§?????????
    """
    if node.left is not None:
        return search_minimum_node(node.left)
    else:
        return node


def delete(node):
    if node.left is None and node.right is None:
        # ?????? node ??????????????§, ?????? parent ???????????§??§????????????????????????
        if node.parent.left is node:
            node.parent.left = None
        else:
            node.parent.right = None
    elif node.left is None and node.right is not None:
        # node ??????????????????????????§, node ????????? node ????????????????????§?????????????????????
        # node ??????????????? node ???????????????????????¨???????????§?????????????????????
        if node is node.parent.left:
            # node ?????????????????????
            node.parent.left = node.right
        else:
            # node ?????????????????????
            node.parent.right = node.right
        node.right.parent = node.parent
    elif node.left is not None and node.right is None:
        # node ??????????????????????????§, node ????????? node ????????????????????§?????????????????????
        # node ??????????????? node ???????????????????????¨???????????§?????????????????????
        if node is node.parent.left:
            # node ?????????????????????
            node.parent.left = node.left
        else:
            # node ?????????????????????
            node.parent.right = node.left
        node.left.parent = node.parent
    else:
        # ????????????????????????????????????
        # ?¬??????? (successor) ????¨??????????
        successor = search_minimum_node(node.right)
        if successor.right is not None:
            # ??????????¬???????????????´??? node ???????????´???, ?????????????????????????????????
            successor.right.parent = successor.parent
            successor.parent.left = successor.right
        else:
            # ?¬???????????????´??? node ???????????´???, ?¬????????????????????????????????????????
            if successor.parent.left is successor:
                successor.parent.left = None
            else:
                successor.parent.right = None
        # node ??????????¬???????????????§???????????????
        node.key = successor.key


def main():
    lines = sys.stdin.readlines()
    root = Node()

    for order in lines[1:]:
        if order.startswith("insert"):
            _, key = order.strip().split(" ")
            key = int(key)
            if root.key is None:
                root.key = key
            else:
                insert(key, root)
        elif order.startswith("find"):
            _, key = order.strip().split(" ")
            fild_result = "no" if find(int(key), root) is None else "yes"
            print(fild_result)
        elif order.startswith("print"):
            print_inorder(root)
            print("")
            print_preorder(root)
            print("")
        elif order.startswith("delete"):
            _, key = order.strip().split(" ")
            node = find(int(key), root)  # ????????§ node ???????????????????????£?????¨???????????????????§???§????????????
            delete(node)


if __name__ == "__main__":
    main()