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;
 
class Node {
    static Node root;
 
    int key;
    Node left;
    Node right;
    Node p;
 
    Node(int key) {
        this.key = key;
    }
 
    void insert() {
        Node y = null;
        Node x = root;
        while(x != null) {
            y = x;
            if(key < x.key) x = x.left;
            else x = x.right;
        }
        p = y;
 
        if(y == null) root = this;
        else if(key < y.key) y.left = this;
        else y.right = this;
    }
 
    static Node find(int key) {
        Node x = root;
        while(x != null) {
            if(key < x.key) x = x.left;
            else if(x.key < key) x = x.right;
            else return x;
        }
        return null;
    }
 
    void delete() {
        if(left == null && right == null) {
            if(p.left == this) p.left = null;
            else p.right = null;
        }else if(right == null) {
            if(p.left == this) p.left = left;
            else p.right = left;
            left.p = p;
        }else if(left == null) {
            if(p.left == this) p.left = right;
            else p.right = right;
            right.p = p;
        }else {
            Node y = right;
            while(y.left != null) {
                y = y.left;
            }
            key = y.key;
            y.delete();
        }
    }
 
    static String inorder(Node x) {
        String str = "";
        if(x == null) return str;
 
        if(x.left != null) str += inorder(x.left);
        str += " " + x.key;
        if(x.right != null)str += inorder(x.right);
 
        return str;
    }
 
    static String preorder(Node x) {
        String str = "";
        if(x == null) return str;
 
        str += " " + x.key;
        if(x.left != null) str += preorder(x.left);
        if(x.right != null) str += preorder(x.right);
 
        return str;
    }
}
 
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
  
        Node.root = null;
        int m = sc.nextInt();
        for(int i = 0; i < m; i++) {
            String cmd = sc.next();
            if(cmd.equals("insert")) {
                new Node(sc.nextInt()).insert();
            }else if(cmd.equals("find")) {
                System.out.println(Node.find(sc.nextInt()) != null? "yes" : "no");
            }else if(cmd.equals("delete")) {
                Node.find(sc.nextInt()).delete();
            }else {
                System.out.println(Node.inorder(Node.root));
                System.out.println(Node.preorder(Node.root));
            }
        }
 
        sc.close();
    }
}