class BinarySearchTree:
    def __init__(self):
        self.lcs = {}
        self.rcs = {}
        self.preorder = []
        self.inorder = []
        self.root = None

    def insert(self, key):
        if self.root is None:
            self.root = key
            self.lcs[key] = None
            self.rcs[key] = None
            return
        parent = self.root
        while True:
            if key < parent:
                if self.lcs[parent] is None:
                    self.lcs[parent] = key
                    self.lcs[key] = None
                    self.rcs[key] = None
                    break
                else:
                    parent = self.lcs[parent]
            else:
                if self.rcs[parent] is None:
                    self.rcs[parent] = key
                    self.lcs[key] = None
                    self.rcs[key] = None
                    break
                else:
                    parent = self.rcs[parent]

    def _find(self, key, cur):
        if key == cur:
            return True
        elif key < cur:
            if self.lcs[cur] is None:
                return False
            else:
                return self._find(key, self.lcs[cur])
        else:
            if self.rcs[cur] is None:
                return False
            else:
                return self._find(key, self.rcs[cur])

    def find(self, key):
        return self._find(key, self.root)

    def _search(self, cur):
        self.preorder.append(cur)
        if self.lcs[cur]:
            self._search(self.lcs[cur])
        self.inorder.append(cur)
        if self.rcs[cur]:
            self._search(self.rcs[cur])

    def print(self):
        self.preorder = []
        self.inorder = []
        self._search(self.root)
        print(' ' + ' '.join(map(str, self.inorder)))
        print(' ' + ' '.join(map(str, self.preorder)))

    def _delete(self, key, cur, parent):
        if key == cur:
            if self.lcs[cur] is None:
                # print('R', key)
                if self.lcs[parent] == cur:
                    self.lcs[parent] = self.rcs[cur]
                else:
                    self.rcs[parent] = self.rcs[cur]
            elif self.rcs[cur] is None:
                # print('L', key)
                if self.lcs[parent] == cur:
                    self.lcs[parent] = self.lcs[cur]
                else:
                    self.rcs[parent] = self.lcs[cur]
            else:
                # print('B', key)
                n = self.inorder[self.inorder.index(key) + 1]
                self._delete(n, cur, parent)
                self.lcs[n], self.rcs[n] = self.lcs[cur], self.rcs[cur]
                self.lcs[cur], self.rcs[cur] = None, None
                if key == self.root:
                    self.root = n
                elif self.lcs[parent] == cur:
                    self.lcs[parent] = n
                else:
                    self.rcs[parent] = n
            return True
        elif key < cur:
            if self.lcs[cur] is None:
                return False
            else:
                return self._delete(key, self.lcs[cur], cur)
        else:
            if self.rcs[cur] is None:
                return False
            else:
                return self._delete(key, self.rcs[cur], cur)

    def delete(self, key):
        self.preorder = []
        self.inorder = []
        self._search(self.root)
        self._delete(key, self.root, None)


T = BinarySearchTree()
N = int(input())
for _ in range(N):
    s = input().split()
    if s[0] == 'insert':
        T.insert(int(s[1]))
    elif s[0] == 'find':
        print('yes' if T.find(int(s[1])) else 'no')
    elif s[0] == 'delete':
        T.delete(int(s[1]))
    else:
        T.print()



Measured memory_bytes: 100712