Toong CPP104 Lab4 Finals
Toong CPP104 Lab4 Finals
University of Cabuyao
Pamantasan ng Cabuyao
College of Engineering
Katapatan Mutual Homes, Brgy. Banay-banay, City of Cabuyao, Laguna, Phillippines 4025
SUBMITTED BY:
SUBMITTED TO:
Engr. Cherry Casuat
INSTRUCTOR
DATE OF SUBMISSION:
January 5, 2024
Republic of the Philippines
University of Cabuyao
Pamantasan ng Cabuyao
College of Engineering
Katapatan Mutual Homes, Brgy. Banay-banay, City of Cabuyao, Laguna, Phillippines 4025
MACHINE PROBLEM
CODE:
class BSTNode:
def __init__(self, key):
self.key = key
self.left = None
self.right = None
self.parent = None
def inorder(self):
result = []
if self.left:
result.extend(self.left.inorder())
result.append(self.key)
if self.right:
result.extend(self.right.inorder())
return result
def find_min(self):
current = self
Republic of the Philippines
University of Cabuyao
Pamantasan ng Cabuyao
College of Engineering
Katapatan Mutual Homes, Brgy. Banay-banay, City of Cabuyao, Laguna, Phillippines 4025
while current.left:
current = current.left
return current
def remove(self):
if self.left and self.right:
successor = self.right.find_min()
self.key = successor.key
successor.remove()
elif self.left or self.right:
child = self.left or self.right
self.replace_node_of_parent(child)
else:
self.replace_node_of_parent(None)
class BSTree:
def __init__(self):
self.root = None
def inorder(self):
if self.root:
return self.root.inorder()
return []
def print_menu():
print("****************BINARY SEARCH TREE****************")
print("* Programmed by: Toong, Mark John R. *")
print("* *")
print("* < < < MAIN MENU > > > *")
print("* *")
print("* ADD <KEYS> *")
print("* REMOVE <KEYS> *")
print("* INORDER TRAVERSAL *")
print("* EXIT PROGRAM *")
print("* *")
print("**************************************************")
def main():
bstree = BSTree()
menu_shown = False
while True:
if not menu_shown:
print_menu()
menu_shown = True
if __name__ == "__main__":
main()
Republic of the Philippines
University of Cabuyao
Pamantasan ng Cabuyao
College of Engineering
Katapatan Mutual Homes, Brgy. Banay-banay, City of Cabuyao, Laguna, Phillippines 4025
OUTPUT:
Republic of the Philippines
University of Cabuyao
Pamantasan ng Cabuyao
College of Engineering
Katapatan Mutual Homes, Brgy. Banay-banay, City of Cabuyao, Laguna, Phillippines 4025
Write a Python program which allows the user to input the in-order and post-order
traversal of the same binary tree, then the program will print the corresponding:
CODE:
class TreeNode:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
root_value = postorder.pop()
root = TreeNode(root_value)
inorder_index = inorder.index(root_value)
return root
def inorder_traversal(root):
result = []
if root:
result += inorder_traversal(root.left)
result.append(root.value)
result += inorder_traversal(root.right)
return result
def postorder_traversal(root):
result = []
if root:
result += postorder_traversal(root.left)
result += postorder_traversal(root.right)
result.append(root.value)
return result
def preorder_traversal(root):
result = []
if root:
result.append(root.value)
result += preorder_traversal(root.left)
result += preorder_traversal(root.right)
return result
Republic of the Philippines
University of Cabuyao
Pamantasan ng Cabuyao
College of Engineering
Katapatan Mutual Homes, Brgy. Banay-banay, City of Cabuyao, Laguna, Phillippines 4025
OUTPUT: