CMD=0
VAL=1
 
class Node
  attr_accessor :value,:left,:right
  def initialize (value)
    @value = value
    @left = nil
    @right = nil
  end
 
  def insert (value)
    if value < @value
      if @left.nil?
        @left = Node.new(value)
      else
        @left.insert(value)
      end
    else
      if @right.nil?
        @right = Node.new(value)
      else
        @right.insert(value)
      end
    end
  end
 
  def find(value)
    if value==@value
      print "yes\n"
    elsif value < @value
      if @left.nil?
        print "no\n"
      else
        @left.find(value)
      end
    else
      if @right.nil?
        print "no\n"
      else
        @right.find(value)
      end
    end
  end
 
 # delete node and return new node 
  def delete(value)
    if value == @value
      if @left.nil? and @right.nil?
        # no child
        return nil
      elsif @left.nil?
        # right child only
        return @right
      elsif @right.nil?
        # left child only
        return @left
      else
        # more than 2 child
        # swap value
        @value = @right.get_new_value
        # delete swapped node
        @right = @right.delete(@value)
        return self
      end
    elsif value < @value
      @left = @left.delete(value)
      return self
    else
      @right = @right.delete(value)
      return self
    end
  end
 
 # find minimum key of subtree
  def get_new_value
    unless @left.nil?
      return @left.get_new_value
    else
      return @value
    end
  end
       
  def print_inorder
    @left.print_inorder unless @left.nil?
    print " #{value}"
    @right.print_inorder unless @right.nil?
  end
   
  def print_preorder
    print " #{value}"
    @left.print_preorder unless @left.nil?
    @right.print_preorder unless @right.nil?
  end
end
 
num_of_cmd = STDIN.gets()
inputs = STDIN.read().split("\n").map{ |input| input.split(' ') }
 
root=nil
inputs.each do |input|
  if input[CMD] == 'insert'
    if root.nil?
      root = Node.new(input[VAL].to_i)
    else
      root.insert(input[VAL].to_i)
    end
  elsif input[CMD] == 'find'
    root.find(input[VAL].to_i)
  elsif input[CMD] == 'delete'
    root.delete(input[VAL].to_i)
  else
    root.print_inorder unless root.nil?
    print "\n"
    root.print_preorder unless root.nil?
    print "\n"
  end
end