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: Accepted)

//
// Created by kota_ on 2019/12/30.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define True 1
#define False 0

typedef struct node{
    int key;
    struct node *parent;
    struct node *left;
    struct node *right;
}Node;

typedef Node* Nodepointer;
void print_preorder(Nodepointer root);
void print_inorder(Nodepointer root);
void clear(Nodepointer root);
Nodepointer find(Nodepointer root, int key);
void delete(Nodepointer);

Nodepointer make_1node(int key){
    Nodepointer ret = malloc(sizeof(Node));
    ret->key = key;
    ret->parent = NULL;
    ret->left = NULL;
    ret->right = NULL;
    return ret;
}


void insert(Nodepointer *Tree, int key){
    Nodepointer x, y, z;
    y = NULL;
    x = *Tree;
    while(x != NULL){
        y = x;
        if(key < x->key){
            x = x->left;
        }
        else{
            x = x->right;
        }
    }
    z = make_1node(key);
    z->parent = y;

    if(y == NULL){
        *Tree = z;
    }
    else if(key < y->key){
        y->left = z;
    }
    else{
        y->right = z;
    }

}




int main(){
    int i, n, key;
    char command[7];
    Nodepointer Tree = NULL, tmp;
    scanf("%d", &n);
    for(i = 0; i < n; i++){
        scanf("%s", command);
        if(strcmp("insert", command) == 0){
            scanf("%d", &key);
            insert(&Tree, key);
        }
        else if(strcmp("find", command) == 0){
            scanf("%d", &key);
            if(find(Tree, key) != NULL){
                printf("yes\n");
            }
            else{
                printf("no\n");
            }
        }
        else if(strcmp("delete", command) == 0){
            scanf("%d", &key);
            tmp = find(Tree, key);
            delete(tmp);
        }
        else if(strcmp("print", command) == 0){
            print_inorder(Tree);
            printf("\n");
            print_preorder(Tree);
            printf("\n");
        }
    }
    clear(Tree);
    return 0;
}



void print_preorder(Nodepointer root){
    if(root == NULL){
        return;
    }
    printf(" %d", root->key);
    print_preorder(root->left);
    print_preorder(root->right);
}

void print_inorder(Nodepointer root) {
    if(root == NULL){
        // 安全対策、ヨシ！
        return;
    }
    if(root->left != NULL){
        print_inorder(root->left);
    }
    printf(" %d", root->key);
    if(root->right != NULL){
        print_inorder(root->right);
    }
}

void clear(Nodepointer root){
    if(root == NULL){
        // 安全対策、ヨシ！
        return;
    }
    clear(root->right);
    clear(root->left);
    free(root);
}

Nodepointer find(Nodepointer root, int key) {
    if(root == NULL){
        return NULL;
    }
    if(root->key == key){
        return root;
    }
    if(root->key > key){
        return find(root->left, key);
    }
    else{
        return find(root->right, key);
    }
}

void delete(Nodepointer target) {
    Nodepointer new_target;
    if(target == NULL){
        //見つからないってことはないと思うが、制約に書いてないので一応
        return;
    }
    // 1.子がいなかった場合
    if(target->right == NULL && target->left == NULL){
        // 親いなかった場合（まず無いと信じたい
        if(target->parent==NULL){

        }
        //左の子として登録されていた場合(キー重複ないので、大小比較でわかる）
        else if(target->parent->key > target->key){
            target->parent->left = NULL;
        }
        // 右の子として登録されていた場合
        else{
            target->parent->right = NULL;
        }
    }
    // 2.子が2人の場合
    else if(target->left != NULL && target->right != NULL){
        //まずは新しいターゲットを見つける。
        //zの次節点
        //→中間順巡回でzの次に得られる節点
        //→最初に右の子供に移動してそこから行けるところまで左に行く
        for(new_target = target->right;
                new_target->left != NULL;
                new_target = new_target->left){
            // pass
        }
        target->key = new_target->key;
        delete(new_target);
        // この場合、メモリ解放は必要無いのでreturn
        return;
    }
    // 3.子が一人の場合（左)
    else if(target->left != NULL){
        //左の子として登録されていた場合(キー重複ないので、大小比較でわかる）
        if(target->parent->key > target->key){
            target->parent->left = target->left;
            target->left->parent = target->parent;
        }
            // 右の子として登録されていた場合
        else{
            target->parent->right = target->left;
            target->left->parent = target->parent;
        }
    }
    // 3.子が一人の場合（右)
    else if(target->right != NULL) {
        //左の子として登録されていた場合(キー重複ないので、大小比較でわかる）
        if (target->parent->key > target->key) {
            target->parent->left = target->right;
            target->right->parent = target->parent;
        }
            // 右の子として登録されていた場合
        else {
            target->parent->right = target->right;
            target->right->parent = target->parent;
        }
    }
    // 1 or 3 の場合、最後にメモリ解放して終わり
    free(target);

}