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: Wrong Answer

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

    def insert(self, key):
        z = Node(key)
        y = None  # x?????????????´????????????°
        # z???parent???????´?
        x = self.root
        while x:
            y = x
            if z.key < x.key:
                x = x.left
            else:
                x = x.right
        z.parent = y
        # z??????????????´??????????´?
        if y is None:
            self.root = z
        elif z.key < y.key:
            y.left = z
        else:
            y.right = z

    def find(self, key):
        x = self.root
        while x and key != x.key:
            if key < x.key:
                x = x.left
            else:
                x = x.right
        return x

    def getSuccessor(self, x):
        if x.right is not None:
            return self.getMinimum(x.right)

        y = x.parent
        while y and x == y.right:
            x = y
            y = y.parent
        return y

    def getMinimum(self, x):
        while x.left is not None:
            x = x.left
        return x

    def delete(self, z):
        #??????????±????y???????´?
        if z.left is None or z.right is None:
            y = z
        else:
            y = getSuccessor(z)

        # y??????x????????????
        if y.left is not None:
            x = y.left
        else:
            x = y.right

        if x is not None:
            x.parent = y.parent

        if y.parent is None:
            self.root = x
        elif y == y.parent.left:
            y.parent.left = x
        else:
            y.parent.right = x

        if y != z:
            z.key = y.key


    def show(self):
        print(" ", end="")
        print(*list(map(str, self.root.inwalk())))
        print(" ", end="")
        print(*list(map(str, self.root.prewalk())))


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

    def prewalk(self):
        nodeList = [self.key]
        if self.left:
            nodeList += self.left.prewalk()
        if self.right:
            nodeList += self.right.prewalk()
        return nodeList

    def inwalk(self):
        nodeList = []
        if self.left:
            nodeList += self.left.inwalk()
        nodeList += [self.key]
        if self.right:
            nodeList += self.right.inwalk()
        return nodeList


tree = Tree()
n = int(input())
for i in range(n):
    cmd = list(input().split())
    if cmd[0] == 'insert':
        tree.insert(int(cmd[1]))
    elif cmd[0] == 'find':
        if tree.find(int(cmd[1])):
            print("yes")
        else:
            print("no")
    elif cmd[0] == 'print':
        tree.show()

Submission status: Wrong Answer

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

    def insert(self, key):
        z = Node(key)
        y = None  # x?????????????´????????????°
        # z???parent???????´?
        x = self.root
        while x:
            y = x
            if z.key < x.key:
                x = x.left
            else:
                x = x.right
        z.parent = y
        # z??????????????´??????????´?
        if y is None:
            self.root = z
        elif z.key < y.key:
            y.left = z
        else:
            y.right = z

    def find(self, key):
        x = self.root
        while x and key != x.key:
            if key < x.key:
                x = x.left
            else:
                x = x.right
        return x

    def getSuccessor(self, x):
        if x.right is not None:
            return self.getMinimum(x.right)

        y = x.parent
        while y and x == y.right:
            x = y
            y = y.parent
        return y

    def getMinimum(self, x):
        while x.left is not None:
            x = x.left
        return x

    def delete(self, key):
        #??????????±????y???????´?
        z = self.find(key)
        if z.left is None or z.right is None:
            y = z
        else:
            y = self.getSuccessor(z)

        # y??????x????????????
        if y.left is not None:
            x = y.left
        else:
            x = y.right

        if x is not None:
            x.parent = y.parent

        if y.parent is None:
            self.root = x
        elif y == y.parent.left:
            y.parent.left = x
        else:
            y.parent.right = x

        if y != z:
            z.key = y.key


    def show(self):
        print(" ", end="")
        print(*list(map(str, self.root.inwalk())))
        print(" ", end="")
        print(*list(map(str, self.root.prewalk())))


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

    def prewalk(self):
        nodeList = [self.key]
        if self.left:
            nodeList += self.left.prewalk()
        if self.right:
            nodeList += self.right.prewalk()
        return nodeList

    def inwalk(self):
        nodeList = []
        if self.left:
            nodeList += self.left.inwalk()
        nodeList += [self.key]
        if self.right:
            nodeList += self.right.inwalk()
        return nodeList


