Question
Answered step-by-step
i need help fixing my remove_element method so that it removes...
i need help fixing my remove_element method so that it removes elements recursively and my get_height method so that the
...
Question
Answered step-by-step
i need help fixing my remove_element method so that it removes...
i need help fixing my remove_element method so that it removes elements recursively and my get_height method so that the height kept track.
class Search_Tree:
class __BST_Node:
def __init__(self, value):
# TODO complete Node initialization
self.value = value
self.left = None
self.right = None
self.height= 1
def insert_element(self, data):
if self.value == data.value:
return False
elif self.value > data.value:
if self.left:
return self.left.insert_element(data)
else:
self.left = data
return True
else:
if self.right:
return self.right.insert_element(data)
else:
self.right = data
return True
def search(self, data):
if(self.value == data):
return True
elif self.value > data:
if self.left:
return self.left.search(data)
else:
return False
else:
if self.right:
return self.right.search(data)
else:
return False
def pre_order(self):
if self:
print (str(self.value)) ##
if self.left:
self.left.pre_order()
if self.right:
self.right.pre_order()
def post_order(self):
if self:
if self.left:
self.left.post_order()
if self.right:
self.right.post_order()
print (str(self.value))
def in_order(self):
if self:
if self.left:
self.left.in_order()
print (str(self.value))
if self.right:
self.right.in_order()
def get_height(self):
if self.left and self.right:
return 1 + max(self.left.get_height(), self.right.get_height())
elif self.left:
return 1 + self.left.get_height()
elif self.right:
return 1 + self.right.get_height()
else:
return 1
def remove_element(self, data): ##fix
if(self.value == data):
return True
elif self.value > data:
if self.left:
return self.left.search(data)
else:
return False
else:
if self.right:
return self.right.search(data)
else:
return False
def __init__(self):
self.__root = None
def insert_element(self, value):
node = self.__BST_Node(value)
if self.__root:
return self.__root.insert_element(node)
else:
self.__root = node
return True
def search(self, data):
if self.__root:
return self.__root.search(data)
else:
return False
def remove_element(self, value):
# Remove the value specified from the tree, raising a ValueError
# if the value isn't found. When a replacement value is necessary,
# select the minimum value to the from the right as this element's
# replacement. Take note of when to move a node reference and when
# to replace the value in a node instead. It is not necessary to
# return the value (though it would reasonable to do so in some
# implementations). Your solution must be recursive.
# This will involve the introduction of additional private
# methods to support the recursion control variable.
node = self.__BST_Node(value)
if self.__root:
return self.__root.remove_element(node)
else:
self.__root = node
return True
def in_order(self):
print("InOrder")
self.__root.in_order
def pre_order(self):
print("Pre-Order")
self.__root.pre_order
def post_order(self):
print("Post-Order")
self.__root.post_order
def get_height(self):
if self.__root:
return self.__root.get_height()
else:
return 0
def __str__(self):
return self.in_order()
if __name__ == '__main__':
bst = Search_Tree()
print(bst.insert_element(7))
print(bst.insert_element(3))
bst.insert_element(5)
bst.insert_element(9)
bst.pre_order()
print('Height = ', bst.get_height())
bst.post_order()
print(bst.remove_element(10))
bst.in_order()
bst.pre_order()
[Show More]