import java.io._
import scala.io.StdIn.{readInt, readLine}

object Main {
  val out:BufferedWriter =
    new BufferedWriter(new OutputStreamWriter(new FileOutputStream(FileDescriptor.out), "ASCII"), 512)
  var count = 0

  def printstr(str:String) = {
    out.write(str)
    if(count<500) count += 1
    else { out.flush; count = 0 }
  }

  class Node(var key:Option[Long] = None, var left:Option[Node] = None, var right:Option[Node] = None, var parent:Option[Node] = None) { }

  def getNodeToInsert(node:Option[Node],keyNode:Option[Node]):Option[Node] =
    if(node.get.key.get > keyNode.get.key.get) {
      if(node.get.left == None) node
      else getNodeToInsert(node.get.left,keyNode)
    } else {
      if(node.get.right == None) node
      else getNodeToInsert(node.get.right, keyNode)
    }

  def insert(root:Option[Node], keyNode:Option[Node]) = {
    val target = getNodeToInsert(root,keyNode)
    if(keyNode.get.key.get < target.get.key.get) target.get.left = keyNode
    else target.get.right = keyNode
    keyNode.get.parent = target
  }

  def inOrder(node:Option[Node]):Unit =
    if(node == None|| node.get.key == None) return
    else {
      inOrder(node.get.left)
      printstr(" ")
      printstr(node.get.key.get.toString)
      inOrder(node.get.right)
    }

  def preOrder(node:Option[Node]):Unit =
    if(node == None || node.get.key == None) return
    else {
      printstr(" ")
      printstr(node.get.key.get.toString)
      preOrder(node.get.left)
      preOrder(node.get.right)
    }

  def find(root:Option[Node], key:Long):Option[Node] = {
    def loop(node:Option[Node]):Option[Node] = {
      if(node == None || node.get.key == None) {
        None
      } else if(node.get.key.get > key) {
        loop(node.get.left)
      } else if(node.get.key.get < key) {
        loop(node.get.right)
      } else {
        node
      }
    }
    loop(root)
  }

  def getNode(node:Option[Node]):Option[Node] = {
    def loop(x:Option[Node]):Option[Node] = {
      if(x.get.left == None) x
      else loop(x.get.left)
    }
    loop(node)
  }

  def delete(root:Option[Node], key:Long) = {
    val node = find(root, key)
    if(node != None) {
      if(node.get.left == None && node.get.right == None) {
        if(node.get.parent.get.left == node) node.get.parent.get.left = None else node.get.parent.get.right = None
        node.get.key = None
      } else if(node.get.left == None) {
        if(node.get.parent.get.left == node) node.get.parent.get.left = node.get.right else node.get.parent.get.right = node.get.right
      } else if(node.get.right == None) {
        if(node.get.parent.get.left == node) node.get.parent.get.left = node.get.left else node.get.parent.get.right = node.get.left
      } else {
        val newNode = getNode(node.get.right)
        node.get.key = newNode.get.key
        if(newNode.get.parent.get.left == newNode) newNode.get.parent.get.left = None else newNode.get.parent.get.right = None
        newNode.get.key = None
      }
    }
  }

  def main(args:Array[String]) = {
    val n = readInt
    val root = new Node

    for(i<-0 until n) {
      val s = readLine.split(" ")
      s(0) match {
        case "insert" =>
          if(root.key == None) root.key = Some(s(1).toLong)
          else insert(Some(root), Some(new Node(Some(s(1).toLong))))
        case "find" => {
          val node = find(Some(root), s(1).toInt)
          if(node!=None) printstr("yes\n") else printstr("no\n")
        }
        case "delete" => {
          delete(Some(root), s(1).toInt)
        }
        case _ => {
          inOrder(Some(root))
          printstr("\n")
          preOrder(Some(root))
          printstr("\n")
        }
      }
    }
    out.flush
    out.close
  }
}

Measured memory_bytes: 295008