def insert(tree,k):
    y = None
    x = tree
    z = {'key':k, 'parent':None, 'left':None, 'right':None}
    while x != None:
        y = x
        if x['key'] > k:
            x = x['left']
        else:
            x = x['right']
    z['parent'] = y
    if y == None:
        tree = z
    elif y['key'] > k:
        y['left'] = z
    else:
        y['right'] = z
    return tree

def get_successor(x):
    if x['right'] != None:
        return get_minimum(x['right'])
    else:
        y = x['parent']
        while y != None and x == y['left']:
            x = y
            y = y['parent']
        return y

def get_minimum(x):
    while x['left'] != None:
        x = x['left']
    return x

def delete_node(tree,z):
    if z['left'] == None or z['right'] == None:
        y = z
    else:
        y = get_successor(z)

    if y['left'] != None:
        x = y['left']
    else:
        x = y['right']

    if x != None:
        x['parent'] = y['parent']

    if y['parent'] == None:
        tree = x
    else:
        if y['parent']['left']['key'] == y['key']:
            y['parent']['left'] = x
        else:
            y['parent']['right'] = x

    z['key'] = y['key']

    return tree

def find(tree,k):
    x = tree
    while x != None:
        if x['key'] == k:
            return x
        elif x['key'] > k:
            x = x['left']
        else:
            x = x['right']
    return None

def middleorder(x):
    if x != None:
        middleorder(x['left'])
        print x['key'],
        middleorder(x['right'])

def preorder(x):
    if x != None:
        print x['key'],
        preorder(x['left'])
        preorder(x['right'])



tree = None
m = int(raw_input())
for i in range(m):
    c = raw_input().strip()
    if c[0] == 'i':
        tree = insert(tree, int(c[7:]))
    elif c[0] == 'f':
        x = find(tree, int(c[5:]))
        if x == None:
            print "no"
        else:
            print "yes"
    elif c[0] == 'd':
        z = find(tree, int(c[7:]))
        if not z == None: tree = delete_node(tree,z)
    elif c[0] == 'p':
        print '',
        middleorder(tree)
        print
        print '',
        preorder(tree)
        print

### FIXED CODE ###

def insert(tree,k):
    y = None
    x = tree
    z = {'key':k, 'parent':None, 'left':None, 'right':None}
    while x != None:
        y = x
        if x['key'] > k:
            x = x['left']
        else:
            x = x['right']
    z['parent'] = y
    if y == None:
        tree = z
    elif y['key'] > k:
        y['left'] = z
    else:
        y['right'] = z
    return tree

def get_successor(x):
    if x['right'] != None:
        return get_minimum(x['right'])
    else:
        y = x['parent']
        while y != None and x == y['left']:
            x = y
            y = y['parent']
        return y

def get_minimum(x):
    while x['left'] != None:
        x = x['left']
    return x

def delete_node(tree,z):
    if z['left'] == None or z['right'] == None:
        y = z
    else:
        y = get_successor(z)

    if y['left'] != None:
        x = y['left']
    else:
        x = y['right']

    if x != None:
        x['parent'] = y['parent']

    if y['parent'] == None:
        tree = x
    else:
        if y['parent']['left'] != None and y['parent']['left']['key'] == y['key']:
            y['parent']['left'] = x
        else:
            y['parent']['right'] = x

    z['key'] = y['key']

    return tree

def find(tree,k):
    x = tree
    while x != None:
        if x['key'] == k:
            return x
        elif x['key'] > k:
            x = x['left']
        else:
            x = x['right']
    return None

def middleorder(x):
    if x != None:
        middleorder(x['left'])
        print x['key'],
        middleorder(x['right'])

def preorder(x):
    if x != None:
        print x['key'],
        preorder(x['left'])
        preorder(x['right'])



tree = None
m = int(raw_input())
for i in range(m):
    c = raw_input().strip()
    if c[0] == 'i':
        tree = insert(tree, int(c[7:]))
    elif c[0] == 'f':
        x = find(tree, int(c[5:]))
        if x == None:
            print "no"
        else:
            print "yes"
    elif c[0] == 'd':
        z = find(tree, int(c[7:]))
        if not z == None: tree = delete_node(tree,z)
    elif c[0] == 'p':
        print '',
        middleorder(tree)
        print
        print '',
        preorder(tree)
        print