class Tree
  class Node
    attr_accessor :value, :parent, :left, :right

    def initialize(value, parent=nil)
      @value = value
      @parent = parent
      @left = nil
      @right = nil
    end

    def ==(other)
      @value == other.value
    end

    def insert(value)
      if value < @value
        if @left
          @left.insert(value)
        else
          @left = Node.new(value, self)
        end
      else
        if @right
          @right.insert(value)
        else
          @right = Node.new(value, self)
        end
      end
    end

    def find(value)
      return self if value == @value

      if value < @value
        return @left && @left.find(value)
      else
        return @right && @right.find(value)
      end
    end

    def visit_inorder(&block)
      @left.visit_inorder(&block) if @left
      block.call(self) if block
      @right.visit_inorder(&block) if @right
    end

    def visit_preorder(&block)
      block.call(self) if block
      @left.visit_preorder(&block) if @left
      @right.visit_preorder(&block) if @right
    end
  end

  def initialize
    @root = nil
  end

  def insert(value)
    if @root
      @root.insert(value)
    else
      @root = Node.new(value)
    end
  end

  def include?(value)
    return false unless @root

    !!@root.find(value)
  end

  def delete(value)
    return false unless @root

    target = @root.find(value)
    return false unless target

    if target.left && target.right
      min_node = target.right
      while min_node.left
        min_node = min_node.left
      end

      parent = min_node.parent

      if parent == target
        right = min_node.right
        parent.right = right
        right.parent = parent if right
        min_node.parent = min_node.right = nil
      else
        right = min_node.right
        parent.left = right
        right.parent = parent if right
        min_node.parent = min_node.right = nil
      end

      target.value = min_node.value
    else
      parent = target.parent
      child = target.left || target.right

      if parent
        child.parent = parent if child

        if parent.left == target
          parent.left = child
        else
          parent.right = child
        end
      else
        @root = child
      end

      target.parent = target.left = target.right = nil
    end

    true
  end

  def visit_inorder(&block)
    @root.visit_inorder(&block) if @root
  end

  def visit_preorder(&block)
    @root.visit_preorder(&block) if @root
  end
end

if $PROGRAM_NAME == __FILE__
  n = gets.to_i

  tree = Tree.new

  n.times do
    command, value = gets.chomp.split

    value = value.to_i if value

    case command
    when 'insert'
      tree.insert(value)
    when 'find'
      puts tree.include?(value) ? 'yes' : 'no'
    when 'delete'
      tree.delete(value)
    when 'print'
      tree.visit_inorder do |node|
        print " #{node.value}"
      end
      puts

      tree.visit_preorder do |node|
        print " #{node.value}"
      end
      puts
    else
      raise "unknown command: #{command}"
    end
  end
end