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
  class Node
    attr_accessor :key, :left, :right, :parent

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

    def add_left(node)
      @left = node
      node.parent = self
    end

    def add_right(node)
      @right = node
      node.parent = self
    end

    def remove
      if (@parent.left != nil && @parent.left.key == @key) then
        @parent.left = nil
      elsif (@parent.right != nil && @parent.right.key == @key) then
        @parent.right = nil
      end
    end
  end

  attr_accessor :count

  def initialize
    @count = 0
    @root = nil
  end

  def insert(z)
    y = nil
    x = @root
    while x != nil do
      y = x
      if z.key < x.key then
        x = x.left
      else
        x = x.right
      end
    end
    
    if y == nil then
      @root = z
    elsif z.key < y.key
      y.add_left(z)
    else
      y.add_right(z)
    end
  end

  def find(z)
    x = @root
    while x != nil do
      if z == x.key then
        return x 
      elsif z < x.key then
        x = x.left
      elsif z > x.key then
        x = x.right
      end
    end
    return nil
  end

  def delete(z)
    children = [z.left, z.right].compact
    case children.size
    when 0 then
      z.remove
    when 1 then
      children[0].parent = z.parent
      if (z.parent.left != nil && z.parent.left.key == z.key) then
        z.parent.left = children[0]
      elsif (z.parent.right != nil && z.parent.right.key == z.key) then
        z.parent.right = children[0]
      end
      z.remove
    else
      next_node = get_next_inorder(z)
      z.key = next_node.key
      delete(next_node)
    end
  end

  def get_next_inorder(node)
    array_inorder = inorder
    next_node_key =  array_inorder[array_inorder.index(node.key)+1]
    return find(next_node_key)
  end

  def inorder
    node = @root
    array = []
    do_inorder(node, array) unless node == nil
    return array
  end

  def preorder
    node = @root
    array = []
    do_preorder(node, array) unless node == nil
    return array
  end

  def print
    print_inorder
    print_preorder
  end

  def print_inorder
    node = @root
    array = []
    do_inorder(node, array) unless node == nil
    puts " #{array.join(' ')}"
  end

  def print_preorder
    node = @root
    array = []
    do_preorder(node, array) unless node == nil
    puts " #{array.join(' ')}"
  end

  private

  def do_inorder(node, array)
    do_inorder(node.left, array) if node.left != nil
    array << node.key
    do_inorder(node.right, array) if node.right != nil
  end

  def do_preorder(node, array)
    array << node.key
    do_preorder(node.left, array) if node.left != nil
    do_preorder(node.right, array) if node.right != nil
  end
end


# main
tree = BinaryTree.new
n = gets.chomp.to_i
(n).times do
  command, key = gets.chomp.split
  if command == "insert" then
    tree.insert(BinaryTree::Node.new(key.to_i))
  elsif command == "find" then
    puts "#{tree.find(key.to_i) != nil ? 'yes' : 'no'}"
  elsif command == "delete" then
    tree.delete(tree.find(key.to_i))
  elsif command == "print" then
    tree.print
  end
end