class BinaryTree():
def __init__(self, x):
self.data = x
self.left = None
self.right = None
def CreateNode(x):
Node = BinaryTree(x)
return Node
def find(Node, x):
if (not Node):
return None
else:
if (x > Node.data):
return find(Node.right, x)
elif (x < Node.data):
return find(Node.left, x)
else:
return Node
def find1(Node, x):
if (not Node): return None
while (Node):
if (x > Node.data):
Node = Node.right
elif (x < Node.data):
Node = Node.left
else:
return Node
return None
def Insert(Node, x):
if (not Node):
Node = CreateNode(x)
elif (x > Node.data):
Node.right = Insert(Node.right, x)
else:
Node.left = Insert(Node.left, x)
return Node
def findmin(Node):
if (Node):
while (Node.left):
Node = Node.left
return Node
def findmax(Node):
if (Node):
while (Node.right):
Node = Node.right
return Node
def findmin1(Node):
if (Node):
if (Node.left):
return findmin1(Node.left)
else:
return Node
def Delete(Node, x):
if (not Node):
return
else:
if (x < Node.data):
Node.left = Delete(Node.left, x)
elif (x > Node.data):
Node.right = Delete(Node.right, x)
else:
if (not Node.right and not Node.left):
temp = findmin(Node.right)
Node.data = temp.data
Node.right = Delete(Node.right, temp.data)
elif (not Node.right):
Node = Node.left
elif (not Node.left):
Node = Node.right
return Node
def TreeHeight(Node):
if Node:
HL = TreeHeight(Node.left)
HR = TreeHeight(Node.right)
maxH = max(HL, HR)
return maxH + 1
else:
return 0
def PreOrderTraval(Node):
if (not Node):
return
else:
print(Node.data)
PreOrderTraval(Node.left)
PreOrderTraval(Node.right)
def Compare(Node1, Node2):
if (not Node1 and not Node2):
return True
elif (not Node1 and Node2):
return False
elif (not Node2 and Node1):
return False
elif (Node1.data == Node2.data):
return Compare(Node1.left, Node2.left) and Compare(Node1.right, Node2.right)
else:
return False
aList = [i for i in range(10)]
if __name__ == '__main__':
Node = CreateNode(aList[0])
for i in aList[1:]:
Node = Insert(Node, i)
Node1 = CreateNode(aList[0])
for i in aList[1:]:
Node = Insert(Node1, i)
print(Compare(Node, Node1))
print(findmin1(Node).data)