#include <iostream>
#include <string>
#include <vector>
using namespace std;
using ll = long long;
#define REP(i,N) for(ll i=0;i<N;++i)

struct Node {
	Node(){};
	~Node() {
		delete left;
		delete right;
	};	
	long long key; 
	Node *parent;
	Node *left;
	Node *right;
};

// root node is not nullptr
void insert(Node *root, Node *z) {
	Node *y = nullptr;
	Node *x = root;
	while (x != nullptr) {
		y = x;
		if (z->key < x->key) {
			x = x->left;
		} else {
			x = x->right;
		}
	}
	z->parent = y;
	if (z->key < y->key) {
		y->left = z;
	} else {
		y->right = z;
	}
}

bool find(Node *root, ll key) {
	if (root == nullptr) return false;
	if (root->key == key) return true;
	Node *ptr = key < root->key ? root->left : root->right;
	return find(ptr, key);
}

Node* find_using_loop(Node *root, ll key) {
	while (root != nullptr && key != root->key) {
		if (key < root->key) root = root->left;
		else root = root->right;
	}
	return root;
}

Node* get_minimum(Node *x) {
	while (x->left != nullptr) {
		x = x->left;
	}
	return x;
}

/// returns the next node to z
Node* get_successor(Node *z) {
	if (z->right != nullptr) {
		return get_minimum(z->right);
	}
	Node *y = z->parent;
	while (y != nullptr && z == y->right) {
		z = y;
		y = y->parent;
	}
	return y;
}

/// z: target to delete
///
void delete_node(Node **root, Node *z) {
	if (z == nullptr) return;
	Node *x;
	/// find the real target to delete
	Node *y;
	if (z->left == nullptr || z->right == nullptr) {
		y = z;
	} else {
		y = get_successor(z);
	}
	/// determine the child of y
	if (y->left != nullptr) {
		x = y->left;
	} else {
		x = y->right;
	}

	if (x != nullptr) {
		x->parent = y->parent;
	}
	/// yがroot nodeだった場合
	if (y->parent == nullptr) {
		*root = x;
	} else if (y == y->parent->left) {
		y->parent->left = x;
	} else {
		y->parent->right = x;
	}	

	if (y != z) {
		z->key = y->key;
	}
	y->left = y->right = nullptr;
	delete y;
}

void print_by_preorder(Node *node) {
	if (node == nullptr) return;
	cout << " " << node-> key; 
	print_by_preorder(node->left);
	print_by_preorder(node->right);
}

void print_by_inorder(Node *node) {
	if (node == nullptr) return;
	print_by_inorder(node->left);
	cout << " " << node-> key; 
	print_by_inorder(node->right);
}

int main () {
	ll m;
	cin >> m;
	Node *root = new Node();
	REP(i,m) {
		string s;
		cin >> s;
		ll key;
		if (s[0] == 'i') {
			cin >> key;
			// initialize root node
			if (i==0) {
				// root node
				root->left = root->right = nullptr;
				root->key = key;
				continue;
			}
			Node *node = new Node();
			node->key = key;
			node->left = nullptr; // caution
			node->right= nullptr; // caution
			insert(root, node);
		} else if (s[0] == 'p') {
			print_by_inorder(root);
			cout << endl;
			print_by_preorder(root);
			cout << endl;
		} else if (s[0] == 'f') {
			cin >> key;
			//1.
			//cout << (find(root, key) ? "Yes" : "No") << endl;
			//2.
		  Node *t = find_using_loop(root, key);	
			if (t == nullptr) {
				cout << "no" << endl;
			} else {
				cout << "yes" << endl;
			}
		} else if (s[0] == 'd') {
			cin >> key;
			Node *t = find_using_loop(root, key);
			Node** pp_root = &root;
			delete_node(pp_root, t);	
		}
	}
	delete root; 
	return 0;
}




Measured memory_bytes: 21768