Problem:

Binary Search Tree III

Write a program which performs the following operations to a binary search tree $T$ by adding delete operation to B: Binary Search Tree II.

  
insert  $k$: Insert a node containing $k$ as key into $T$.

  
find $k$: Report whether $T$ has a node containing $k$. 

  
delete $k$: Delete a node containing $k$.

  
print: Print the keys of the binary search tree by inorder tree walk and  preorder tree walk respectively.

The operation delete $k$ for deleting a given node $z$ containing key $k$ from $T$ can be implemented by an algorithm which considers the following cases:

 If $z$ has no children, we modify its parent $z.p$ to replace $z$ with NIL as its child (delete $z$).

 If $z$ has only a single child, we "splice out" $z$ by making a new link between its child and its parent.

 If $z$ has two children, we splice out $z$'s successor $y$ and replace $z$'s key with $y$'s key.

Input

   In the first line, the number of operations $m$ is given. In the following $m$ lines, operations represented by insert $k$, find $k$, delete $k$ or print are given.

Output

For each find $k$ operation, print "yes" if $T$ has a node containing $k$, "no" if not.

  In addition, for each print operation, print a list of keys obtained by inorder tree walk and preorder tree walk in a line respectively. Put a space character before each key

Constraints

   
The number of operations $\leq 500,000$

The number of print operations $\leq 10$.

  
$-2,000,000,000 \leq key \leq 2,000,000,000$

  
The height of the binary tree does not exceed 100 if you employ the above pseudo code.

The keys in the binary search tree are all different.

Sample Input 1

18
insert 8
insert 2
insert 3
insert 7
insert 22
insert 1
find 1
find 2
find 3
find 4
find 5
find 6
find 7
find 8
print
delete 3
delete 7
print

Sample Output 1

yes
yes
yes
no
no
no
yes
yes
 1 2 3 7 8 22
 8 2 1 3 7 22
 1 2 8 22
 8 2 1 22

Reference

Introduction to Algorithms, Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. The MIT Press.



Sample input:
18
insert 8
insert 2
insert 3
insert 7
insert 22
insert 1
find 1
find 2
find 3
find 4
find 5
find 6
find 7
find 8
print
delete 3
delete 7
print


Sample output:
yes
yes
yes
no
no
no
yes
yes
 1 2 3 7 8 22
 8 2 1 3 7 22
 1 2 8 22
 8 2 1 22

Submission status: Accepted

Solution code:
import sys

def main():
    n = input()
    tree = MyBinaryTree(n)
    for i in range(n):
        cmd = map(str,raw_input().split())
        if cmd[0] == "insert":
            insert(tree,MyNode(int(cmd[1])))
        elif cmd[0] == "print":
            tree.inorder(tree.root)
            print ""
            tree.preorder(tree.root)
            print ""
        elif cmd[0] == "find":
            find(tree,int(cmd[1]))
        elif cmd[0] == "delete":
            delete(tree,int(cmd[1]))
    
    
class MyBinaryTree():
    def __init__(self,num_nodes):
        self.root = None

    def preorder(self,node):
        sys.stdout.write(" {}".format(node.key))
        l = node.left
        r = node.right
        if l is not None:
            self.preorder(l)
        if r is not None:
            self.preorder(r)    

    def inorder(self,node):
        l = node.left
        r = node.right
        if l is not None:
            self.inorder(l)
        sys.stdout.write(" {}".format(node.key))
        if r is not None:
            self.inorder(r)


def insert(T,z):
    y = None
    x = T.root
    while x is not None:
        y = x
        if z.key < x.key:
            x = x.left
        else:
            x = x.right
    z.parent = y

    if y is None:
        T.root = z
    elif z.key < y.key:
        y.left = z
    else:
        y.right = z


def find(T,key):
    x = T.root
    while x is not None and key != x.key:
        if key < x.key:
            x = x.left
        else:
            x = x.right

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


def delete(T,key):
    x = T.root
    where = None
    while x is not None and key != x.key:
        if key < x.key:
            x = x.left
            where = 0
        else:
            x = x.right
            where = 1

    if x is not None:
        if x.left is None:
            if x.right is None:
                if where == 0:
                    x.parent.left = None
                else:
                    x.parent.right = None
            elif where == 0:
                x.parent.left  = x.right
                x.right.parent = x.parent
            else:
                x.parent.right = x.right
                x.right.parent = x.parent
            x = None
        elif x.right is None :
            if where == 0:
                x.parent.left = x.left
                x.left.parent = x.parent
            else:
                x.parent.right = x.left
                x.left.parent  = x.parent
            x = None
        else:
            y = x.right
            where = 1
            while y.left is not None:
                y = y.left
                where = 0

            x.key = y.key
            if y.right is not None:
                y.right.parent = y.parent
                y.parent.left  = y.right
            else:
                if where == 0:
                    y.parent.left = None
                else:
                    y.parent.right = None
            y = None
            
        
