#include <map>
#include <set>
#include <list>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <numeric>
#include <utility>
#include <iostream>
#include <algorithm>
#include <functional>
#include <unordered_map>
#include <unordered_set>

using namespace std;

#define ll long long
#define ull unsigned long long
#define INF (1 << 30)
#define LLINF (1LL << 62)
#define MOD7 1000000007
#define MOD9 1000000009
#define eps 1e-9
#define P pair<int, int>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pul pair<ull, ull>
#define all(a) (a).begin(), (a).end()
#define FOR(i,a,b) for (int i=(a);i<(b);++i)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);--i)
#define REP(i,n) for (int i=0;i<(n);++i)
#define RREP(i,n) for (int i=(n)-1;i>=0;--i)

void YES() { cout << "YES" << endl; }
void NO() { cout << "NO" << endl; }
void Yes() { cout << "Yes" << endl; }
void No() { cout << "No" << endl; }
void yes() { cout << "yes" << endl; }
void no() { cout << "no" << endl; }

template<class T>
class BinarySearchTree {
public:
	class Node {
	public:
		Node *parent;
		Node *left;
		Node *right;
		T key;

		Node() : parent(nullptr), left(nullptr), right(nullptr) {}
	};

	Node* root;

	BinarySearchTree() : root(nullptr) {}

	void insert(T key) {
		Node *parent = root;
		Node *node = new Node;

		node->key = key;
		while (parent != nullptr) {
			node->parent = parent;
			if (node->key < parent->key)
				parent = parent->left;
			else
				parent = parent->right;
		}

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

	Node* find(T key) {
		Node *node = root;
		while (node != nullptr) {
			if (node->key == key)
				return node;
			else if (key < node->key)
				node = node->left;
			else
				node = node->right;
		}
		return nullptr;
	}

	void deleteKey(T key) {
		Node *node = find(key);
		if (node == nullptr)
			return;

		Node *target = node;
		if (target->left != nullptr && target->right != nullptr) {
			target = target->right;
			while (target->left != nullptr)
				target = target->left;
			node->key = target->key;
		}

		Node *child;
		if (target->left != nullptr)
			child = target->left;
		else
			child = target->right;

		if (child != nullptr)
			child->parent = target->parent;

		if (target->parent == nullptr) {
			root = child;
		}
		else {
			if (target == target->parent->left)
				target->parent->left = child;
			else
				target->parent->right = child;
		}

		delete target;
	}

	vector<T> getPreOrder() {
		vector<T> dst;
		calcPreOrder(dst, root);
		return dst;
	}

	void calcPreOrder(vector<T>& order, Node *node) {
		if (node == nullptr)
			return;
		order.push_back(node->key);
		calcPreOrder(order, node->left);
		calcPreOrder(order, node->right);
	}

	vector<T> getInOrder() {
		vector<T> dst;
		calcInOrder(dst, root);
		return dst;
	}

	void calcInOrder(vector<T>& order, Node *node) {
		if (node == nullptr)
			return;
		calcInOrder(order, node->left);
		order.push_back(node->key);
		calcInOrder(order, node->right);
	}

	vector<T> getPostOrder() {
		vector<T> dst;
		calcPostOrder(dst, root);
		return dst;
	}

	void calcPostOrder(vector<T>& order, Node *node) {
		if (node == nullptr)
			return;
		calcPostOrder(order, node->left);
		calcPostOrder(order, node->right);
		order.push_back(node->key);
	}
};

template<class T>
void printVector(vector<T> v, string between, int mode) {
	if (mode == 0) {
		for (int i = 0, k = v.size(); i < k; ++i) {
			cout << v[i];
			if (i < k - 1)
				cout << between;
		}
		cout << endl;
	}
	else if (mode == 1) {
		for (int i = 0, k = v.size(); i < k; ++i) {
			cout << between << v[i];
		}
		cout << endl;
	}
}

int main() {
	BinarySearchTree<int> tree;
	int n, k;
	string s;

	cin >> n;
	while (n--) {
		cin >> s;
		if (s == "insert") {
			cin >> k;
			tree.insert(k);
		}
		else if (s == "print") {
			printVector(tree.getInOrder(), " ", 1);
			printVector(tree.getPreOrder(), " ", 1);
		}
		else if (s == "find") {
			cin >> k;
			if (tree.find(k) != nullptr)
				yes();
			else
				no();
		}
		else if (s == "delete") {
			cin >> k;
			tree.deleteKey(k);
		}
	}

	return 0;
}

Measured memory_bytes: 25240