package main

import (
	"bufio"
	"bytes"
	"fmt"
	"io"
	"log"
	"os"
	"strconv"
	"strings"
)

// Node  ノード
type Node struct {
	id    int
	left  *Node
	right *Node
}

func (n *Node) String() string {
	if n == nil {
		return "nil"
	}
	var left, right string
	if n.left == nil {
		left = "nil"
	} else {
		left = fmt.Sprintf("%d", n.left.id)
	}
	if n.right == nil {
		right = "nil"
	} else {
		right = fmt.Sprintf("%d", n.right.id)
	}
	return fmt.Sprintf("{id[%d] left[%s] right[%s]}", n.id, left, right)
}

func main() {
	var root *Node
	reader := bufio.NewReader(os.Stdin)
	n := eGetInt(reader)
	for i := 0; i < n; i++ {
		infos := strings.Fields(eGetLine(reader))
		if len(infos) == 1 && infos[0] == "print" {
			printInOrder(root)
			printPreOrder(root)
		} else {
			val := eAtoi(infos[1])

			switch infos[0] {
			case "insert":
				root = insert(root, val)
			case "find":
				found := find(root, val)
				if found != nil {
					fmt.Println("yes")
				} else {
					fmt.Println("no")
				}
			case "delete":
				root = delete(root, val)
			default:
				panic(fmt.Sprintf("unexpected input[%s]", infos[0]))
			}
		}
	}
}
func debugPrintPreOrder(root *Node) {
	var buf bytes.Buffer
	preOrder(root, func(n *Node) {
		buf.WriteString(fmt.Sprintf("%s\n", n))
	})
	fmt.Print(buf.String())
}
func printPreOrder(root *Node) {
	var buf bytes.Buffer
	preOrder(root, func(n *Node) {
		buf.WriteString(fmt.Sprintf(" %d", n.id))
	})
	fmt.Println(buf.String())
}
func printInOrder(root *Node) {
	var buf bytes.Buffer
	inOrder(root, func(n *Node) {
		buf.WriteString(fmt.Sprintf(" %d", n.id))
	})
	fmt.Println(buf.String())
}

func insert(root *Node, n int) *Node {
	current := root
	var parent *Node
	for current != nil {
		parent = current
		if n < current.id {
			current = current.left
		} else {
			current = current.right
		}
	}
	current = &Node{n, nil, nil}
	if parent == nil {
		return current
	} else if current.id < parent.id {
		parent.left = current
		return root
	} else {
		parent.right = current
		return root
	}
}
func delete(root *Node, n int) *Node {
	current := root
	var currentP **Node

	// 該当ノードまで移動
	for current != nil && current.id != n {
		if n < current.id {
			currentP = &current.left
			current = current.left
		} else {
			currentP = &current.right
			current = current.right
		}
	}
	if current != nil {
		if current.left == nil && current.right == nil {
			// 子なしノード
			(*currentP) = nil
		} else if current.left == nil {
			(*currentP) = current.right
		} else if current.right == nil {
			(*currentP) = current.left
		} else {
			// 右の子の最小の要素を探す
			minP := &current.right
			min := current.right
			for min.left != nil {
				minP = &min.left
				min = min.left
			}
			// 削除するノードの値を入れ替える
			// 木構造は保ちたいので,
			// 値だけ入れ替えるのがミソ
			current.id = min.id

			// 最小のノードを削除
			if min.right == nil {
				// 子なしノード
				(*minP) = nil
			} else {
				(*minP) = min.right
			}
		}
	}
	return root
}
func find(root *Node, n int) *Node {
	current := root
	for current != nil {
		if current.id == n {
			break
		}
		if n < current.id {
			current = current.left
		} else {
			current = current.right
		}
	}
	return current
}

func preOrder(current *Node, f func(n *Node)) {
	if current == nil {
		return
	}
	f(current)
	preOrder(current.left, f)
	preOrder(current.right, f)
	return
}
func inOrder(current *Node, f func(n *Node)) {
	if current == nil {
		return
	}
	inOrder(current.left, f)
	f(current)
	inOrder(current.right, f)
	return
}
func postOrder(current *Node, f func(n *Node)) {
	if current == nil {
		return
	}
	postOrder(current.left, f)
	postOrder(current.right, f)
	f(current)
	return
}

func eGetLine(r *bufio.Reader) string {
	s, err := r.ReadString(byte('\n'))
	if err != nil {
		if err != io.EOF {
			log.Fatal(err.Error())
		}
		return s
	}
	return strings.TrimRight(s, "\r\n")
}
func eGetInt(r *bufio.Reader) int {
	line := eGetLine(r)
	return eAtoi(line)
}
func eGetInts(r *bufio.Reader) []int {
	line := eGetLine(r)
	fields := strings.Split(line, " ")
	var ints []int
	for _, f := range fields {
		ints = append(ints, eAtoi(f))
	}
	return ints
}
func eAtoi(s string) int {
	n, err := strconv.Atoi(s)
	if err != nil {
		log.Fatal(err)
	}
	return n
}



Measured memory_bytes: 44336