class MyNode():
    def __init__(self,key):
        self.key =key
        self.parent = None
        self.left = None
        self.right = None


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:

import sys

YES = "yes"
NO = "no"

class Node:
    def __init__(self, value, p = None, l = None, r = None):
        self.key = value
        self.p = p
        self.left = l
        self.right = r

def chgchild(parent, old, new = None):
    if parent == None:
        return
    else:
#        print("\tparent:{}".format(parent.key))
        if parent.left == old:
            parent.left = new
        else:
            parent.right = new
        if new != None:
            new.p = parent

def delete(node):
    if node == None:
        return
#    print("\tdel key:{}".format(node.key))
    if node.left == None:
        chgchild(node.p, node, node.right)
    elif node.right == None:
        chgchild(node.p, node, node.left)
    else:
        ino = []
        inorder(node,ino)
        next = 0
        for i in range(len(ino)):
            if ino[i].key == node.key:
                next = i + 1
                break
        node.key = ino[next].key
        delete(ino[next])

def getroot(x):
    if x.p != None:
        return getroot(x.p)
    return x

def keylist(A):
    B = []
    for i in A:
        B.append(i.key)
    return B

def preorder(x, A):
    if x == None:
        return
    A.append(x)
    preorder(x.left, A)
    preorder(x.right, A)

def inorder(x, A):
    if x == None:
        return
    inorder(x.left, A)
    A.append(x)
    inorder(x.right, A)

def postorder(x, A):
    if x == None:
        return
    postorder(x.left, A)
    postorder(x.right, A)
    A.append(x)

def ptree(root):
    pre = []
    ino = []
    preorder(root, pre)
    inorder(root, ino)
    ap = keylist(pre)
    ip = keylist(ino)
    print(" {}".format(" ".join(map(str,ip))))
    print(" {}".format(" ".join(map(str,ap))))

def find(value, root):
    if root == None:
        return None
    elif value < root.key:
        return find(value,root.left)
    elif value > root.key:
        return find(value,root.right)
    return root

def insert(root, z):
    y = None # x ??????
    x = root  # 'T ??????'
    while x != None:
        y = x # ???????¨????
        if z.key < x.key:
            x = x.left # ?????????????§????
        else:
            x = x.right # ?????????????§????
    z.p = y

    if y == None: # T ???????????´???
        return z
    elif z.key < y.key:
        y.left = z # z ??? y ?????????????????????
    else:
        y.right = z # z ??? y ?????????????????????
    return root

def main():

    """ ????????? """
    num = int(input().strip())
    istr = sys.stdin.read()
    cmds = list(istr.splitlines())
    root = None
    for i in range(num):
        cmd = cmds[i][0]
        if cmd == "i":
            n = Node(int(cmds[i][7:]))
            root = insert(root, n)
        elif cmd == "f":
            if find(int(cmds[i][5:]),root) == None:
                print(NO)
            else:
                print(YES)
        elif cmd == "p":
            ptree(root)
        elif cmd == "d":
            node = find(int(cmds[i][7:]),root)
            delete(node)

 

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
class Node():
    def __init__(self, key):
        self.parent = None
        self.left = None
        self.right = None
        self.key = key

class Tree():
    def __init__(self):
        self.root = None
    
    def insert(self, key):
        z = Node(key)
        y = None
        x = self.root
        
        while x:
            y = x
            if z.key < x.key:
                x = x.left
            else:
                x = x.right
        z.parent = y
        
        if y == None:
            self.root = z
        elif z.key < y.key:
            y.left = z
        else:
            y.right = z
            
    def find(self, key):
        node = self.root
        
        while node:
            if node.key == key:
                return node
            elif node.key > key:
                node = node.left
            else:
                node = node.right
        
        return None
    
    def delete(self, key):
        node = self.find(key)
        
        if node.left and node.right:
            right_min = node.right
            while right_min.left:
                right_min = right_min.left
            node.key = right_min.key
            node = right_min
        
        if node.left or node.right:
            child = node.left if node.left else node.right
            if node.parent.left == node:
                node.parent.left = child
                child.parent = node.parent
            else:
                node.parent.right = child
                child.parent = node.parent
        else:
            if node.parent.left == node:
                node.parent.left = None
            else:
                node.parent.right = None
                
    def preorder(self, node):
        print(" {}".format(node.key), end = "")
        
        if node.left:
            self.preorder(node.left)
        if node.right:
            self.preorder(node.right)
    
    def inorder(self, node):
        if node.left:
            self.inorder(node.left)
            
        print(" {}".format(node.key), end = "")
        
        if node.right:
            self.inorder(node.right)
            
    def out(self):
        self.inorder(self.root)
        print('')
        self.preorder(self.root)
        print('')

tree = Tree()
N = int(input())
for _ in range(N):
    order = input().split()
    if order[0] == 'insert':
        tree.insert(int(order[1]))
    elif order[0] == 'find':
        print('yes' if tree.find(int(order[1])) else 'no')
    elif order[0] == 'delete':
        tree.delete(int(order[1]))
    else:
        tree.out()

Submission status: Accepted

Solution code:
m = int(input())

class Node:
    def __init__(self, key, left ,right):
        self.key = key
        self.left = left
        self.right = right


class BinaryTree():
    def __init__(self):
        self.root = None

    def getRoot(self):
        return self.root
    def setRoot(self, v):
        self.root = v

    def inorder(self, v=None):
        if v is None:
            v = self.root
        if v.left is not None:
            self.inorder(v.left)
        print(' ' + str(v.key), end='')
        if v.right is not None:
            self.inorder(v.right)
        
    def preorder(self, v=None):
        if v is None:
            v = self.root
        print(' ' + str(v.key), end='')
        if v.left is not None:
            self.preorder(v.left)
        if v.right is not None:
            self.preorder(v.right)

def insert(T, z):
    y = None
    x = T.getRoot()
    while x is not None:
        y = x
        if z.key < x.key:
            x = x.left
        else:
            x = x.right
    z.p = y

    if y is None:
        T.setRoot(z)
    elif z.key < y.key:
        y.left = z
    else:
        y.right = z

def find(T, k, noprint=False):
    x = T.getRoot()
    while x is not None:
        if x.key == k:
            if not noprint:
                print('yes')
            return x
        elif x.key < k:
            x = x.right
        else:
            x = x.left
    print('no')

def getNext(v):
    if v.right is not None:
        return getMinimum(v.right)  
    y = v.p
    while y is not None and v == y.right:
        v = y
        y = y.p
    return y

def getMinimum(v):
    while v.left is not None:
        v = v.left
    return v


    
def delete(T, z):
    if not z.left and not z.right:
        p = z.p
        if z.key < p.key:
            p.left = None
        else:
            p.right = None

    elif z.left and not z.right:
        p = z.p
        c = z.left
        if z.key > p.key:
            p.right = c
        else:
            p.left = c
        c.p = p
    elif not z.left and z.right:
        p = z.p
        c = z.right
        if z.key > p.key:
            p.right = c
        else:
            p.left = c
        c.p = p
    else:
        n = getNext(z)
        z.key = n.key
        delete(T, n)

T = BinaryTree()

for i in range(m):
    inp = input().split()
    if inp[0] == 'print':
        T.inorder()
        print()
        T.preorder()
        print()
    elif inp[0] == 'find':
        find(T, int(inp[1]))
    elif inp[0] == 'delete':
        delete(T, find(T, int(inp[1]),True))
    else:
        v = Node(int(inp[1]), None, None)
        insert(T, v)

Submission status: Accepted

Solution code:
class Node():
    def __init__(self, key):
        self.key = key
        self.left = None
        self.right = None
        self.parent = None


