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 JAVA (judge verdict: Accepted)

import java.util.Scanner;
public class Main{
    static tree[] wd;
    static int root =-1;
	public static void main(String[] args) {
	    int i=0,j,n,hz;
	    String x;
	// TODO Auto-generated method stub
     Scanner sca = new Scanner(System.in);
     n=sca.nextInt();
     wd=new tree[n];
    for(j=0;j<n;j++){
       x=sca.next();
    	if(x.equals("insert")){
	    wd[i]=new tree(sca.nextInt());
	    insert(i);
	    i++;
	 }
    	if(x.equals("print")){
    	    inorder(root);
    	    System.out.println("");
    	    preorder(root);
    	    System.out.println("");  		
    	}
	if(x.equals("find")){
	    if(find(sca.nextInt(),root)!=-1){
		System.out.println("yes");
	    }else{
		System.out.println("no");
	    }
	}
	if(x.equals("delete")){
	    hz=find(sca.nextInt(),root);
	    if(hz!=-1)delete(hz);
     	}
    }
   }
    public static void preorder(int nd){
	System.out.print(" "+wd[nd].getnode());
	if(wd[nd].getleft()!=-1)preorder(wd[nd].getleft());
	if(wd[nd].getright()!=-1)preorder(wd[nd].getright());
    }
    public static void inorder(int nd){
	if(wd[nd].getleft()!=-1)inorder(wd[nd].getleft());
	System.out.print(" "+wd[nd].getnode());
	if(wd[nd].getright()!=-1)inorder(wd[nd].getright());
    }
    public static void insert(int data){//NIL->-1
     int y=-1,x=root;//parent of x
     while(x!=-1){
    	y=x;
    	if(wd[data].getnode()<wd[x].getnode()){
    		x=wd[x].getleft();
    	}else{
    		x=wd[x].getright();
    	}
    }
     if (y==-1){//it is root
    	 root=data;
     }else if(wd[data].getnode()<wd[y].getnode()){
    	 wd[y].setleft(data);
	 wd[data].setpar(y);
     }else{
    	 wd[y].setright(data);
	 wd[data].setpar(y);
     }
    }
    public static int find(int key,int opo){//true->index of factor false->-1 
	if(opo==-1)return -1;
	if(key==wd[opo].getnode())return opo;
	if(key<wd[opo].getnode()){
	return find(key,wd[opo].getleft());
	}else{
	return find(key,wd[opo].getright());
	}
    }
    public static int treesuccessor(int x){
	int y;
	if(wd[x].getright()!=-1)return treeminimum(wd[x].getright());
	y=wd[x].getpar();
	while(y!=-1&&x==wd[y].getright()){
	    x=y;
	    y=wd[y].getpar();
	}
	return y;
    }
    public static int treeminimum(int x){
	while(wd[x].getleft()!=-1){
	    x=wd[x].getleft();
	}
	return x;
    }
    public static void delete(int key){
	int y,x;
	if(wd[key].getleft()==-1||wd[key].getright()==-1){
	    y=key;
	}else{
	    y=treesuccessor(key);
	}
	if(wd[y].getleft()!=-1){
	    x=wd[y].getleft();
	}else{
	    x=wd[y].getright();
	}
	if(x!=-1)wd[x].setpar(wd[y].getpar());
	if(wd[y].getpar()==-1){
	    root=x;
	}else if(y==wd[wd[y].getpar()].getleft()){
	    wd[wd[y].getpar()].setleft(x);	  
	}else{
	    wd[wd[y].getpar()].setright(x);	         
	}
	if(y!=key)wd[key].setnode(wd[y].getnode());
	
    }
}
class tree{
    int node,par;
    int left=-1,right=-1;
    tree(int node){
	this.node=node;
    }
    public int getnode(){
	return node;
    }
    public void setnode(int node){
	this.node=node;
    }
    public void setleft(int c){
	left=c;
    }
    public int getleft(){
	return left;
    }
    public void setright(int c){
	right=c;
    }
    public int getright(){
	return right;
    }
    public void setpar(int c){
	par=c;
    }
    public int getpar(){
	return par;
    }

}