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: Time Limit Exceeded)

#include <stdio.h>
struct binary_tree{
    int n;
    int l;
    int r;
};
binary_tree bt[1000000];
int now=0,empty[1000000];
void push(int n,int t){
    if(now==0){
        bt[0].n=n;
        now++;
        return;
    }
    if(bt[t].n<n){
        if(bt[t].r==0){
            bt[t].r=empty[now];
            bt[bt[t].l].r=bt[bt[t].r].l=0;
            bt[bt[t].r].n=n;
            now++;
            return;
        }
        else push(n,bt[t].r);
    }
    if(bt[t].n>n){
        if(bt[t].l==0){
            bt[t].l=empty[now];
            bt[bt[t].l].n=n;
            bt[bt[t].l].r=bt[bt[t].r].l=0;
            now++;
            return;
        }
        else push(n,bt[t].l);
    }
}
void dfs(int n){
    printf(" %d",bt[n].n);
    if(bt[n].l!=0){
        dfs(bt[n].l);
    }
    if(bt[n].r!=0){
        dfs(bt[n].r);
    }
}
void print_sorted(int n){
    if(bt[n].l!=0){
        print_sorted(bt[n].l);
    }
    printf(" %d",bt[n].n);
    if(bt[n].r!=0){
        print_sorted(bt[n].r);
    }
}
bool find(int n,int t){
    if(bt[t].n==n)return true;
    if(bt[t].n<n){
        if(bt[t].r!=0)return find(n,bt[t].r);
        return false;
    }
    if(bt[t].l!=0)return find(n,bt[t].l);
    return false;
}
void delete_item(int n,int t,int p){
    if(bt[t].n==n){
        if(bt[t].r!=0&&bt[t].l!=0){
            int big=bt[t].r,bp=t;
            while(bt[big].l!=0){
                bp=big;
                big=bt[big].l;
            }
            bt[t].n=bt[big].n;
            if(bp!=t)bt[bp].l=bt[big].r;
            else bt[bp].r=bt[big].r;
            now--;
            empty[now]=big;
            return;
        }
        if(bt[t].r==0){
            if(p==-1){
                now--;
                empty[now]=bt[t].l;
                bt[t]=bt[bt[t].l];
                return;
            }
            if(bt[p].n>n)bt[p].l=bt[t].l;
            else bt[p].r=bt[t].l;
            now--;
            empty[now]=t;
            return;
        }
        if(bt[t].l==0){
            if(p==-1){
                now--;
                empty[now]=bt[t].r;
                bt[t]=bt[bt[t].r];
                return;
            }
            if(bt[p].n>n)bt[p].l=bt[t].r;
            else bt[p].r=bt[t].r;
            now--;
            empty[now]=t;
            return;
        }
    }
    else if(bt[t].n>n){
        delete_item(n,bt[t].l,t);
        return;
    }
    else delete_item(n,bt[t].r,t);
}
int main(){
    int n,temp;
    char a[1000];
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        empty[i]=i;
        bt[i].n=bt[i].l=bt[i].r=0;
        scanf("%s",a);
        if(a[0]=='i'){
            scanf("%d",&temp);
            push(temp,0);
        }
        if(a[0]=='p'){
            print_sorted(0);
            printf("\n");
            dfs(0);
            printf("\n");
        }
        if(a[0]=='f'){
            scanf("%d",&temp);
            if(find(temp,0))printf("yes\n");
            else printf("no\n");
        }
        if(a[0]=='d'){
            scanf("%d",&temp);
            delete_item(temp,0,-1);
        }
    }
}