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: Wrong Answer)

using System;
using System.Text;
using System.Collections.Generic;
 
public class node
{
    public node parent;
    public node rightChild;
    public node leftChild;
    public int key;
}
 
public class Test
{
    public static int[] preorderKey, inorderKey;
    public static int precount, incount;
     
    public static void Main()
    {
		int n = int.Parse(Console.ReadLine());
        List<node> tree = new List<node>();
         
        for(int i = 0; i < n ; i++) {
            string[] atom = Console.ReadLine().Split(" ".ToCharArray());
             
            switch(atom[0][0]){
                case 'i':
                    Insert(int.Parse(atom[1]), tree);
                    break;
				
				case 'f':
					Console.WriteLine( Find( int.Parse(atom[1]), tree[0])? "yes" : "no" );
					break;
				
				case 'd':
					node z = Find( int.Parse(atom[1]), tree);
					Delete(z, tree);
					break;
                 
                case 'p':
                    Print(tree.ToArray());
                    break;
            }
        }
    }
     
    public static void Insert(int v ,List<node> T)
    {
        node y = null;
        
        node z = new node();
		z.key = v;
		T.Add(z);
        
        if(T.Count == 1) return;
        node x = T[0];
 
        while(x != null) {
            y = x;
            if(v < x.key) x = x.leftChild;
            else x = x.rightChild;
        }
        z.parent = y;
         
        if(y == null) {
            
        } else if (v < y.key) {
            y.leftChild = T[T.Count - 1];
        } else {
            y.rightChild = T[T.Count - 1];
        }
    }
	
	public static bool Find(int v, node x)
	{
		while(x != null) {
			if(x.key == v) return true;
			else if(v < x.key) x = x.leftChild;
			else x = x.rightChild;
		}
		
		return false;
	}
	
	public static node Find(int v, List<node> T)
	{
		node x = T[0];
		
		while(x != null) {
			if(x.key == v) return x;
			else if(v < x.key) x = x.leftChild;
			else x = x.rightChild;
		}
		
		return null;
	}
	
	public static void Delete(node z, List<node> T)
	{	
		if(z.leftChild == null && z.rightChild == null) {
			if(z.key < z.parent.key) z.parent.leftChild = null;
			else if(z.key > z.parent.key) z.parent.rightChild = null;
			else {
				if(z == z.parent.leftChild) z.parent.leftChild = null;
				else z.parent.rightChild = null;
			}
			T.Remove(z);
		} else if (z.leftChild == null) {
			if( z == z.parent.rightChild ) z.parent.rightChild = z.rightChild;
			else z.parent.leftChild = z.rightChild;
			z.rightChild.parent = z.parent;
			T.Remove(z);
		} else if (z.rightChild == null) {
			if( z == z.parent.leftChild ) z.parent.leftChild = z.leftChild;
			else z.parent.rightChild = z.leftChild;
			z.leftChild.parent = z.parent;
			T.Remove(z);
		} else {
			node y = z;
			while(y.leftChild != null) {
				y = y.leftChild;
			}
			z.key = y.key;
			Delete(y, T);
		}
	}
 
    public static void Print(node[] T)
    {
        preorderKey = new int[T.Length];
        inorderKey = new int[T.Length];
        precount = 0;
        incount = 0;
         
        node root = T[0];
        Inorder(root, T);
        Preorder(root, T);
        
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < T.Length; i++) {
            sb.Append(" " + inorderKey[i]);
        }
        Console.WriteLine(sb.ToString());
        sb = new StringBuilder();
        for(int i = 0; i < T.Length; i++) {
            sb.Append(" " + preorderKey[i]);
        }
        Console.WriteLine(sb.ToString());
         
    }
     
    public static void Preorder(node t, node[] tree)
    {
        preorderKey[precount++] = t.key;
        if(t.leftChild != null) Preorder(t.leftChild, tree);
        if(t.rightChild != null) Preorder(t.rightChild, tree);
    }
     
    public static void Inorder(node t, node[] tree)
    {
        if(t.leftChild != null) Inorder(t.leftChild, tree);
        inorderKey[incount++] = t.key;
        if(t.rightChild != null) Inorder(t.rightChild, tree);
    }
     
    public static node GetRoot(node[] T)
    {
        if ( T ==  null ) return null;
        if ( T.Length == 0) return null;
         
        node u = T[0];
        while(u.parent != null) {
            u = u.parent;
        }
         
        return u;
    }
}