Binary Search Tree
Binary Search Tree
class BST:
def __init__(self):
self.root = None
def inorder_traversal(self):
result = []
self._inorder_traversal_recursive(self.root, result)
return result
def main():
bst = BST()
print("Binary Search Tree Operations:")
print("1. Insert")
print("2. Search")
print("3. Inorder Traversal")
print("4. Exit")
while True:
choice = input("Enter your choice: ")
if choice == '1':
key = int(input("Enter the value to insert: "))
bst.insert(key)
print(f"{key} inserted into the tree.")
elif choice == '2':
key = int(input("Enter the value to search: "))
result = bst.search(key)
if result:
print(f"{key} found in the tree.")
else:
print(f"{key} not found in the tree.")
elif choice == '3':
print("Inorder Traversal:", bst.inorder_traversal())
elif choice == '4':
print("Exiting...")
break
else:
print("Invalid choice. Please enter a valid option.")
if __name__ == "__main__":
main()