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 C# (judge verdict: Runtime Error)

using System;

namespace B
{
    class Node
    {
        public int key = 2000000001;
        public int id;
        public int left = 2000000001;
        public int right = 2000000001;
        public int parent = 2000000001;
        public void node()
        {
            key = 2000000001;
        }
    }

    class Tree
    {
        public Node[] node = new Node[500001];
    }

    class Output
    {
        public void insert(ref Tree tree, int key, int nowid)
        {
            if (tree.node[0].key == 2000000001)
            {
                tree.node[0].key = key;
            }
            else
            {
                int x = 0, parent = 0;
                while (x != 2000000001)
                {
                    parent = tree.node[x].id;
                    if (key < tree.node[x].key)
                    {
                        x = tree.node[x].left;
                    }
                    else
                    {
                        x = tree.node[x].right;
                    }
                }

                if (key < tree.node[parent].key)
                {                    
                    tree.node[parent].left = nowid;
                }
                else
                {
                    tree.node[parent].right = nowid;
                }
                tree.node[nowid].parent = parent;
                tree.node[nowid].key = key;
            }
        }

        public void delete(ref Tree tree, int key)
        {
            int id = delfind(tree, key, 0);

            if (tree.node[id].left == 2000000001 && tree.node[id].right == 2000000001)
            {
                if (tree.node[tree.node[id].parent].key > tree.node[id].key)
                {
                    tree.node[tree.node[id].parent].left = 2000000001;
                }
                else
                {
                    tree.node[tree.node[id].parent].right = 2000000001;
                }
            }
            else if (tree.node[id].left != 2000000001 && tree.node[id].right == 2000000001)
            {
                if (tree.node[tree.node[id].parent].key > tree.node[id].key)
                {
                    tree.node[tree.node[id].parent].left = tree.node[id].left;
                    tree.node[tree.node[id].left].parent = tree.node[id].parent;
                }
                else
                {
                    tree.node[tree.node[id].parent].right = tree.node[id].left;
                    tree.node[tree.node[id].right].parent = tree.node[id].parent;
                }
            }
            else if (tree.node[id].left == 2000000001 && tree.node[id].right != 2000000001)
            {
                if (tree.node[tree.node[id].parent].key > tree.node[id].key)
                {
                    tree.node[tree.node[id].parent].left = tree.node[id].right;
                    tree.node[tree.node[id].left].parent = tree.node[id].parent;
                }
                else
                {
                    tree.node[tree.node[id].parent].right = tree.node[id].right;
                    tree.node[tree.node[id].right].parent = tree.node[id].parent;
                }
            }
            else
            {
                tree.node[id].key = tree.node[tree.node[id].parent].key;
                delete(ref tree, key);
            }
        }

        public int delfind(Tree tree, int key, int id)
        {
            if (key < tree.node[id].key)
            {
                id = delfind(tree, key, tree.node[id].left);
            }
            else if(key > tree.node[id].key){
                id = delfind(tree, key, tree.node[id].right);
            }
            return id;
        }

        public void inorder(Tree tree, int key)
        {
            if (tree.node[key].left != 2000000001)
            {
                inorder(tree, tree.node[key].left);
            }
            Console.Write(" " + tree.node[key].key);
            if (tree.node[key].right != 2000000001)
            {
                inorder(tree, tree.node[key].right);
            }
        }

        public void preorder(Tree tree, int key)
        {
            Console.Write(" " + tree.node[key].key);
            if (tree.node[key].left != 2000000001)
            {
                preorder(tree, tree.node[key].left);
            }
            if (tree.node[key].right != 2000000001)
            {
                preorder(tree, tree.node[key].right);
            }
        }

        public void find(Tree tree, int key, int id)
        {
            if (id == 2000000001)
            {
                Console.WriteLine("no");
            }
            else if (tree.node[id].key == key)
            {
                Console.WriteLine("yes");
            }
            else if (key < tree.node[id].key)
            {
                find(tree, key, tree.node[id].left);
            }
            else
            {
                find(tree, key, tree.node[id].right);
            }
        }
    }

    class C
    {
        static void Main(string[] args)
        {
            string input1 = Console.ReadLine();
            int n = int.Parse(input1), i;
            string[] input2 = new string[2];
            Output output = new Output();
            Tree tree = new Tree();
            int count = 0;
            for (i = 0; i < tree.node.Length; i++)
            {
                tree.node[i] = new Node();
                tree.node[i].id = i;
            }
            for (i = 0; i < n; i++)
            {
                input2 = Console.ReadLine().Split(' ');
                if (input2[0] == "print")
                {
                    output.inorder(tree, 0);
                    Console.WriteLine();
                    output.preorder(tree, 0);
                    Console.WriteLine();
                }
                else if (input2[0] == "find")
                {
                    output.find(tree, int.Parse(input2[1]), 0);
                }
                else if(input2[0] == "delete")
                {
                    output.delete(ref tree, int.Parse(input2[1]));
                }
                else
                {
                    output.insert(ref tree, int.Parse(input2[1]), count);
                    count++;
                }
            }
        }
    }
}