#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstdlib>
#include <string>
#include <iostream>

using namespace std;

struct Node {
	int key;
	Node *right, *left, *parent;
};

Node *root, *NIL;

Node *treeSuccessor(Node *x);

void treeDelete(Node *z) {
	bool isLeftChild = true;
	Node *y;

	if (z == root) {
		if (z->left == NIL && z->right == NIL) { //no child
			root = NIL;
			free(z);
			return;
		}
		else if (z->left == NIL) { //right child
			root = z->right;
			root->parent = NIL;
			free(z);
			return;
		}
		else if (z->right == NIL) { //left child
			root = z->left;
			root->parent = NIL;
			free(z);
			return;
		}
		else { //2 children
			y = treeSuccessor(z);
			z->key = y->key;
			treeDelete(y);
			return;
		}
	}

	//z is not a root of Tree

	if (z->parent->right == z) {
		isLeftChild = false;
	}

	if (z->left == NIL && z->right == NIL) { //no child
		if (isLeftChild) {
			z->parent->left = NIL;
		}
		else {
			z->parent->right = NIL;
		}
		free(z);
		return;
	}
	else if (z->left == NIL) { //right child
		if (isLeftChild) {
			z->parent->left = z->right;
			z->right->parent = z->parent;
		}
		else {
			z->parent->right = z->right;
			z->right->parent = z->parent;
		}
		free(z);
		return;
	}
	else if (z->right == NIL) { //left child
		if (isLeftChild) {
			z->parent->left = z->left;
			z->left->parent = z->parent;
		}
		else {
			z->parent->right = z->left;
			z->left->parent = z->parent;
		}
		free(z);
		return;
	}
	else { //2 children
		y = treeSuccessor(z);
		z->key = y->key;
		treeDelete(y);
		return;
	}
}

Node *treeMinimum(Node *x) {
	if (x->left != NIL) {
		return treeMinimum(x->left);
	}
	else {
		return x;
	}
}

Node *treeSuccessor(Node *x) {
	if (x->right != NIL) {
		return treeMinimum(x->right);
	}
	Node *y = x->parent;
	while (y != NIL) {
		if (y->left == x) {
			return y;
		}
		x = y;
		y = x->parent;
	}
	return NIL;
}

Node *find(Node *u, int k) {
	while (u != NIL) {
		if (u->key == k) {
			return u;
		}
		else if (k < u->key) {
			u = u->left;
		}
		else {
			u = u->right;
		}
	}
	return u;
}

void insert(int k) {
	Node *y = NIL;
	Node *x = root;
	Node *z;

	z = (Node *)malloc(sizeof(Node));
	z->key = k;
	z->left = NIL;
	z->right = NIL;

	while (x != NIL) {
		y = x;
		if (z->key < x->key) {
			x = x->left;
		}
		else {
			x = x->right;
		}
	}

	z->parent = y;
	if (y == NIL) {
		root = z;
	}
	else {
		if (z->key < y->key) {
			y->left = z;
		}
		else {
			y->right = z;
		}
	}
}

void inorder(Node *u) {
	if (u == NIL) {
		return;
	}
	inorder(u->left);
	printf(" %d", u->key);
	inorder(u->right);
}

void preorder(Node *u) {
	if (u == NIL) {
		return;
	}
	printf(" %d", u->key);
	preorder(u->left);
	preorder(u->right);
}

int main() {
	int n, i, x;
	string com;
	Node *z;

	NIL = NULL;

	scanf("%d", &n);

	for (i = 0; i < n; i++) {
		cin >> com;
		if (com == "insert") {
			scanf("%d", &x);
			insert(x);
		}
		else if (com == "print") {
			inorder(root);
			printf("\n");
			preorder(root);
			printf("\n");
		}
		else if (com == "find") {
			scanf("%d", &x);
			if (find(root, x) != NIL) {
				printf("yes\n");
			}
			else {
				printf("no\n");
			}
		}
		else if (com == "delete") {
			scanf("%d", &x);
			z = find(root, x);
			if (z != NIL) {
				treeDelete(z);
			}
		}
	}

	return 0;
}

Measured memory_bytes: 20012