package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)

	var tree Tree

	tree.root = nil

	for i := 0; i < n; i++ {
		order := getNextString(scanner)
		switch order {
		case "print":
			tree.i = 0
			tree.keys = make([]int, tree.n)
			tree.printInOrders(tree.root)
			fmt.Fprintln(writer, fmt.Sprintf(" %s", joinint(tree.keys, " ")))
			tree.i = 0
			tree.keys = make([]int, tree.n)
			tree.printPreOrders(tree.root)
			fmt.Fprintln(writer, fmt.Sprintf(" %s", joinint(tree.keys, " ")))
		case "insert":
			key := getNextInt(scanner)
			tree.insert(key)
		case "delete":
			key := getNextInt(scanner)
			del := tree.find(key)
			tree.delete(del)
		case "find":
			key := getNextInt(scanner)
			if tree.find(key) != nil {
				fmt.Fprintln(writer, "yes")
				continue
			}
			fmt.Fprintln(writer, "no")
		}
	}

	writer.Flush()
}

// Tree tree
type Tree struct {
	root *Node
	keys []int
	i    int
	n    int
}

func (t *Tree) insert(key int) {
	t.n++
	node := Node{
		key:    key,
		parent: nil,
		left:   nil,
		right:  nil,
	}
	if t.root == nil {
		t.root = &node
		return
	}

	n := t.root
	var p *Node
	p = nil

	for n != nil {
		p = n
		if n.key < key {
			n = n.right
			continue
		}
		n = n.left
	}
	node.parent = p
	if p.key < key {
		p.right = &node
		return
	}
	p.left = &node
}

func (t *Tree) find(key int) *Node {
	if t.root == nil {
		return nil
	}

	n := t.root

	for n != nil {
		if n.key == key {
			return n
		}
		if n.key < key {
			n = n.right
			continue
		}
		n = n.left
	}

	return nil
}

func (t *Tree) delete(n *Node) {
	if n == nil {
		return
	}
	t.n--
	if n.left == nil && n.right == nil {
		if n.parent == nil {
			t.root = nil
			return
		}
		if n.parent.left == n {
			n.parent.left = nil
			return
		}
		if n.parent.right == n {
			n.parent.right = nil
			return
		}
	}

	if n.left == nil {
		n.right.parent = n.parent
		if n.parent.left == n {
			n.parent.left = n.right
			return
		}
		n.parent.right = n.right
		return
	}
	if n.right == nil {
		n.left.parent = n.parent
		if n.parent.right == n {
			n.parent.right = n.left
			return
		}
		n.parent.left = n.left
		return
	}

	l := n.right
	for l.left != nil {
		l = l.left
	}
	n.key = l.key
	if l == n.right {
		n.right = l.right
	} else {
		l.parent.left = l.right
	}
	if l.right != nil {
		l.right.parent = l.parent
	}
}

func (t *Tree) printPreOrders(n *Node) {
	if n == nil {
		return
	}
	t.keys[t.i] = n.key
	t.i++
	t.printPreOrders(n.left)
	t.printPreOrders(n.right)
}

func (t *Tree) printInOrders(n *Node) {
	if n == nil {
		return
	}
	t.printInOrders(n.left)
	t.keys[t.i] = n.key
	t.i++
	t.printInOrders(n.right)
}

// Node node
type Node struct {
	key    int
	parent *Node
	left   *Node
	right  *Node
}

func joinint(a []int, sep string) string {
	n := len(a)
	ss := make([]string, n)
	for i := 0; i < n; i++ {
		ss[i] = fmt.Sprintf("%d", a[i])
	}
	return strings.Join(ss, sep)
}



Measured memory_bytes: 55640