/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/* 
 * File:   main.c
 * Author: Mu-Chan
 *
 * Created on August 8, 2018, 7:20 AM
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct Node Node;

struct Node{
    int data;
    Node *parent;
    Node *left;
    Node *right;
};

void initNode(Node*);
Node copyNode(Node*,Node*);
Node* getSuccessor(Node*);
Node* getMinNode(Node*);
Node* getMaxNode(Node*);
void insert(Node*,Node*[],int);
void delete(Node*,Node*);
void printTree(Node*);
Node* find(Node*,int);
void inorder(Node*);
void preorder(Node*);


int main(int argc, char** argv) {
    int commandSize;
    scanf("%d",&commandSize);
    Node *tree[commandSize-1];
    for(int i = 0; i < commandSize; i++)
        tree[i] = NULL;
    int loc = 0;
    int j, findVal;
    Node temp[commandSize-1];
    Node *successor = NULL;
    Node *delNode;
    char cmd[7];
    for(int i = 0; i < commandSize; i++){
        initNode(&temp[i]);
        scanf("%s",cmd);
        if(!strcmp(cmd,"insert")){
            scanf("%d", &temp[i].data);
            insert(&temp[i],tree,loc++);
        }
        else if(!strcmp(cmd,"print")){
            if(tree[0]==NULL){
                j = 0;
                while(j < 4){           //First four pointers disappear, repoint objects
                    if(tree[j]==NULL)
                        tree[j] = &temp[j];
                    j++;
                }
            }
            printTree(tree[0]);
        }
        else if(!strcmp(cmd,"find")){
            scanf("%d",&findVal);
            if(find(tree[0],findVal)!=NULL)
                printf("yes\n");
            else
                printf("no\n");
        }
        else{
            scanf("%d", &findVal);
            delNode = find(tree[0],findVal);
            successor = getSuccessor(delNode);
            delete(delNode,successor);
        }
    }
    return (EXIT_SUCCESS);
}

void initNode(Node *n){
    n->data = -1;
    n->parent = NULL;
    n->left = NULL;
    n->right = NULL;
}

void insert(Node *node, Node *tree[], int loc){
    Node *parent = NULL;
    Node *current = tree[0];
    while(current != NULL){
        parent = current;
        if(node->data < current->data)
            current = current->left;
        else
            current = current->right;
    }
    node->parent = parent;
    if(parent == NULL){
        tree[loc] = node;
    }
    else if(node->data < parent->data){
        tree[loc] = node;
        parent->left = tree[loc];
    }
    else{
        tree[loc] = node;
        parent->right = tree[loc];
    }
}

Node copyNode(Node *dest, Node *orig){
    dest->data = orig->data;
    dest->parent = orig->parent;
    dest->left = orig->left;
    dest->right = orig->right;
}

Node* getSuccessor(Node *root){
    if(root->right != NULL){
        if(root->left == NULL)
            return root->right;
        return getMinNode(root->right);
    }
    if(root->left != NULL){
        if(root->right == NULL)
            return root->left;
        return getMaxNode(root->left);
    }
    return NULL;
}

Node* getMinNode(Node *root){
    while(root->left != NULL)
        root = root->left;
    return root;
}

Node* getMaxNode(Node *root){
    while(root->right != NULL)
        root = root->right;
    return root;
}

void delete(Node *target, Node *successor){
    Node *parent = target->parent;
    if(successor != NULL){
        if(target->right != NULL && target->left != NULL){
            if(successor->parent->left == successor)
                successor->parent->left = NULL;
            if(successor->parent->right == successor)
                successor->parent->right = NULL;
            successor->right = target->right;
            successor->left = target->left;
        }
        successor->parent = parent;
    }
    if(parent == NULL)
        return;
    if(parent->left == target)
        parent->left = successor;
    else
        parent->right = successor;
    initNode(target);
}

void printTree(Node *root){
    inorder(root);
    printf("\n");
    preorder(root);
    printf("\n");
}

Node* find(Node *root, int val){
    if(root != NULL){
        if(val < root->data)
            return find(root->left,val);
        else if(val > root->data)
            return find(root->right,val);
        else if(val == root->data)
            return root;
    }
    else
        return NULL;
}

void inorder(Node *root){
    if(root->left != NULL)
        inorder(root->left);
    printf(" %d", root->data);
    if(root->right != NULL)
        inorder(root->right);
}

void preorder(Node *root){
    printf(" %d", root->data);
    if(root->left != NULL)
        preorder(root->left);
    if(root->right != NULL)
        preorder(root->right);
}



Measured memory_bytes: 21296