#include<iostream>
#include<string>
#include<vector>
#include<memory>

template<class T>
struct node {
	T key;
	node* parent;
	std::unique_ptr<node<T>> left, right;
	bool valid_;


	bool nil()const {
		return !valid_;
	}

	bool valid()const {
		return valid_;
	}

	void validate() {
		valid_ = true;
	}

	void invalidate() {
		this->valid_ = false;
	}

	node() :key{}
		, parent{ nullptr }
		, left{ nullptr }
		, right{ nullptr }
		, valid_{ false }
	{}

	template<class X>
	node(X&&t) :key{ t }
		, parent{ nullptr }
		, left{ nullptr }
		, right{ nullptr }
		, valid_{ false }
	{}

};



template<class T>
void inorder(const node<T>* const root) {
	if (root == nullptr)
		return;

	inorder(root->left.get());
	std::cout << " " << root->key;
	inorder(root->right.get());
}

template<class T>
void preorder(const node<T>* const root) {
	if (root == nullptr)
		return;

	std::cout << " " << root->key;
	preorder(root->left.get());
	preorder(root->right.get());
}


template<class T>
void print(const node<T>*const root) {
	inorder(root);
	std::cout << "\n";
	preorder(root);
	std::cout << "\n";
}


template<class T>
void insert(node<T>* root, const T&value_) {
	decltype(root) parent = nullptr;
	decltype(root) here = root;

	auto z = std::make_unique<node<T>>(value_);
	z->validate();

	//値が最も近い、有効なノード(parent)へ移動
	while (here != nullptr && here->valid()) {
		parent = here;
		//		std::cout << here->key << " ";

		if (z->key < here->key) {
			here = here->left.get();
			//			std::cout << "> ";
		}
		else {
			here = here->right.get();
			//			std::cout << "< ";
		}
		//		std::cout << value_ << "\n";
	}

	//zはparentの left か right になる
	z->parent = parent;

	auto& up
		= z->key < parent->key ?
		parent->left
		: parent->right;

	up = std::move(z);

}

template<class T>
auto find(node<T>* root, const T& value_)->node<T>* {
	if (root == nullptr)
		return nullptr;

	if (root->key == value_)
		return root;

	auto& child = value_ < root->key ? root->left : root->right;
	return find(child.get(), value_);

}



template<class T>
auto inorder_next( node<T>* const here)
	->node<T>* {
//	if (here == nullptr);
//		return here;

	node<T>* tmp = here->left ? here->left.get() : here;

	while (tmp != nullptr && tmp->left)
		tmp = tmp->left.get();

	return tmp;
}


template<class T>
auto& which(node<T>*np) {
	return  np->left  ? np->left : np->right;
}

template<class T>
auto& which(node<T>*np, const T&key) {
	return np->left->key == key
		? np->left : np->right;
}


template<class T>
decltype(auto) del_ch0(node<T>*node) {
	if (node == nullptr)
		return 
			static_cast< decltype(node) >(nullptr);

	auto&p = node->parent;

	if(p->left && p->right)
		which(p, node->key).reset();
	else
		which(p).reset();
	
	return 
		node->parent;
}

template<class T>
decltype(auto) del_ch1(node<T>*node){

	if (node == nullptr)
		return node;

	auto& child = which(node);//有効な方、nullptrではない方
	
	child->parent = node->parent;

	auto* cp = child->parent;

	(
		cp->left && cp->right ?
		which(cp, node->key)
		: which(cp)
		) = std::move(child) ;

	return cp;
}


template<class T>
decltype(auto) del_ch2(node<T>*node) {

	using r_type = decltype(node);

	if (node == nullptr)
		return r_type{nullptr};


	auto* suc = inorder_next(node->right.get());


	node->key = std::move(suc->key);
	
	
	auto ch = (suc->left ? 1 : 0) + (suc->right ? 1 : 0);

	if (ch == 0)
		return del_ch0(suc);
	else
		return del_ch1(suc);

}

template<class T>
auto del(node<T>*node) {
	auto lb = !!node->left;
	auto rb = !!node->right;

	auto chs = (lb ? 1 : 0) + (rb ? 1 : 0);

//	auto& p = node->parent;
//	if(node->parent != nullptr)

	if (chs == 0)
		return
			del_ch0(node);
	
	else if (chs == 1) 
		return
			del_ch1(node);
	
	else 
		return
			del_ch2(node);

}

int main() {
	int n;
	std::cin >> n;

	std::unique_ptr<node<int>> root{ nullptr };


	for (int i{}; i < n; ++i) {
		std::string s;
		std::cin >> s;
		if (s == "insert") {
			int k;
			std::cin >> k;
			if (!root) {
				root = std::make_unique<node<int>>(k);
				root->validate();
			}
			else
				insert(root.get(), k);
		}
		else if (s == "find") {
			int k;
			std::cin >> k;
			std::cout << (find(root.get(), k) ? "yes" : "no") << "\n";
		}
		else if (s == "delete") {
			int k;
			std::cin >> k;
			auto*p = find(root.get(), k);
			if (p == nullptr) {
				std::cout << ">>>>  error\n";
			}
			else
				del(p);
		}
		else
			print(root.get());
	}

	return 0;
}


Measured memory_bytes: 21776