class Node(object):

    def __init__(self, key):
        self.left = None
        self.right = None
        self.parent = None

        self.key = key


class Tree(object):

    def __init__(self):
        self.root = None

    def insert(self, new_key):
        x = self.root
        y = None

        while x is not None:
            y = x

            if new_key < y.key:
                x = y.left
            else:
                x = y.right

        new_node = Node(new_key)
        new_node.parent = y
        if y is None:
            self.root = new_node
        elif new_key < y.key:
            y.left = new_node
        else:
            y.right = new_node

    def inorder(self, now):
        visited = list()
        if now is None:
            return visited

        visited.extend(self.inorder(now.left))
        visited.append(now.key)
        visited.extend(self.inorder(now.right))
        return visited

    def preorder(self, now):
        visited = list()
        if now is None:
            return visited

        visited.append(now.key)
        visited.extend(self.preorder(now.left))
        visited.extend(self.preorder(now.right))
        return visited

    def order_print(self):
        keys = list()
        keys.append(self.inorder(self.root))
        keys.append(self.preorder(self.root))

        for visited_keys in keys:
            print "",
            print " ".join(map(str, visited_keys))

    def find(self, key):
        """
        keys = self.inorder(self.root)
        if key in keys:
            print "yes"
        else:
            print "no"
        """
        x = self.root
        y = None

        while not (x is None or x.key == key):
            y = x
            if key < y.key:
                x = y.left
            else:
                x = y.right

        if x is not None:
            print "yes"
        else:
            print "no"


    def min_node(self, now):
        """min_node
        Return the node with the minimum key,
        treeting now as the root of a sub tree.
        """
        x = now
        while x.left is not None:
            x = x.left

        return x
        """
        if now.left is not None:
            print "now: {}, left: {}".format(now.key, now.left.key)
            return self.min_node(now.left)
        print "now: {}, left: {}, right: {}".format(now.key, now.left, now.right)
        return now
        """


    def delete(self, key):
        x = self.root
        y = None

        while not(x is None or x.key == key):
            y = x
            if key < y.key:
                x = y.left
            else:
                x = y.right

        # y is parent of x here
        if x.left is None and x.right is None:
            if y.left == x:
                y.left = None
            else:
                y.right = None
        elif x.left is None or x.right is None:
            child = x.left if x.left is not None else x.right
            child.parent = y
            if y.left == x:
                y.left = child
            else:
                y.right = child
        else:
            next_node = self.min_node(x.right)
            tmp = next_node.key
            self.delete(next_node.key)
            x.key = next_node.key


def main():
    tree = Tree()

    num_orders = int(raw_input())
    for _ in xrange(num_orders):
        order = raw_input()
        if order[0] == 'i':
            new_key = int(order.split()[1])
            tree.insert(new_key)
        elif order[0] == 'p':
            tree.order_print()
        elif order[0] == 'f':
            key = int(order.split()[1])
            tree.find(key)
        elif order[0] == 'd':
            key = int(order.split()[1])
            tree.delete(key)


if __name__ == "__main__":
    main()

Measured memory_bytes: 188192