{-# OPTIONS_GHC -Wall #-}

-- foldM_ を使うとMLEするので…
foldM_' :: (Monad m) => (a -> b -> m a) -> a -> [b] -> m ()
foldM_' _ _ [] = return ()
foldM_' f z (x:xs) = do
    z' <- f z x
    z' `seq` foldM_' f z' xs

readInt :: String -> Int
readInt = read

data BST a = Empty | Node a (BST a) (BST a)
    deriving (Show)

bstFind :: (Ord a) => BST a -> a -> BST a
bstFind Empty _ = Empty
bstFind node@(Node y l r) x
    | x == y    = node
    | x <  y    = bstFind l x
    | otherwise = bstFind r x

bstInsert :: (Ord a) => BST a -> a -> BST a
bstInsert Empty x = Node x Empty Empty
bstInsert node@(Node y l r) x
    | x == y    = node
    | x <  y    = Node y (bstInsert l x) r
    | otherwise = Node y l (bstInsert r x)

bstDelete :: (Ord a) => BST a -> a -> BST a
bstDelete Empty _ = Empty
bstDelete node@(Node y Empty Empty) x
    | x == y    = Empty
    | otherwise = node
bstDelete (Node y l Empty) x
    | x == y    = l
    | otherwise = Node y (bstDelete l x) Empty
bstDelete (Node y Empty r) x
    | x == y    = r
    | otherwise = Node y Empty (bstDelete r x)
bstDelete (Node y l r) x
    | x == y    = let z = bstMinimum r in
                          Node z l (bstDelete r z)
    | x <  y    = Node y (bstDelete l x) r
    | otherwise = Node y l (bstDelete r x)
    where
        bstMinimum :: (Ord a) => BST a -> a
        bstMinimum Empty             = error "invalid argument"
        bstMinimum (Node x' Empty _) = x'
        bstMinimum (Node _ l' _)     = bstMinimum l'

bstPreorder :: (Ord a) => BST a -> [a]
bstPreorder Empty = []
bstPreorder (Node x l r) = [x] ++ bstPreorder l ++ bstPreorder r

bstInorder :: (Ord a) => BST a -> [a]
bstInorder Empty = []
bstInorder (Node x l r) = bstInorder l ++ [x] ++ bstInorder r

doCmd :: BST Int -> [String] -> IO (BST Int)
doCmd node ("find":arg0:_) = do
    let x = readInt arg0
    let found = case bstFind node x of
                    Node{} -> True
                    Empty  -> False
    putStrLn $ if found then "yes" else "no"
    return node
doCmd node ("insert":arg0:_) = do
    let x = readInt arg0
    return $ bstInsert node x
doCmd node ("delete":arg0:_) = do
    let x = readInt arg0
    return $ bstDelete node x
doCmd node ("print":_) = do
    putStrLn . concatMap ((' ':) . show) $ bstInorder  node
    putStrLn . concatMap ((' ':) . show) $ bstPreorder node
    return node
doCmd _ _ = error "invalid command"

main :: IO ()
main = do
    _    <- getLine
    cmds <- fmap (map words . lines) getContents

    let bst = Empty :: BST Int

    foldM_' doCmd bst cmds

