class BinaryTree
  class Node
    attr_accessor :key, :left, :right, :parent

    def initialize(key)
      @key = key
      @left = nil
      @right = nil
      @parent = nil
    end

    def add_left(node)
      @left = node
      node.parent = self
    end

    def add_right(node)
      @right = node
      node.parent = self
    end

    def remove
      if (@parent.left != nil && @parent.left.key == @key) then
        @parent.left = nil
      elsif (@parent.right != nil && @parent.right.key == @key) then
        @parent.right = nil
      end
    end
  end

  attr_accessor :count

  def initialize
    @count = 0
    @root = nil
  end

  def insert(z)
    y = nil
    x = @root
    while x != nil do
      y = x
      if z.key < x.key then
        x = x.left
      else
        x = x.right
      end
    end
    
    if y == nil then
      @root = z
    elsif z.key < y.key
      y.add_left(z)
    else
      y.add_right(z)
    end
  end

  def find(z)
    x = @root
    while x != nil do
      if z == x.key then
        return x 
      elsif z < x.key then
        x = x.left
      elsif z > x.key then
        x = x.right
      end
    end
    return nil
  end

  def delete(z)
    children = [z.left, z.right].compact
    case children.size
    when 0 then
      z.remove
    when 1 then
      children[0].parent = z.parent
      if (z.parent.left != nil && z.parent.left.key == z.key) then
        z.parent.left = children[0]
      elsif (z.parent.right != nil && z.parent.right.key == z.key) then
        z.parent.right = children[0]
      end
      z.remove
    else
      next_node = get_next_inorder(z)
      z.key = next_node.key
      delete(next_node)
    end
  end

  def get_next_inorder(node)
    array_inorder = inorder
    next_node_key =  array_inorder[array_inorder.index(node.key)+1]
    return find(next_node_key)
  end

  def inorder
    node = @root
    array = []
    do_inorder(node, array) unless node == nil
    return array
  end

  def preorder
    node = @root
    array = []
    do_preorder(node, array) unless node == nil
    return array
  end

  def print
    print_inorder
    print_preorder
  end

  def print_inorder
    node = @root
    array = []
    do_inorder(node, array) unless node == nil
    puts " #{array.join(' ')}"
  end

  def print_preorder
    node = @root
    array = []
    do_preorder(node, array) unless node == nil
    puts " #{array.join(' ')}"
  end

  private

  def do_inorder(node, array)
    do_inorder(node.left, array) if node.left != nil
    array << node.key
    do_inorder(node.right, array) if node.right != nil
  end

  def do_preorder(node, array)
    array << node.key
    do_preorder(node.left, array) if node.left != nil
    do_preorder(node.right, array) if node.right != nil
  end
end


# main
tree = BinaryTree.new
n = gets.chomp.to_i
(n).times do
  command, key = gets.chomp.split
  if command == "insert" then
    tree.insert(BinaryTree::Node.new(key.to_i))
  elsif command == "find" then
    puts "#{tree.find(key.to_i) != nil ? 'yes' : 'no'}"
  elsif command == "delete" then
    tree.delete(tree.find(key.to_i))
  elsif command == "print" then
    tree.print
  end
end