class BinarySearchTree():
    def __init__(self):
        self.root = None

    def insert(self, key):
        root_node = self.root
        insert_node = Node(key)
        parent_node = None

        while root_node:
            parent_node = root_node
            if insert_node.key < root_node.key:
                root_node = root_node.left
            else:
                root_node = root_node.right

        insert_node.parent = parent_node

        if parent_node == None:
            self.root = insert_node
        elif insert_node.key < parent_node.key:
            parent_node.left = insert_node
        else:
            parent_node.right = insert_node

    def find(self, key):
        node = self.root

        while node:
            if node.key == key:
                return node
            elif node.key < key:
                node = node.right
            else:
                node = node.left

        return None

    def delete(self, key):
        node = self.find(key)

        if node == None:
            return

        if node.left and node.right:
            next_node = node.right

            while next_node.left:
                next_node = next_node.left

            node.key = next_node.key
            node = next_node

        if node.left:
            node.left.parent =  node.parent
            if node.parent.left == node:
                node.parent.left = node.left
            else:
                node.parent.right = node.left
        elif node.right:
            node.right.parent =  node.parent
            if node.parent.left == node:
                node.parent.left = node.right
            else:
                node.parent.right = node.right
        else:
            if node.parent.left == node:
                node.parent.left = None
            else:
                node.parent.right = None

    def show_preorder(self, node):
        node_list = []

        node_list.append(node.key)
        if node.left:
            node_list.extend(self.show_preorder(node.left))
        if node.right:
            node_list.extend(self.show_preorder(node.right))

        return node_list

    def show_inorder(self, node):
        node_list = []

        if node.left:
            node_list.extend(self.show_inorder(node.left))
        node_list.append(node.key)
        if node.right:
            node_list.extend(self.show_inorder(node.right))

        return node_list

    def print(self):
        print(' ' + ' '.join(map(str, self.show_inorder(self.root))))
        print(' ' + ' '.join(map(str, self.show_preorder(self.root))))


if __name__ == '__main__':
    tree = BinarySearchTree()

    n = int(input())

    for i in range(n):
        command = input().split()

        if command[0] == 'insert':
            tree.insert(int(command[1]))
        elif command[0] == 'find':
            print('yes' if tree.find(int(command[1])) else 'no')
        elif command[0] == 'delete':
            tree.delete(int(command[1]))
        elif command[0] == 'print':
            tree.print()

Submission status: Accepted

Solution code:
class BinaryTree():
    def __init__(self):
        self.root = None

    def insert(self, insert_node):
        p = None
        r = self.root
        while r:
            p = r
            if insert_node.value < r.value:
                r = r.left
            else:
                r = r.right

        if not p:
            self.root = insert_node
        elif insert_node.value < p.value:
            p.left = insert_node
        else:
            p.right = insert_node

    def find(self, find_value):
        r = self.root
        while r:
            if find_value == r.value:
                return "yes"
            if find_value < r.value:
                r = r.left
            else:
                r = r.right
        return "no"

    def delete(self, delete_value):
        flag = None
        p = None
        r = self.root
        while True:
            if delete_value == r.value:
                if r.left == None and r.right == None:
                    if flag == "l":
                        p.left = None
                    else:
                        p.right = None
                    return
                elif r.left == None:
                    if flag == "l":
                        p.left = r.right
                    else:
                        p.right = r.right
                    return
                elif r.right == None:
                    if flag == "l":
                        p.left = r.left
                    else:
                        p.right = r.left
                    return
                else:
                    r2 = r.right
                    while r2:
                        p = r2
                        r2 = r2.left
                    swap_value = p.value
                    self.delete(swap_value)
                    r.value = swap_value
                    return
            elif delete_value < r.value:
                p = r
                r = r.left
                flag = "l"
            else:
                p = r
                r = r.right
                flag = "r"

class Node():
    def __init__(self, value, left, right):
        self.value = value
        self.left  = left
        self.right = right

    def pre_order_search(self):
        l = self.left
        r = self.right
        print("",self.value, end="")
        if l:
            l.pre_order_search()
        if r:
            r.pre_order_search()

    def in_order_search(self):
        l = self.left
        r = self.right
        if l:
            l.in_order_search()
        print("",self.value, end="")
        if r:
            r.in_order_search()

m = int(input())
tree = BinaryTree()

for i in range(m):
    inp = input().split()
    if inp[0] == "insert":
        tree.insert(Node(int(inp[1]), None, None))
    elif inp[0] == "find":
        print(tree.find(int(inp[1])))
    elif inp[0] == "delete":
        tree.delete(int(inp[1]))
    else:
        tree.root.in_order_search()
        print()
        tree.root.pre_order_search()
        print()

Submission status: Accepted

Solution code:
class BinaryTree():
    def __init__(self):
        self.root = None

    def insert(self, insert_node):
        p = None
        r = self.root
        while r:
            p = r
            if insert_node.value < r.value:
                r = r.left
            else:
                r = r.right

        if not p:
            self.root = insert_node
        elif insert_node.value < p.value:
            p.left = insert_node
        else:
            p.right = insert_node

    def find(self, find_value):
        r = self.root
        while r:
            if find_value == r.value:
                return "yes"
            if find_value < r.value:
                r = r.left
            else:
                r = r.right
        return "no"

    def delete(self, delete_value):
        flag = None
        p = None
        r = self.root
        while True:
            if delete_value == r.value:
                if r.left == None and r.right == None:
                    if flag == "l":
                        p.left = None
                    else:
                        p.right = None
                    return
                elif r.left == None:
                    if flag == "l":
                        p.left = r.right
                    else:
                        p.right = r.right
                    return
                elif r.right == None:
                    if flag == "l":
                        p.left = r.left
                    else:
                        p.right = r.left
                    return
                else:
                    min_node = r.right
                    while min_node:
                        p = min_node
                        min_node = min_node.left
                    swap_value = p.value
                    self.delete(swap_value)
                    r.value = swap_value
                    return
            elif delete_value < r.value:
                p = r
                r = r.left
                flag = "l"
            else:
                p = r
                r = r.right
                flag = "r"

