package main

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

type Node struct {
	value  int
	parent *Node
	left   *Node
	right  *Node
}

type Tree struct {
	root *Node
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	n, err := nextInt(sc)
	if err != nil {
		panic(err)
	}

	tree := &Tree{}
	for i := 0; i < n; i++ {
		command := nextText(sc)
		if command == "insert" {
			x, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			insertTree(x, tree)
		} else if command == "print" {
			printTree(tree)
		} else if command == "find" {
			x, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			if findTree(x, tree) {
				fmt.Println("yes")
			} else {
				fmt.Println("no")
			}
		} else if command == "delete" {
			x, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			deleteTree(x, tree)
		} else {
			panic(fmt.Errorf("unknown command: %s\n", command))
		}
	}
}

func insertTree(x int, tree *Tree) {
	if tree.root == nil {
		tree.root = &Node{x, nil, nil, nil}
	} else {
		insertNode(x, tree.root)
	}
}

func insertNode(x int, node *Node) {
	for {
		if node.value < x {
			if node.right == nil {
				node.right = &Node{x, node, nil, nil}
				break
			} else {
				node = node.right
			}
		} else {
			if node.left == nil {
				node.left = &Node{x, node, nil, nil}
				break
			} else {
				node = node.left
			}
		}
	}
}

func printTree(tree *Tree) {
	if tree.root == nil {
		fmt.Println("")
		fmt.Println("")
	} else {
		printResult(inorder(tree.root, []int{}))
		printResult(preorder(tree.root, []int{}))
	}
}

func findTree(x int, tree *Tree) bool {
	return findNode(x, tree.root)
}

func findNode(x int, node *Node) bool {
	for node != nil {
		if node.value == x {
			return true
		} else if node.value < x {
			node = node.right
		} else {
			node = node.left
		}
	}
	return false
}

// There is an error when delete root
func deleteTree(x int, tree *Tree) {
	node := tree.root
	lastDirection := -1

	for node != nil {
		if node.value < x {
			node = node.right
			lastDirection = 1
		} else if node.value > x {
			node = node.left
			lastDirection = 0
		} else {
			// delete operation
			if node.left != nil && node.right != nil {
				rightLowestNode := node.right
				for rightLowestNode.left != nil {
					rightLowestNode = rightLowestNode.left
				}
				node.value = rightLowestNode.value
				if rightLowestNode == node.right {
					rightLowestNode.parent.right = nil
				} else {
					rightLowestNode.parent.left = nil
				}
			} else if node.left != nil {
				node.left.parent = node.parent
				if lastDirection != -1 {
					if lastDirection == 1 {
						node.parent.right = node.left
					} else if lastDirection == 0 {
						node.parent.left = node.left
					}
				} else {
					tree.root = node.left
				}
			} else if node.right != nil {
				node.right.parent = node.parent
				if lastDirection != -1 {
					if lastDirection == 1 {
						node.parent.right = node.right
					} else if lastDirection == 0 {
						node.parent.left = node.right
					}
				} else {
					tree.root = node.right
				}
			} else {
				if lastDirection != -1 {
					if lastDirection == 1 {
						node.parent.right = nil
					} else if lastDirection == 0 {
						node.parent.left = nil
					}
				} else {
					tree.root = nil
				}
			}
			break
		}
	}

}

func preorder(top *Node, vs []int) []int {
	vs = append(vs, top.value)
	if top.left != nil {
		vs = preorder(top.left, vs)
	}
	if top.right != nil {
		vs = preorder(top.right, vs)
	}
	return vs
}

func inorder(top *Node, vs []int) []int {
	if top.left != nil {
		vs = inorder(top.left, vs)
	}
	vs = append(vs, top.value)
	if top.right != nil {
		vs = inorder(top.right, vs)
	}
	return vs
}

func nextText(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

func nextInt(sc *bufio.Scanner) (int, error) {
	sc.Scan()
	return strconv.Atoi(sc.Text())
}

func printResult(result []int) {
	writer := bufio.NewWriter(os.Stdout)
	for i := 0; i < len(result); i++ {
		writer.Write([]byte(fmt.Sprintf(" %d", result[i])))
	}
	writer.Write([]byte("\n"))
	writer.Flush()
}



Measured memory_bytes: 38340