Problem:
Binary Search Tree III

Write a program which performs the following operations to a binary search tree $T$ by adding delete operation to B: Binary Search Tree II.

insert $k$: Insert a node containing $k$ as key into $T$.

find $k$: Report whether $T$ has a node containing $k$.

delete $k$: Delete a node containing $k$.

print: Print the keys of the binary search tree by inorder tree walk and preorder tree walk respectively.

The operation delete $k$ for deleting a given node $z$ containing key $k$ from $T$ can be implemented by an algorithm which considers the following cases:

If $z$ has no children, we modify its parent $z.p$ to replace $z$ with NIL as its child (delete $z$).

If $z$ has only a single child, we "splice out" $z$ by making a new link between its child and its parent.

If $z$ has two children, we splice out $z$'s successor $y$ and replace $z$'s key with $y$'s key.

Input

In the first line, the number of operations $m$ is given. In the following $m$ lines, operations represented by insert $k$, find $k$, delete $k$ or print are given.

Output

For each find $k$ operation, print "yes" if $T$ has a node containing $k$, "no" if not.

In addition, for each print operation, print a list of keys obtained by inorder tree walk and preorder tree walk in a line respectively. Put a space character before each key

Constraints

The number of operations $\leq 500,000$

The number of print operations $\leq 10$.

$-2,000,000,000 \leq key \leq 2,000,000,000$

The height of the binary tree does not exceed 100 if you employ the above pseudo code.

The keys in the binary search tree are all different.

Sample Input 1

18
insert 8
insert 2
insert 3
insert 7
insert 22
insert 1
find 1
find 2
find 3
find 4
find 5
find 6
find 7
find 8
print
delete 3
delete 7
print

Sample Output 1

yes
yes
yes
no
no
no
yes
yes
1 2 3 7 8 22
8 2 1 3 7 22
1 2 8 22
8 2 1 22

Reference

Introduction to Algorithms, Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. The MIT Press.

Solution in Python (judge verdict: Wrong Answer)

import sys
n_order = int(raw_input())


class node:

    def __init__(self, num, parent):
        self.num = num
        self.left = None
        self.right = None
        self.parent = None


def insert(num, c_node):
    if num >= c_node.num:
        if c_node.right == None:
            # print num, c_node.num, "right"
            c_node.right = node(num, c_node)
            c_node.right.parent = c_node
        elif c_node.right != None:
            insert(num, c_node.right)
    elif num < c_node.num:
        if c_node.left == None:
            c_node.left = node(num, c_node)
            c_node.left.parent = c_node
            # print num, c_node.num, "left"
        elif c_node.left != None:
            insert(num, c_node.left)


def c_print(c_node):
    sys.stdout.write(str(c_node.num) + " ")
    if c_node.left != None:
        c_print(c_node.left)
    if c_node.right != None:
        c_print(c_node.right)


def p_print(c_node):
    if c_node.left != None:
        p_print(c_node.left)
    sys.stdout.write(str(c_node.num) + " ")
    if c_node.right != None:
        p_print(c_node.right)


def find(num, c_node):
    if num == c_node.num:
        print "yes"
    elif num > c_node.num:
        if c_node.right == None:
            print "no"
        elif c_node.right != None:
            find(num, c_node.right)
    elif num < c_node.num:
        if c_node.left == None:
            print "no"
        elif c_node.left != None:
            find(num, c_node.left)


def find_return(num, c_node):
    if num == c_node.num:
        return c_node
    elif num > c_node.num:
        if c_node.right == None:
            return None
        elif c_node.right != None:
            return find_return(num, c_node.right)
    elif num < c_node.num:
        if c_node.left == None:
            return None
        elif c_node.left != None:
            return find_return(num, c_node.left)


def find_midorder_return(c_node):
    if c_node.left == None:
        return c_node
    else:
        return find_midorder_return(c_node.left)


def delete(num, c_node):
    del_node = find_return(num, c_node)
    if del_node.left == None and del_node.right == None:
        if del_node.parent == None:
            c_node = None
        elif del_node.parent.num > del_node.num:
            del_node.parent.left = None
        elif del_node.parent.num < del_node.num:
            del_node.parent.right = None
    elif del_node.left == None:
        if del_node.parent == None:
            c_node = del_node.right
        elif del_node.parent.num > del_node.num:
            del_node.parent.left = del_node.right
            del_node.right.parent = del_node.parent
        elif del_node.parent.num < del_node.num:
            del_node.parent.right = del_node.right
            del_node.right.parent = del_node.parent
    elif del_node.right == None:
        if del_node.parent == None:
            c_node = del_node.left
        elif del_node.parent.num > del_node.num:
            del_node.parent.left = del_node.left
            del_node.left.parent = del_node.parent
        elif del_node.parent.num < del_node.num:
            del_node.parent.right = del_node.left
            del_node.left.parent = del_node.parent
    else:
        deep_left_node = find_midorder_return(del_node)
        if deep_left_node.right != None:
            if del_node.parent == None:
                pass
            elif deep_left_node.parent.num > deep_left_node.num:
                deep_left_node.parent.left = deep_left_node.right
                deep_left_node.right.parent = deep_left_node.parent
            elif del_node.parent.num < deep_left_node.num:
                deep_left_node.parent.right = deep_left_node.right
                deep_left_node.right.parent = deep_left_node.parent
        deep_left_node.parent.left = None
        del_node.num = deep_left_node.num


root = node(None, None)
read_count = 0
while True:
    read_count += 1
    order = raw_input()
    splited_order = order.split(" ")
    if splited_order[0] == "insert":
        root.num = int(splited_order[1])
        break
print "root:", root.num

for i in range(n_order - read_count):
    order = raw_input()
    splited_order = order.split(" ")
    if splited_order[0] == "insert":
        insert(int(splited_order[1]), root)
    elif splited_order[0] == "find":
        find(int(splited_order[1]), root)
    elif splited_order[0] == "delete":
        delete(int(splited_order[1]), root)
    elif splited_order[0] == "print":
        sys.stdout.write(" ")
        p_print(root)
        print ""
        sys.stdout.write(" ")
        c_print(root)
        print ""