import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
	
	public static final String INSERT = "insert";
	public static final String PRINT = "print";
	public static final String FIND = "find";
	public static final String  DELETE = "delete";
	public static Node root; 
	
	enum LR{
		LEFT,RIGHT;
	}
	
	enum FindMode{
		PRINT,NOPRINT;
	}

	public static void main(String[] args) throws Exception {
		//??\??????????????????
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        //????????°?????????
        int commandNum =Integer.parseInt(br.readLine());
        //root(????????????)
        root = null;
        //????????°?????????????????????
        for(int i = 0 ; i < commandNum; i++){
        	//???????????????
        	execute(br);
        }
	}
	
	/**
	 * ???????????????????????????
	 * 
	 * insert print?????????
	 * @param br
	 * @throws Exception
	 */
	private static void execute(BufferedReader br) throws Exception{
		String tmp = br.readLine();
		
		//???????????????????????????????????????
		if(tmp.startsWith(INSERT)){ //insert
			insert(Integer.parseInt(tmp.substring(INSERT.length() + 1)));
		}else if(tmp.startsWith(PRINT)){ //print
			print();
		}else if(tmp.startsWith(FIND)){
			find(Integer.parseInt(tmp.substring(FIND.length() + 1)),FindMode.PRINT);
		}else if(tmp.startsWith(DELETE)){
			delete(Integer.parseInt(tmp.substring(DELETE.length() + 1)));
		}
	}
	
	private static void delete(int key){
		Node deletedNode = find(key,FindMode.NOPRINT);
		if(deletedNode != null){
			//?????????????????°??§????????????????????????
			if(deletedNode.getLeft() == null //??????
					&& deletedNode.getRight() == null){
				deleteNoChildNode(deletedNode);
			}else if(deletedNode.getLeft() != null //???????????????
					&& deletedNode.getRight() != null){
				deleteTwoChildNode(deletedNode);
			}else{ //?????????????????????
				deleteOneChildNode(deletedNode);
			}

		}

	}
	
	private static void deleteNoChildNode(Node deletedNode){
		Node parentNode = deletedNode.getParent();
		
		//??????????±????root?????´???
		if(parentNode == null){
			root = null;
			return;
		}
		
		//????????????????????????????????????
		//??????????????????????????????????????§?????????????????§????¶????????????§??????
		if(parentNode.getLeft() == deletedNode){ //???
			parentNode.setLeft(null);
		}else{ //???
			parentNode.setRight(null);
		}
		
	}
	
	private static void deleteOneChildNode(Node deletedNode){
		//?????????????????????????????????????????????????????????
		Node childNode = null;
		if(deletedNode.getLeft() != null){
			childNode = deletedNode.getLeft();
		}else{
			childNode = deletedNode.getRight();
		}
		
		//??????????±????root?????´????????????root?????????
		if(deletedNode.getParent() == null){
			root = childNode;
			childNode.setParent(null);
			return;
		}
		//????????????????????????????????????
		Node parentNode = deletedNode.getParent();
		if(parentNode.getLeft() == deletedNode){ 
			//???
			//?????????????±??????????????????????????±???????????¨??????????
			parentNode.setLeft(childNode);
			//?????????????±??????????????????????????±???????????¨???????
			childNode.setParent(parentNode);
		}else{ 
			//???
			//?????????????±??????????????????????????±???????????¨??????????
			parentNode.setRight(childNode);
			//?????????????±??????????????????????????±???????????¨???????
			childNode.setParent(parentNode);
		}
	}
	
	private static void deleteTwoChildNode(Node deletedNode){
		//1.??????????±?????????????????????????????¬???\????????????(??????????±?????????§??????key??§????°????)
		Node nextInorderNode = findNextInorderNode(deletedNode.getRight());
		int nextInorderNodeKey = nextInorderNode.getKey();
		//2.1??§????????????Node???????????????
		delete(nextInorderNodeKey);
		//3.??????????±????key???1??§????????????Node???key??§???????????????
		deletedNode.setKey(nextInorderNodeKey);
	}
	
	
	private static Node findNextInorderNode(Node node){
		if(node.getLeft() == null){
			return node;
		}
		return findNextInorderNode(node.getLeft());
	}
	
	private static Node find(int key,FindMode mode){
		//????????¨????????¨
		Node comparisonNode = root;
		
		while(comparisonNode != null){
			//??¨?????¢??????
			if(key == comparisonNode.getKey()){
				if(mode == FindMode.PRINT){
					System.out.print("yes");
					System.out.print("\n");
				}
				return comparisonNode;
			}else{
				if(key < comparisonNode.getKey()){
					comparisonNode = comparisonNode.getLeft();
				}else if(key > comparisonNode.getKey()){
					comparisonNode = comparisonNode.getRight();
				}
			}
		}
		//??¨?????¢??????
		if(mode == FindMode.PRINT){
			System.out.print("no");
			System.out.print("\n");
		}
		return null;
	}
	
	private static void insert(int key){
		Node node = makeNode(key);
		//root????????¨?¢???? ?????¨???????????´??????insert??????????????????root??¨??????
		if(root == null){
			root = node;
		}else{
			//????????¨????????¨
			Node comparisonNode = root;
			//???????????????????????°????¨???????????????????????????????innsert???????????????????????¨??????
			Node parentNode = null;
			//????????????????????????innsert???????????????????¨?????????????????????????????????°
			LR LRFlag = null;
			
			//?????\??´????????¢?´¢??????
			while(comparisonNode != null){
				//?????¨?????????????±???????????????????
				parentNode = comparisonNode;
				if(node.getKey() < comparisonNode.getKey()){
					//??????????±????node???????°?????????´???????¬??????????????±???????????????¨??????
					comparisonNode = comparisonNode.getLeft();
					LRFlag = LR.LEFT;
				}else if(node.getKey() > comparisonNode.getKey()){
					//??????????±????node????????§????????´???????¬??????????????±???????????????¨??????
					comparisonNode = comparisonNode.getRight();
					LRFlag = LR.RIGHT;
				}
			}
			
			//innsert?????????????????????????¨????
			node.setParent(parentNode);
			
			 //innsert?????????????????????????????????????????????????¨????????????????
			 //????????°?????¨????????????
			if(LRFlag == LR.LEFT){
				parentNode.setLeft(node);
			}else if(LRFlag == LR.RIGHT){
				parentNode.setRight(node);
			}
		}
	}
	
	private static Node makeNode(int key){
		return new Node(key);
	}
	
	private static void print(){
		StringBuffer sb = new StringBuffer();
		
		//???????????????
		inorderPrint(root,sb);
		sb.append("\n");
		System.out.print(sb.toString());
		
		//sb???????????¢
		sb.delete(0,sb.length());
		
		//???????????????
		preorderPrint(root,sb);
		sb.append("\n");
		System.out.print(sb.toString());
	}
	
	/**
	 * ???????????????
	 */
	private static void inorderPrint(Node node,StringBuffer sb){
		if(node == null){
			return;
		}
		inorderPrint(node.getLeft(),sb);
		sb.append(" ");
		sb.append(node.getKey());
		inorderPrint(node.getRight(),sb);
	}
	
	/**
	 * ???????????????
	 */
	private static void preorderPrint(Node node,StringBuffer sb){
		if(node == null){
			return;
		}
		sb.append(" ");
		sb.append(node.getKey());
		preorderPrint(node.getLeft(), sb);
		preorderPrint(node.getRight(), sb);
	}

}

class Node{
	
	private int key;
	private Node left;
	private Node right;
	private Node parent;
	
	public Node(int key){
		this.key = key;
	}
	
	public int getKey() {
		return key;
	}
	public void setKey(int key) {
		this.key = key;
	}
	public Node getLeft() {
		return left;
	}
	public void setLeft(Node left) {
		this.left = left;
	}
	public Node getRight() {
		return right;
	}
	public void setRight(Node right) {
		this.right = right;
	}
	public Node getParent() {
		return parent;
	}
	public void setParent(Node parent) {
		this.parent = parent;
	}
}

Measured memory_bytes: 129388