tree = Tree()
n = int(input())
for i in range(n):
    cmd = list(input().split())
    if cmd[0] == 'insert':
        tree.insert(int(cmd[1]))
    elif cmd[0] == 'find':
        if tree.find(int(cmd[1])):
            print("yes")
        else:
            print("no")
    elif cmd[0] == 'print':
        tree.show()

Submission status: Wrong Answer

Solution code:
#BinarySearchTree
class TreeNode():
    def __init__(self):
        self.key = None
        self.parent = None
        self.left = None
        self.right = None
    def inoder(self):
        r=[]
        if self.left:
            r += self.left.inoder()
        r += [self.key]
        if self.right:
            r += self.right.inoder()
        return r

    def preoder(self):
        r = [self.key]
        if self.left:
            r += self.left.preoder()
        if self.right:
            r += self.right.preoder()
        return r

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

    def insert(self, z):
        y = None
        x = self.root
        while x != None:
            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 show(self):
        x = self.root
        print(" " + " ".join(map(str, self.root.inoder())))
        print(" " + " ".join(map(str, self.root.preoder())))

    def find(self, key):
        currentNode = self.root
        while currentNode:
            if key == currentNode.key:
                return currentNode
            else:
                if key < currentNode.key:
                    currentNode = currentNode.left
                else:
                    currentNode = currentNode.right
        return False
    def next(self, key):
        return self.min(key.right)
    def min (self, key):
        while key.left is not None:
           key = key.left
        return key
    
    def delete(self, key):
        currentNode = self.find(key)
        if not currentNode: return
        
        if (currentNode.left is not None) and (currentNode.right is not None):
            z = self.next(currentNode)
        else:
            z = currentNode
        
        if z.left != None:
            z.left.parent = z.parent
            if z.parent.key > z.left.key:
                z.parent.left = z.left
            else:
                z.parent.right = z.left
        if z.right != None:
            z.right.parent = z.parent
            if z.parent.key > z.right.key:
                z.parent.left = z.right
            else:
                z.parent.right = z.right
        if z.right is None and z.left is None:
            if z.parent.key > z.key:
                z.parent.left = None
            else:
                z.parent.right = None
        if z != currentNode:
            currentNode = z 

n = int(input())
Tree = BinaryTree()
for i in range(n):
    L = input().split()
    if L[0] == "insert":
        Node = TreeNode()
        Node.key = int(L[1])
        Tree.insert(Node)
    elif L[0] =="print":
        Tree.show()
    elif L[0] =="find":
        if Tree.find(int(L[1])) == False:
            print("no")
        else:
            print("yes")
    else:
        Tree.delete(int(L[1]))

Submission status: Wrong Answer

Solution code:
#BinarySearchTree
class TreeNode():
    def __init__(self):
        self.key = None
        self.parent = None
        self.left = None
        self.right = None
    def inoder(self):
        r=[]
        if self.left:
            r += self.left.inoder()
        r += [self.key]
        if self.right:
            r += self.right.inoder()
        return r

    def preoder(self):
        r = [self.key]
        if self.left:
            r += self.left.preoder()
        if self.right:
            r += self.right.preoder()
        return r

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

    def insert(self, z):
        y = None
        x = self.root
        while x != None:
            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 show(self):
        x = self.root
        print(" " + " ".join(map(str, self.root.inoder())))
        print(" " + " ".join(map(str, self.root.preoder())))

    def find(self, key):
        currentNode = self.root
        while currentNode:
            if key == currentNode.key:
                return currentNode
            else:
                if key < currentNode.key:
                    currentNode = currentNode.left
                else:
                    currentNode = currentNode.right
        return False
    def next(self, key):
        return self.min(key.right)
    def min (self, key):
        while key.left is not None:
           key = key.left
        return key
    
    def delete(self, key):
        currentNode = self.find(key)
        if currentNode == False: return
        if (currentNode.left is not None) and (currentNode.right is not None):
            z = self.next(currentNode)
        else:
            z = currentNode
            if z == self.root:
                self.root = None
        
        if z.left != None:
            z.left.parent = z.parent
            if z.parent.key > z.left.key:
                z.parent.left = z.left
            else:
                z.parent.right = z.left
        if z.right != None:
            z.right.parent = z.parent
            if z.parent.key > z.right.key:
                z.parent.left = z.right
            else:
                z.parent.right = z.right
        if z.right is None and z.left is None:
            if z.parent.key > z.key:
                z.parent.left = None
            else:
                z.parent.right = None
        if z != currentNode:
            currentNode = z 
        if z == self.root:
            self.root = z.left

