Problem:
Binary Search Tree III

Write a program which performs the following operations to a binary search tree $T$ by adding delete operation to B: Binary Search Tree II.

insert $k$: Insert a node containing $k$ as key into $T$.

find $k$: Report whether $T$ has a node containing $k$.

delete $k$: Delete a node containing $k$.

print: Print the keys of the binary search tree by inorder tree walk and preorder tree walk respectively.

The operation delete $k$ for deleting a given node $z$ containing key $k$ from $T$ can be implemented by an algorithm which considers the following cases:

If $z$ has no children, we modify its parent $z.p$ to replace $z$ with NIL as its child (delete $z$).

If $z$ has only a single child, we "splice out" $z$ by making a new link between its child and its parent.

If $z$ has two children, we splice out $z$'s successor $y$ and replace $z$'s key with $y$'s key.

Input

In the first line, the number of operations $m$ is given. In the following $m$ lines, operations represented by insert $k$, find $k$, delete $k$ or print are given.

Output

For each find $k$ operation, print "yes" if $T$ has a node containing $k$, "no" if not.

In addition, for each print operation, print a list of keys obtained by inorder tree walk and preorder tree walk in a line respectively. Put a space character before each key

Constraints

The number of operations $\leq 500,000$

The number of print operations $\leq 10$.

$-2,000,000,000 \leq key \leq 2,000,000,000$

The height of the binary tree does not exceed 100 if you employ the above pseudo code.

The keys in the binary search tree are all different.

Sample Input 1

18
insert 8
insert 2
insert 3
insert 7
insert 22
insert 1
find 1
find 2
find 3
find 4
find 5
find 6
find 7
find 8
print
delete 3
delete 7
print

Sample Output 1

yes
yes
yes
no
no
no
yes
yes
1 2 3 7 8 22
8 2 1 3 7 22
1 2 8 22
8 2 1 22

Reference

Introduction to Algorithms, Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. The MIT Press.

Solution in Ruby (judge verdict: Accepted)

class MyNode
  attr_accessor :v, :parent, :left_child, :right_child

  def initialize v, parent, left_child, right_child
    @v = v
    @parent = parent
    @left_child = left_child
    @right_child = right_child
  end
end

def insert(root_node, v)
  p = nil
  x = root_node
  while x
    p = x
    if v < x.v
      x = x.left_child
    else
      x = x.right_child
    end
  end
  node = MyNode.new(v, p, nil, nil)
  if p
    if node.v < p.v
      p.left_child = node
    else
      p.right_child = node
    end
  else
    root_node = node
  end

  return root_node
end

def find(root_node, v)
  return true if root_node.v == v
  if v < root_node.v && root_node.left_child
    return find(root_node.left_child, v)
  end
  if v > root_node.v && root_node.right_child
    return find(root_node.right_child, v)
  end
  return false
end

def get_minimum(root_node)
  root_node = get_minimum(root_node.left_child) if root_node.left_child
  root_node
end

def delete(root_node, v)
  if root_node.v == v
    if root_node.left_child.nil? && root_node.right_child.nil?
      if root_node.v < root_node.parent.v
        root_node.parent.left_child = nil
      else
        root_node.parent.right_child = nil
      end
    elsif root_node.left_child && root_node.right_child
      successor = get_minimum(root_node.right_child)
      root_node.v = successor.v
      root_node.v = successor.v
      delete(successor, successor.v)
    elsif root_node.left_child
      root_node.left_child.parent = root_node.parent
      if root_node.v < root_node.parent.v
        root_node.parent.left_child = root_node.left_child
      else
        root_node.parent.right_child = root_node.left_child
      end
    elsif root_node.right_child
      root_node.right_child.parent = root_node.parent
      if root_node.v < root_node.parent.v
        root_node.parent.left_child = root_node.right_child
      else
        root_node.parent.right_child = root_node.right_child
      end
    end
    return
  end
  if v < root_node.v && root_node.left_child
    delete(root_node.left_child, v)
  end
  if v > root_node.v && root_node.right_child
    delete(root_node.right_child, v)
  end
end

def make_pre node, vs
  vs << node.v
  make_pre node.left_child, vs if node.left_child
  make_pre node.right_child, vs if node.right_child
  vs
end

def make_in node, vs
  make_in node.left_child, vs if node.left_child
  vs << node.v
  make_in node.right_child, vs if node.right_child
  vs
end

_ = gets
root = nil
while l = gets
  command, val = l.chomp.split(' ')
  if command == 'insert'
    root = insert(root, val.to_i)
  elsif command == 'print'
    puts ' ' + make_in(root, []).join(' ')
    puts ' ' + make_pre(root, []).join(' ')
  elsif command == 'find'
    puts find(root, val.to_i) ? 'yes' : 'no'
  elsif command == 'delete'
#     puts ' ' + make_in(root, []).join(' ')
    delete(root, val.to_i)
  end
end