package main

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

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

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

var root *Node

func insert(z *Node) {
	var y *Node
	x := root
	for x != nil {
		y = x
		if z.key < y.key {
			x = y.left
		} else {
			x = y.right
		}
	}
	z.parent = y
	if y == nil {
		root = z
	} else if z.key < y.key {
		y.left = z
	} else {
		y.right = z
	}
}

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

func printByInOrder(w io.Writer, n *Node) {
	if n.left != nil {
		printByInOrder(w, n.left)
	}
	fmt.Fprint(w, " ", n.key)
	if n.right != nil {
		printByInOrder(w, n.right)
	}
}

func printByPreOrder(w io.Writer, n *Node) {
	fmt.Fprint(w, " ", n.key)
	if n.left != nil {
		printByPreOrder(w, n.left)
	}
	if n.right != nil {
		printByPreOrder(w, n.right)
	}
}

func deleteNode(k int) {
	n := find(k)
	if n.left == nil && n.right == nil {
		if p := n.parent; p != nil {
			if p.left == n {
				p.left = nil
			} else {
				p.right = nil
			}
		} else {
			root = nil
		}
	} else if (n.left != nil && n.right == nil) || (n.left == nil && n.right != nil) {
		var c *Node
		if n.left != nil {
			c = n.left
		} else {
			c = n.right
		}
		p := n.parent
		if p != nil {
			if p.left == n {
				p.left = c
			} else {
				p.right = c
			}
		} else {
			root = c
		}
		c.parent = p
	} else {
		next := n.right
		for next.left != nil {
			next = next.left
		}
		nextKey := next.key
		deleteNode(nextKey)
		n.key = nextKey
	}
}

func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	N := sc.nextInt()
	for i := 0; i < N; i++ {
		op := sc.nextStr()
		if op == "insert" {
			insert(&Node{sc.nextInt(), nil, nil, nil})
		} else if op == "print" {
			printByInOrder(wtr, root)
			fmt.Fprintln(wtr)
			printByPreOrder(wtr, root)
			fmt.Fprintln(wtr)
		} else if op == "find" {
			if find(sc.nextInt()) != nil {
				fmt.Fprintln(wtr, "yes")
			} else {
				fmt.Fprintln(wtr, "no")
			}
		} else {
			deleteNode(sc.nextInt())
		}
	}

	wtr.Flush()
}



Measured memory_bytes: 23572