n = int(input())
Tree = BinaryTree()
for i in range(n):
    L = input().split()
    if L[0] == "insert":
        Node = TreeNode()
        Node.key = int(L[1])
        Tree.insert(Node)
    elif L[0] =="print":
        Tree.show()
    elif L[0] =="find":
        if Tree.find(int(L[1])) == False:
            print("no")
        else:
            print("yes")
    else:
        Tree.delete(int(L[1]))

Submission status: Wrong Answer

Solution code:
import sys

class Node:
    def __init__(self, x):
        self.data = x
        self.left = None
        self.right = None
    
    def insert(node, x):
        if node == None:
            return Node(x)
        elif x == node.data:
            return node
        elif x < node.data:
            node.left = Node.insert(node.left, x)
        else:
            node.right = Node.insert(node.right, x)
        return node
    
    def find(node, x):
        if node.data == x:
            return True
        elif node.left != None and node.data > x:
            return Node.find(node.left, x)
        elif node.right != None and node.data < x:
            return Node.find(node.right, x)
        else:
            return False
        
    def delete(node, x):
        if node.data == x:
            if node.left == None and node.right == None:
                return None
            elif node.left == None:
                return node.right
            elif node.right == None:
                return node.left
            else:
                #????????????
                node.data = Node.serch_min(node.right)
                node.right = Node.delete_min(node.right)
                return node
        elif node.data > x:
            node.left =  Node.delete(node.left, x)
        else:
            node.right =  Node.delete(node.right, x)
        return node
        
    def serch_min(node):
        if node.left != None:
            return Node.serch_min(node.left)
        else:
            return node.data
    
    def delete_min(node):
        if node.left == None:
            return node.right
        else:
            return Node.delete_min(node.left)
                
        
    def inorder_print(node):
        if node.left != None:
            Node.inorder_print(node.left)
        print("",node.data,  end="")
        if node.right != None:
            Node.inorder_print(node.right)
    def preorder_print(node):
        print("",node.data,  end="")
        if node.left != None:
            Node.preorder_print(node.left)
        if node.right != None:
            Node.preorder_print(node.right)
        
class BinaryTree:
    def __init__(self):
        self.root = None
    
    def insert(self, x):
        self.root = Node.insert(self.root, x)

    def find(self, x):
        if Node.find(self.root, x):
            print("yes")
        else:
            print("no")
            
    def delete(self, x):
        self.root = Node.delete(self.root, x)
        
    def print_tree(self):
        Node.inorder_print(self.root)
        print()
        Node.preorder_print(self.root)
        print()

def main():
    T = BinaryTree()
    m = int(sys.stdin.readline())
    for i in range(m):
        s = sys.stdin.readline().strip()
        if(s == "print"):
            T.print_tree()
        elif s.split()[0] == "find":
            T.find(int(s.split()[1]))
        elif s.split()[0] == "delete":
            T.delete(int(s.split()[1]))
        else:
            z = int(s.split()[1])
            T.insert(z)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys

