package main

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

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

// main
func main() {
	//testFunction()
	solve(bufio.NewScanner(os.Stdin))
}

// test
func testFunction() {
	input := `

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
`
	sc := bufio.NewScanner(strings.NewReader(strings.Trim(input, "\n")))
	solve(sc)
}

func preOrder(node *Node, accm []int) []int {
	if node == nil {
		return accm
	}
	accm = append(accm, node.key)
	accm = preOrder(node.left, accm)
	accm = preOrder(node.right, accm)
	return accm
}

func inOrder(node *Node, accm []int) []int {
	if node == nil {
		return accm
	}
	accm = inOrder(node.left, accm)
	accm = append(accm, node.key)
	accm = inOrder(node.right, accm)
	return accm
}

func postOrder(node *Node, accm []int) []int {
	if node == nil {
		return accm
	}
	accm = postOrder(node.left, accm)
	accm = postOrder(node.right, accm)
	accm = append(accm, node.key)
	return accm
}

//
func NewNode(key int) *Node {
	return &Node{
		key:    key,
		parent: nil,
		left:   nil,
		right:  nil,
	}
}

//func insert(node *Node, key int) *Node {
//	if node == nil {
//		return NewNode(key)
//	}
//
//	if node.key > key {
//		node.left = insert(node.left, key)
//	}
//	if node.key < key {
//		node.right = insert(node.right, key)
//	}
//	node.parent = node
//	return node
//}
func insert(node *Node, key int) *Node {

	var parent *Node
	n := node
	for n != nil {
		parent = n
		if n.key > key {
			n = n.left
		} else {
			n = n.right
		}
	}

	nn := NewNode(key)
	if parent == nil {
		return nn
	}
	nn.parent = parent
	if key < parent.key {
		parent.left = nn
	} else {
		parent.right = nn
	}

	return node
}

func find(node *Node, key int) *Node {
	n := node
	for n != nil && n.key != key {
		if n.key < key {
			n = n.right
		} else {
			n = n.left
		}
	}
	return n
}

func delete(node *Node, key int) {

	target := find(node, key)
	if target == nil {
	    return
	}

	if target.left == nil && target.right == nil {
		if target.parent.left == target {
			target.parent.left = nil
		} else {
			target.parent.right = nil
		}
	} else if target.left == nil {
		if target.parent.left == target {
			target.parent.left = target.right
		} else {
			target.parent.right = target.right
		}
		target.right.parent = target.parent
	} else if target.right == nil {
		if target.parent.left == target {
			target.parent.left = target.left
		} else {
			target.parent.right = target.left
		}
		target.left.parent = target.parent
	} else {
		n := target.right
		for n.left != nil{
			n = n.left
		}

		target.key = n.key
		
		if n.parent.left == n {
			n.parent.left = n.right
		} else {
			n.parent.right = n.right
		}

		if n.right != nil {
		    n.right.parent = n.parent
		}
	}
}

//
func printList(accm []int) {
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range accm {
		buf.Write([]byte(fmt.Sprintf(" %d", v)))
	}
	buf.Flush()
}

func print(node *Node) {
	var accm []int
	accm = inOrder(node, accm)
	printList(accm)
	fmt.Println()
	accm = make([]int, 0)
	accm = preOrder(node, accm)
	printList(accm)
	fmt.Println()
}

// solve
func solve(sc *bufio.Scanner) {
	var node *Node
	sc.Split(bufio.ScanWords)
	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())
	for i := 0; i < n; i++ {
		sc.Scan()
		command := sc.Text()
		if command == "insert" {
			sc.Scan()
			value, _ := strconv.Atoi(sc.Text())
			node = insert(node, value)
		} else if command == "find" {
			sc.Scan()
			value, _ := strconv.Atoi(sc.Text())
			if found := find(node, value); found != nil {
				fmt.Println("yes")
			} else {
				fmt.Println("no")
			}
		} else if command == "delete" {
			sc.Scan()
			value, _ := strconv.Atoi(sc.Text())
			delete(node, value)
		} else if command == "print" {
			print(node)
		}
	}
}



Measured memory_bytes: 36324