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 BinaryTree
  attr_accessor :root

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

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

  def initialize
    @root = nil
  end

  def insert(key)
    n = Node.new(key)
    y = nil
    x = @root

    while x != nil
      y = x
      x = if n.key < x.key
            x.left
          else
            x.right
          end
    end
    n.parent = y

    if y == nil
      @root = n
    elsif n.key < y.key
      y.left = n
    else
      y.right = n
    end
  end

  def find(key)
    n = @root
    while n != nil
      return n if n.key == key
      n = if key < n.key
        n.left
      else
        n.right
      end
    end
    return nil
  end

  def del(key)
    n = find(key)
    if n.left == nil || n.right == nil
      del_node = n
    else
      del_node = get_successor(n)
    end

    if del_node.left != nil
      x = del_node.left
    else
      x = del_node.right
    end

    if x
      x.parent = del_node.parent
    end

    if del_node.parent == nil
      @root = x
    elsif del_node == del_node.parent.left
      del_node.parent.left = x
    else
      del_node.parent.right = x
    end

    if n != del_node
      n.key = del_node.key
    end
  end

  def get_successor(n)
    if n.right
      return get_minimum(n.right)
    end

    y = n.parent
    while y != nil && n == y.right
      n = y
      y = y.parent
    end
    return y
  end

  def get_minimum(n)
    while n.left
      n = n.left
    end
    return n
  end

  def inorder(n)
    return unless n
    inorder(n.left)
    print " #{n.key}"
    inorder(n.right)
  end

  def preorder(n)
    return unless n
    print " #{n.key}"
    preorder(n.left)
    preorder(n.right)
  end

  def dump
    inorder(@root)
    print "\n"
    preorder(@root)
    print "\n"
  end
end

n = gets.to_i
bt = BinaryTree.new

n.times do |i|
  cmd, v = gets.split
  case cmd
  when "insert"
    bt.insert(v.to_i)
  when "find"
    puts bt.find(v.to_i) ? "yes" : "no"
  when "delete"
    bt.del(v.to_i)
  when "print"
    bt.dump
  end
end