class Node:
    def __init__(self, x):
        self.data = x
        self.left = None
        self.right = None
    
    def insert(node, x):
        if node == None:
            return Node(x)
        elif x == node.data:
            return node
        elif x < node.data:
            node.left = Node.insert(node.left, x)
        else:
            node.right = Node.insert(node.right, x)
        return node
    
    def find(node, x):
        if node.data == x:
            return True
        elif node.left != None and node.data > x:
            return Node.find(node.left, x)
        elif node.right != None and node.data < x:
            return Node.find(node.right, x)
        else:
            return False
        
    def delete(node, x):
        if node.data == x:
            if node.left == None and node.right == None:
                return None
            elif node.left == None:
                return node.right
            elif node.right == None:
                return node.left
            else:
                #????????????
                node.data = Node.serch_min(node.right)
                node.right = Node.delete_min(node.right)
                return node
        elif node.data > x:
            node.left =  Node.delete(node.left, x)
        else:
            node.right =  Node.delete(node.right, x)
        return node
        
    def serch_min(node):
        if node.left != None:
            return Node.serch_min(node.left)
        else:
            return node.data
    
    def delete_min(node):
        if node.left == None:
            return node.right
        else:
            node.left = Node.delete_min(node.left)
                
        
    def inorder_print(node):
        if node.left != None:
            Node.inorder_print(node.left)
        print("",node.data,  end="")
        if node.right != None:
            Node.inorder_print(node.right)
    def preorder_print(node):
        print("",node.data,  end="")
        if node.left != None:
            Node.preorder_print(node.left)
        if node.right != None:
            Node.preorder_print(node.right)
        
class BinaryTree:
    def __init__(self):
        self.root = None
    
    def insert(self, x):
        self.root = Node.insert(self.root, x)

    def find(self, x):
        if Node.find(self.root, x):
            print("yes")
        else:
            print("no")
            
    def delete(self, x):
        self.root = Node.delete(self.root, x)
        
    def print_tree(self):
        Node.inorder_print(self.root)
        print()
        Node.preorder_print(self.root)
        print()

def main():
    T = BinaryTree()
    m = int(sys.stdin.readline())
    for i in range(m):
        s = sys.stdin.readline().strip()
        if(s == "print"):
            T.print_tree()
        elif s.split()[0] == "find":
            T.find(int(s.split()[1]))
        elif s.split()[0] == "delete":
            T.delete(int(s.split()[1]))
        else:
            z = int(s.split()[1])
            T.insert(z)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
n_order = int(raw_input())


class node:

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


def insert(num, c_node):
    if num >= c_node.num:
        if c_node.right == None:
            # print num, c_node.num, "right"
            c_node.right = node(num, c_node)
            c_node.right.parent = c_node
        elif c_node.right != None:
            insert(num, c_node.right)
    elif num < c_node.num:
        if c_node.left == None:
            c_node.left = node(num, c_node)
            c_node.left.parent = c_node
            # print num, c_node.num, "left"
        elif c_node.left != None:
            insert(num, c_node.left)


def c_print(c_node):
    sys.stdout.write(str(c_node.num) + " ")
    if c_node.left != None:
        c_print(c_node.left)
    if c_node.right != None:
        c_print(c_node.right)


def p_print(c_node):
    if c_node.left != None:
        p_print(c_node.left)
    sys.stdout.write(str(c_node.num) + " ")
    if c_node.right != None:
        p_print(c_node.right)


def find(num, c_node):
    if num == c_node.num:
        print "yes"
    elif num > c_node.num:
        if c_node.right == None:
            print "no"
        elif c_node.right != None:
            find(num, c_node.right)
    elif num < c_node.num:
        if c_node.left == None:
            print "no"
        elif c_node.left != None:
            find(num, c_node.left)


def find_return(num, c_node):
    if num == c_node.num:
        return c_node
    elif num > c_node.num:
        if c_node.right == None:
            return None
        elif c_node.right != None:
            return find_return(num, c_node.right)
    elif num < c_node.num:
        if c_node.left == None:
            return None
        elif c_node.left != None:
            return find_return(num, c_node.left)


def find_midorder_return(c_node):
    if c_node.left == None:
        return c_node
    else:
        return find_midorder_return(c_node.left)


