class Array
def insert k
x,l,r=self
x ?(k<x ?l:r).insert(k):self<<k<<[]<<[]
end
def find k
puts f(k) ?:yes: :no
end
def f k
x,l,r=self
x&&(k==x||(k<x ?l:r).f(k))
end

def delete k
x,l,r=self
x&&(
if k==x
  d
elsif k<x
  l.delete k
else
  r.delete k
end
)end

def d
  k,l,r=self
  if l[0]
    if r[0]
      self[0]=s r
    else
      a,b,c=*l
      clear
      self<<a<<b<<c
    end
  else
    if r[0]
      a,b,c=*r
      clear
      self<<a<<b<<c
    else
      clear
    end
  end
end

def s
  k,l=self
  if l[0]
    s l
  else
    clear
    k
  end
end

def print
$x,$y=[p],[p]
w
puts $y*" ",$x*" "
end
def w
x,l,r=self
x&&($x<<x
l.w
$y<<x
r.w)
end
end
t=[]
gets
eval"t.#$_"while gets