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 Scala (judge verdict: Accepted)

import scala.collection.mutable.ListBuffer
import scala.io.StdIn

object Main extends App {

  val n = StdIn.readLine().trim.toInt
  var root: Node = _
  val buff = ListBuffer.empty[String]
  case class Node(var key: Int, var parent: Node, var left: Node, var right: Node){
    private def getDefaultKey(p: Node) = if(p == null) -1 else p.key
    override def toString: String = s"Node($key, ${getDefaultKey(parent)} ${getDefaultKey(left)} ${getDefaultKey(right)})"
  }
  
  (0 until n).foreach { _ =>
    val command = StdIn.readLine()
    command.split(' ') match {
      case Array("insert", key) => insert(Node(key.toInt, null, null, null))
      case Array("find"  , key) => 
        buff += (if(find(root, key.toInt) == null) f"no%n" else f"yes%n")
      case Array("delete"  , key) => 
        //println(key.toInt)
        val target = find(root, key.toInt)
        if(target != null) delete(target)
      case _ => 
        // print
        inorder(root)
        buff += f"%n"
        preorder(root)
        buff += f"%n"
    }
  }
  
  
  def insert(z: Node): Unit = {
    var y:Node = null
    var x:Node = root
    while(x != null){
      y = x
      if(z.key < x.key){
        x = x.left
      } else {
        x = x.right
      }
    }
    z.parent = y
    
    if(y == null){
      root = z
    } else if(z.key < y.key){
      y.left = z
    } else {
      y.right = z
    }
  }
  
  def preorder(n: Node): Unit = {
    buff += " " + n.key
    if(n.left != null) preorder(n.left)
    if(n.right != null) preorder(n.right)
  }

  def inorder(n: Node): Unit = {
    if(n.left != null) inorder(n.left)
    buff += " " + n.key
    if(n.right != null) inorder(n.right)
  }
  
  def find(x: Node, key: Int): Node = {
    var temp = x
    while(temp != null && key != temp.key){
      if(key < temp.key){
        temp = temp.left
      } else {
        temp = temp.right
      }
    }
    temp
  }

  def delete(z: Node): Unit = {
    var temp = if(z.left == null || z.right == null) z else {
      getSuccessor(z)
    }
    
    var x = if(temp.left != null){
      temp.left
    } else {
      temp.right
    }
    
    if(x != null){
      x.parent = temp.parent
    }
    
    if(temp.parent == null) {
      root = x
    } else {
      if(temp == temp.parent.left){
        temp.parent.left = x
      } else {
        temp.parent.right = x
      }
    }
    
    if(temp != z){
      z.key = temp.key
    }

  }
  
  def getSuccessor(x: Node): Node = {
    if(x.right != null){
      minimum(x.right)
    } else {
      var temp1 = x
      var temp2 = x.parent
      while(temp2 != null && temp1 == temp2.right){
        temp1 = temp2
        temp2 = temp2.parent
      }
      temp2
    }
    
  }
  
  def minimum(x: Node): Node = {
    var temp = x
    while(temp.left != null){
      temp = temp.left
    }
    temp
  }

  print(buff.mkString(""))
}