def delete(num, c_node):
    del_node = find_return(num, c_node)
    if del_node.left == None and del_node.right == None:
        if del_node.parent == None:
            c_node = None
        elif del_node.parent.num > del_node.num:
            del_node.parent.left = None
        elif del_node.parent.num < del_node.num:
            del_node.parent.right = None
    elif del_node.left == None:
        if del_node.parent == None:
            c_node = del_node.right
        elif del_node.parent.num > del_node.num:
            del_node.parent.left = del_node.right
            del_node.right.parent = del_node.parent
        elif del_node.parent.num < del_node.num:
            del_node.parent.right = del_node.right
            del_node.right.parent = del_node.parent
    elif del_node.right == None:
        if del_node.parent == None:
            c_node = del_node.left
        elif del_node.parent.num > del_node.num:
            del_node.parent.left = del_node.left
            del_node.left.parent = del_node.parent
        elif del_node.parent.num < del_node.num:
            del_node.parent.right = del_node.left
            del_node.left.parent = del_node.parent
    else:
        deep_left_node = find_midorder_return(del_node)
        if deep_left_node.right != None:
            if del_node.parent == None:
                pass
            elif deep_left_node.parent.num > deep_left_node.num:
                deep_left_node.parent.left = deep_left_node.right
                deep_left_node.right.parent = deep_left_node.parent
            elif del_node.parent.num < deep_left_node.num:
                deep_left_node.parent.right = deep_left_node.right
                deep_left_node.right.parent = deep_left_node.parent
        deep_left_node.parent.left = None
        del_node.num = deep_left_node.num


root = node(None, None)
read_count = 0
while True:
    read_count += 1
    order = raw_input()
    splited_order = order.split(" ")
    if splited_order[0] == "insert":
        root.num = int(splited_order[1])
        break
print "root:", root.num

for i in range(n_order - read_count):
    order = raw_input()
    splited_order = order.split(" ")
    if splited_order[0] == "insert":
        insert(int(splited_order[1]), root)
    elif splited_order[0] == "find":
        find(int(splited_order[1]), root)
    elif splited_order[0] == "delete":
        delete(int(splited_order[1]), root)
    elif splited_order[0] == "print":
        sys.stdout.write(" ")
        p_print(root)
        print ""
        sys.stdout.write(" ")
        c_print(root)
        print ""

Submission status: Wrong Answer

Solution code:
import sys


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

    def preorder(self):
        nodeList = [self.key]
        if self.left:
            nodeList += self.left.preorder()
        if self.right:
            nodeList += self.right.preorder()
        return nodeList

    def inorder(self):
        nodeList = []
        if self.left:
            nodeList += self.left.inorder()
        nodeList += [self.key]
        if self.right:
            nodeList += self.right.inorder()
        return nodeList


class BinaryTree():
    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 is None:
            self.root = z
        elif z.key < y.key:
            y.left = z
        else:
            y.right = z

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

    def delete(self, key):
        z = self.find(key)
        if z is not None:
            if z.left is not None and z.right is not None:
                temp = z.left.key
                self.delete(temp)
                z.key = temp
            elif z.left is not None:
                if z.parent is None:
                    self.root = z.left
                    z.left.parent = None
                else:
                    if z.parent.left is z:
                        z.parent.left = z.left
                    else:
                        z.parent.right = z.left
                    z.left.parent = z.parent
            elif z.right is not None:
                if z.parent is None:
                    self.root = z.right
                    z.right.parent = None
                else:
                    if z.parent.left is z:
                        z.parent.left = z.right
                    else:
                        z.parent.right = z.right
                    z.right.parent = z.parent
            else:
                if z.parent is None:
                    self.root = None
                else:
                    if z.parent.left is z:
                        z.parent.left = None
                    else:
                        z.parent.right = None

    def print(self):
        print(" " + " ".join(map(str, self.root.inorder())))
        print(" " + " ".join(map(str, self.root.preorder())))


if __name__ == "__main__":
    tree = BinaryTree()
    n = int(sys.stdin.readline())
    for inp in sys.stdin.readlines():
        inp = inp.split()
        if inp[0] == "insert":
            tree.insert(int(inp[1]))
        elif inp[0] == "find":
            if tree.find(int(inp[1])) is None:
                print("no")
            else:
                print("yes")
        elif inp[0] == "delete":
            tree.delete(int(inp[1]))
        else:
            tree.print()

Submission status: Wrong Answer

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
            while y.left is not None:
                y = y.left

            x.key = y.key
            if y.right is not None:
                y.right.parent = y.parent
                y.parent.left  = y.right
            else:
                y.parent.left = 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()