# http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_8_C
# Binary Search Tree III
# Result:
import sys

class BinarySearchTree(object):
    def __init__(self):
        super(BinarySearchTree, self).__init__()
        self.root = None
        self.count = 0

    def insert(self, val):
        node = Node(val)
        y = None
        x = self.root
        while x != None:
            y = x
            if node.id_ < x.id_:
                x = x.left
            else:
                x = x.right
        node.parent = y

        if y == None:
            self.root = node
        elif node.id_ < y.id_:
            y.left = node
        else:
            y.right = node

    def find(self, val):
        x = self.root
        while x != None:
            if val == x.id_:
                break
            elif val < x.id_:
                x = x.left
            else: # val > x.id_
                x = x.right
        if x != None:
            return True
        else:
            return False

    def delete(self, val):
        p = None
        x = self.root
        while x != None:
            if val == x.id_:
                break
            p = x
            if val < x.id_:
                x = x.left
            else: # val > x.id_
                x = x.right
        if x != None:
            if x.left != None and x.right != None:
                next_ = self._next_node_in_inorder(x)
                x.id_ = next_.id_
                x = next_
                p = next_.parent
            if x.left == None and x.right == None:
                if x == self.root:
                    self.root = None
                else:
                    if p.left == x:
                        p.left = None
                    else: #p.right == x
                        p.right = None
            elif x.left == None: # x.right != None
                x.right.parent = p
                if p != None:
                    if p.left == x:
                        p.left = x.right
                    else:
                        p.right = x.right
                else: # x == self.root
                    self.root = x.right
            elif x.right == None: # x.left != None
                x.left.parent = p
                if p != None:
                    if p.left == x:
                        p.left = x.left
                    else:
                        p.right = x.left
                else: # x == self.root
                    self.root = x.left
            else:
                assert True

    def _next_node_in_inorder(self, node):
        x = node.right
        while x.left != None:
            x = x.left
        return x

    def __str__(self):
        return 'Call to_inorder_walk() or to_preorder_walk()'

    def to_inorder_walk(self):
        return self._to_inorder_walk(self.root)

    def to_preorder_walk(self):
        return self._to_preorder_walk(self.root)

    def _to_inorder_walk(self, node):
        if node == None: return ''
        str_ = self._to_inorder_walk(node.left)
        str_ += ' %d' % (node.id_)
        str_ += self._to_inorder_walk(node.right)
        return str_

    def _to_preorder_walk(self, node):
        if node == None: return ''
        str_ = ' %d' % (node.id_)
        str_ += self._to_preorder_walk(node.left)
        str_ += self._to_preorder_walk(node.right)
        return str_

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

    def __str__(self):
        str_ = '%d' % (self.id_)
        return str_


bst = BinarySearchTree()

_nops = int(sys.stdin.readline())
for _ in range(0, _nops):
    op = sys.stdin.readline().rstrip().split(' ')
    if op[0] == 'insert':
        bst.insert(int(op[1]))
    elif op[0] == 'print':
        print bst.to_inorder_walk()
        print bst.to_preorder_walk()
    elif op[0] == 'find':
        if bst.find(int(op[1])):
            print 'yes'
        else:
            print 'no'
    elif op[0] == 'delete':
        bst.delete(int(op[1]))
    else:
        pass

Measured memory_bytes: 177700