class Node():
    def __init__(self, value, left, right):
        self.value = value
        self.left  = left
        self.right = right

    def pre_order_search(self):
        l = self.left
        r = self.right
        print("",self.value, end="")
        if l:
            l.pre_order_search()
        if r:
            r.pre_order_search()

    def in_order_search(self):
        l = self.left
        r = self.right
        if l:
            l.in_order_search()
        print("",self.value, end="")
        if r:
            r.in_order_search()

m = int(input())
tree = BinaryTree()

for i in range(m):
    inp = input().split()
    if inp[0] == "insert":
        tree.insert(Node(int(inp[1]), None, None))
    elif inp[0] == "find":
        print(tree.find(int(inp[1])))
    elif inp[0] == "delete":
        tree.delete(int(inp[1]))
    else:
        tree.root.in_order_search()
        print()
        tree.root.pre_order_search()
        print()

Submission status: Accepted

Solution code:
# -*- coding:utf-8 -*-
import sys


class Node(object):
    __slots__ = ["value", "left", "right"]

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


class BinTree(object):
    def __init__(self):
        self._tree = None

    def insert(self, value):
        parent = None
        current = self._tree

        while current is not None:
            parent = current
            if value < current.value:
                current = current.left
            else:
                current = current.right

        if parent is None:
            self._tree = Node(value)
        elif value < parent.value:
            parent.left = Node(value)
        else:
            parent.right = Node(value)

    def find(self, value):
        current = self._tree

        while current is not None:
            if value == current.value:
                return True
            elif value < current.value:
                current = current.left
            else:
                current = current.right

        return False

    def delete(self, value):
        parent = None
        current = self._tree

        while current.value != value:
            parent = current
            if current is None:
                return
            elif value < current.value:
                current = current.left
            else:
                current = current.right

        if current.left is None and current.right is None:
            if parent.left is current:
                parent.left = None
            else:
                parent.right = None
        elif current.left is None:
            if parent.left is current:
                parent.left = current.right
            else:
                parent.right = current.right
        elif current.right is None:
            if parent.left is current:
                parent.left = current.left
            else:
                parent.right = current.left
        else:
            next_node_parent = current
            next_node = current.right
            while next_node.left is not None:
                next_node_parent = next_node
                next_node = next_node.left

            if next_node.right is None:
                if next_node_parent.left is next_node:
                    next_node_parent.left = None
                else:
                    next_node_parent.right = None
            else:
                if next_node_parent.left is next_node:
                    next_node_parent.left = next_node.right
                else:
                    next_node_parent.right = next_node.right

            current.value = next_node.value

    def preoder_walk(self):
        def preoder(node):
            result = []
            if node is None:
                pass
            else:
                result.append(node.value)
                result.extend(preoder(node.left))
                result.extend(preoder(node.right))
            return result

        return preoder(self._tree)

    def inorder_walk(self):
        def inorder(node):
            result = []
            if node is None:
                pass
            else:
                result.extend(inorder(node.left))
                result.append(node.value)
                result.extend(inorder(node.right))
            return result

        return inorder(self._tree)


def print_tree(tree):
    print(" " + " ".join([str(val) for val in tree.inorder_walk()]))
    print(" " + " ".join([str(val) for val in tree.preoder_walk()]))


def print_yes_no(boolean):
    if boolean:
        print("yes")
    else:
        print("no")


def main(commands):
    tree = BinTree()
    for com in commands:
        if com[0] == "insert":
            tree.insert(int(com[1]))
        elif com[0] == "print":
            print_tree(tree)
        elif com[0] == "find":
            print_yes_no(tree.find(int(com[1])))
        elif com[0] == "delete":
            tree.delete(int(com[1]))
        else:
            raise ValueError


if __name__ == "__main__":
    n = int(input())
    commands = [com.split() for com in sys.stdin.readlines()]
    main(commands)

Submission status: Accepted

Solution code:


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()