class Node:
'''this is the constructor'''
def __init__(self, data):
self.left = None
self.right = None
self.data = data
def insert(self, data):
''' this function work is insert the data in bst'''
if self.data:
'''insert left side value '''
if data < self.data:
if not self.left:
self.left = Node(data)
else:
self.left.insert(data)
'''insert right side value'''
elif data > self.data :
if not self.right :
self.right = Node(data)
else:
self.right.insert(data)
else:
self.data = data
def getMinValue(self,node):
'''this fuction work is get minimum value'''
current = node
while (current.left is not None):
current = current.left
return current
def delValue(self,data):
'''this fuction work delete value'''
if data < self.data:
self.left = self.left.delValue(data)
elif data > self.data:
self.right = self.right.delValue(data)
else:
if self.left is None:
temp = self.right
self = None
return temp
elif self.right is None:
temp = self.left
self = None
return temp
temp = self.getMinValue(self.right)
self.data = temp.data
self.right = self.right.delValue(temp.data)
return self
'''this function work finde a value in bst or return True ordef False'''getSearchValue(self,data):
if data == self.data:
return print(Trueself.data,"True")
if data < self.data:
if self.left:
self.left.getSearchValue(data)
if data > self.data:
if self.right:
self.right.getSearchValue(data)
return print(False)
'''this function work print adef tree'''print_tree(self):
if self.left:
self.left.printTreeprint_tree()
print(self.data)
if self.right:
self.right.printTreeprint_tree()