Problem:
Binary Search Tree III

Write a program which performs the following operations to a binary search tree $T$ by adding delete operation to B: Binary Search Tree II.

insert $k$: Insert a node containing $k$ as key into $T$.

find $k$: Report whether $T$ has a node containing $k$.

delete $k$: Delete a node containing $k$.

print: Print the keys of the binary search tree by inorder tree walk and preorder tree walk respectively.

The operation delete $k$ for deleting a given node $z$ containing key $k$ from $T$ can be implemented by an algorithm which considers the following cases:

If $z$ has no children, we modify its parent $z.p$ to replace $z$ with NIL as its child (delete $z$).

If $z$ has only a single child, we "splice out" $z$ by making a new link between its child and its parent.

If $z$ has two children, we splice out $z$'s successor $y$ and replace $z$'s key with $y$'s key.

Input

In the first line, the number of operations $m$ is given. In the following $m$ lines, operations represented by insert $k$, find $k$, delete $k$ or print are given.

Output

For each find $k$ operation, print "yes" if $T$ has a node containing $k$, "no" if not.

In addition, for each print operation, print a list of keys obtained by inorder tree walk and preorder tree walk in a line respectively. Put a space character before each key

Constraints

The number of operations $\leq 500,000$

The number of print operations $\leq 10$.

$-2,000,000,000 \leq key \leq 2,000,000,000$

The height of the binary tree does not exceed 100 if you employ the above pseudo code.

The keys in the binary search tree are all different.

Sample Input 1

18
insert 8
insert 2
insert 3
insert 7
insert 22
insert 1
find 1
find 2
find 3
find 4
find 5
find 6
find 7
find 8
print
delete 3
delete 7
print

Sample Output 1

yes
yes
yes
no
no
no
yes
yes
1 2 3 7 8 22
8 2 1 3 7 22
1 2 8 22
8 2 1 22

Reference

Introduction to Algorithms, Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. The MIT Press.

Solution in C++14 (judge verdict: Accepted)

#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;
}