using System;
using System.Text;
using System.Collections.Generic;
 
public class node
{
    public node parent;
    public node rightChild;
    public node leftChild;
    public int key;
}
 
public class Test
{
    public static int[] preorderKey, inorderKey;
    public static int precount, incount;
     
    public static void Main()
    {
        // List<node> tre = new List<node>();
		// Insert(30, tre);	
		// Insert(17, tre);	
		// Insert(88, tre);	
		// Insert(53, tre);	
		// Insert(5, tre);	
		// Insert(20, tre);	
		// Insert(18, tre);	
		// Insert(28, tre);	
		// Insert(27, tre);	
		// Insert(60, tre);
		// Insert(2000000000, tre);	
		// Insert(55, tre);	
		// Insert(63, tre);	
		// Insert(-1, tre);	
		// Insert(8, tre);		
		// Delete(Find(53, tre), tre);
		// Delete(Find(2000000000, tre), tre);
		// Delete(Find(20, tre), tre);
		// Delete(Find(5, tre), tre);
		// Delete(Find(8, tre), tre);
		// Print(tre.ToArray());

		int n = int.Parse(Console.ReadLine());
        List<node> tree = new List<node>();
         
        for(int i = 0; i < n ; i++) {
            string[] atom = Console.ReadLine().Split(" ".ToCharArray());
             
            switch(atom[0][0]){
                case 'i':
                    Insert(int.Parse(atom[1]), tree);
                    break;
				
				case 'f':
					Console.WriteLine( Find( int.Parse(atom[1]), tree[0])? "yes" : "no" );
					break;
				
				case 'd':
					Delete(Find( int.Parse(atom[1]), tree), tree);
					break;
                 
                case 'p':
                    Print(tree.ToArray());
                    break;
            }
        }
    }
     
    public static void Insert(int v ,List<node> T)
    {
        node y = null;
        
        node z = new node();
		z.key = v;
		T.Add(z);
        
        if(T.Count == 1) return;
        node x = T[0];
 
        while(x != null) {
            y = x;
            if(v < x.key) x = x.leftChild;
            else x = x.rightChild;
        }
        z.parent = y;
         
        if(y == null) {
            
        } else if (v < y.key) {
            y.leftChild = T[T.Count - 1];
        } else {
            y.rightChild = T[T.Count - 1];
        }
    }
	
	public static bool Find(int v, node x)
	{
		while(x != null) {
			if(x.key == v) return true;
			else if(v < x.key) x = x.leftChild;
			else x = x.rightChild;
		}
		
		return false;
	}
	
	public static node Find(int v, List<node> T)
	{
		node x = T[0];
		
		while(x != null) {
			if(x.key == v) return x;
			else if(v < x.key) x = x.leftChild;
			else x = x.rightChild;
		}
		
		return null;
	}
	
	public static void Delete(node z, List<node> T)
	{	
		if(z.leftChild == null && z.rightChild == null) {
			if(z.key < z.parent.key) z.parent.leftChild = null;
			else if(z.key > z.parent.key) z.parent.rightChild = null;
			else {
				if(z == z.parent.leftChild) z.parent.leftChild = null;
				else z.parent.rightChild = null;
			}
			T.Remove(z);
		} else if (z.leftChild == null) {
			if( z == z.parent.rightChild ) z.parent.rightChild = z.rightChild;
			else z.parent.leftChild = z.rightChild;
			z.rightChild.parent = z.parent;
			T.Remove(z);
		} else if (z.rightChild == null) {
			if( z == z.parent.leftChild ) z.parent.leftChild = z.leftChild;
			else z.parent.rightChild = z.leftChild;
			z.leftChild.parent = z.parent;
			T.Remove(z);
		} else {
			node y = GetSuccesser(z);
			z.key = y.key;
			Delete(y, T);
		}
	}

	public static node GetSuccesser(node x)
	{
		node y;
		
		if(x.rightChild != null) {
			y = x.rightChild;

			while(y.leftChild != null) {
				y = y.leftChild;
			}
		} else {
			y = x.parent;

			while(y != null && x == y.rightChild) {
				x = y;
				y = y.parent;
			}
		}

		return y;
	}

    public static void Print(node[] T)
    {
        preorderKey = new int[T.Length];
        inorderKey = new int[T.Length];
        precount = 0;
        incount = 0;
         
        node root = T[0];
        Inorder(root, T);
        Preorder(root, T);
        
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < T.Length; i++) {
            sb.Append(" " + inorderKey[i]);
        }
        Console.WriteLine(sb.ToString());
        sb = new StringBuilder();
        for(int i = 0; i < T.Length; i++) {
            sb.Append(" " + preorderKey[i]);
        }
        Console.WriteLine(sb.ToString());
         
    }
     
    public static void Preorder(node t, node[] tree)
    {
        preorderKey[precount++] = t.key;
        if(t.leftChild != null) Preorder(t.leftChild, tree);
        if(t.rightChild != null) Preorder(t.rightChild, tree);
    }
     
    public static void Inorder(node t, node[] tree)
    {
        if(t.leftChild != null) Inorder(t.leftChild, tree);
        inorderKey[incount++] = t.key;
        if(t.rightChild != null) Inorder(t.rightChild, tree);
    }
     
    public static node GetRoot(node[] T)
    {
        if ( T ==  null ) return null;
        if ( T.Length == 0) return null;
         
        node u = T[0];
        while(u.parent != null) {
            u = u.parent;
        }
         
        return u;
    }
}

Measured memory_bytes: 69616