// ALDS1_8_C: Binary Search Tree 3

use std::cell::RefCell;
use std::rc::Rc;
use std::str::FromStr;

fn scan<T: FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).unwrap();
    s.trim().parse().ok().unwrap()
}

#[derive(PartialEq)]
enum BST {
    Node { key: i64, left: Rc<RefCell<BST>>, right: Rc<RefCell<BST>> },
    Nil
}

impl BST {
    fn new() -> BST {
        BST::Nil
    }
    fn insert(&mut self, k: i64) {
        match *self {
            BST::Nil => {
                *self = BST::Node {
                    key:k,
                    left: Rc::new(RefCell::new(BST::Nil)),
                    right: Rc::new(RefCell::new(BST::Nil)),
                }
            },
            BST::Node { ref mut key, ref mut left, ref mut right } => {
                if k < *key {
                    left.borrow_mut().insert(k);
                } else {
                    right.borrow_mut().insert(k);
                }
            }
        }
    }
    fn find(&self, k: i64) -> bool {
        match *self {
            BST::Nil => { 
                false
            },
            BST::Node { ref key, ref left, ref right} => {
                if k < *key {
                    left.borrow().find(k)
                } else if k > *key {
                    right.borrow().find(k)
                } else {
                    true
                }
            }
        }
    }
     fn delete(&mut self, k: i64) {
        let mut delete_self = false;
        let mut x = None;
        match *self {
            BST::Nil => {},
            BST::Node { ref mut key, ref left, ref right} => {
                if k == *key {
                    if *left.borrow() == BST::Nil && *right.borrow() == BST::Nil {
                        delete_self = true;
                    } else if *right.borrow() == BST::Nil {
                        x = Some(Rc::clone(left));
                    } else if *left.borrow() == BST::Nil {
                        x = Some(Rc::clone(right));
                    } else {
                        if let Some(m) = Self::tree_minimum(right.clone()) {
                           right.borrow_mut().delete(m);
                           *key = m;
                        }
                    }
                } else if k < *key {
                    left.borrow_mut().delete(k);
                } else {
                    right.borrow_mut().delete(k);
                }
            }
        }
        if delete_self {
            *self = BST::Nil;
        } else if let Some(x) = x {
            match *x.borrow() {
                BST::Nil => {},
                BST::Node { ref key, ref left, ref right } => {
                    *self = BST::Node {
                        key: *key,
                        left: left.clone(),
                        right: right.clone(),
                    };
                }
            }
        }
    }
    fn tree_minimum(node: Rc<RefCell<BST>>) -> Option<i64> {
        match *node.borrow() {
            BST::Nil => {
                None
            },
            BST::Node { key, ref left, right: _ } => {
                match *left.borrow() {
                    BST::Nil => { 
                        Some(key)
                    },
                    BST::Node {..} => {
                        Self::tree_minimum(left.clone())
                    }
                }
            }
        }
    }
    fn preorder(&self) {
        match *self {
            BST::Nil => {},
            BST::Node { ref key, ref left, ref right} => {
                print!(" {}", *key);
                left.borrow().preorder();
                right.borrow().preorder();
            }
        }
    }
    fn inorder(&self) {
        match *self {
            BST::Nil => {},
            BST::Node { ref key, ref left, ref right } => {
                left.borrow().inorder();
                print!(" {}", *key);
                right.borrow().inorder();
            }
        }
    }
}

fn main() {
    let n = scan::<usize>();
    let mut t = BST::new();
    for _ in 0..n {
        let mut s = String::new();
        std::io::stdin().read_line(&mut s).unwrap();
        let mut it = s.split_whitespace();
        let com = it.next().unwrap();
        if com == "find" {
            let x = it.next().unwrap().parse::<i64>().ok().unwrap();
            if t.find(x) {
                println!("yes");
            } else {
                println!("no");
            }
        } else if com == "insert" {
            let x = it.next().unwrap().parse::<i64>().ok().unwrap();
            t.insert(x);
        } else if com == "delete" {
            let x = it.next().unwrap().parse::<i64>().ok().unwrap();
            t.delete(x);
        } else if com == "print" {
            t.inorder();
            println!();
            t.preorder();
            println!();
        }
    }
}


Measured memory_bytes: 41892