def search(cls): """Find item in tree""" print ("Enter integer value to search: ") key_value = Menu.get_int() print("\n", end='') node = BST.tree.search(key_value, verbose=BST.verbose) print ("Item ", key_value, " is ", "" if node is not None else "not ", "in the tree", sep='')
def insert(cls): """Add a new item to the tree""" print("Enter new integer value: ") key_value = Menu.get_int() print('\n', end='') BST.tree.insert(key_value, verbose=BST.verbose) print ("Item", key_value, "was added to the tree")
def add(cls): """Add a new item to the max heap""" print ("Enter new integer value: ") key_value = Menu.get_int() print ('\n', end='') MaxHeap.heap.add_item(key_value, verbose=MaxHeap.verbose) print ("Item", key_value, "was added to the heap.")
def delete(cls): """Remove an item from the tree, if there""" print("Enter the integer value to remove: ") key_value = Menu.get_int() print('\n', end='') removed_node = BST.tree.delete(key_value, verbose=BST.verbose) if removed_node is not None: print (key_value, "was removed from the tree.") else: print (key_value, "was not found in the tree.")
def right(cls): """Rotate the tree right""" print("Enter the key at which to rotate right: ") key_value = Menu.get_int() print('\n', end='') node = BST.tree.search(key_value, verbose=BST.verbose) if isinstance(node, BinarySearchNode): new_root = node.rotate_right(verbose=BST.verbose) if new_root is not None: print("The tree was rotated right at", key_value) else: print("Unable to rotate the tree right at", key_value) else: print(key_value, "was not found in the tree")
def get_menu(cls, parent=None): """ Rotate the tree :param parent: ExampleMenu parent menu of this max menu Should be from BST :return: ExampleMenu rotate menu """ menu = Menu("""Rotate the tree: """, parent_menu=parent) # Menu options # Pass *references* to functions, not return values menu.add_option("Rotate left at...", function=BST.Rotate.left) menu.add_option("Rotate right at...", function=BST.Rotate.right) return menu
def get_menu(cls, parent=None) -> Menu: """ Get max heap menu options :param parent: ExampleMenu parent menu of this max menu :return: ExampleMenu max heap menu """ menu = Menu("""Binary max heap main menu. Please select an option below. """, parent_menu=parent) # Menu options # Pass *references* to functions, not return values menu.add_option("Add an item to the binary max heap", function=MaxHeap.add) menu.add_option("Extract the maximum value", function=MaxHeap.extract) menu.add_option("Show the heap", function=MaxHeap.show) menu.add_option("Toggle verbose on/off", function=MaxHeap.toggle_verbose) menu.add_option("Empty the heap", function=MaxHeap.empty_tree) return menu
def get_menu(cls, parent=None) -> Menu: """ Get binary search tree menu options :param parent: ExampleMenu parent menu of this max menu :return: ExampleMenu max heap menu """ menu = Menu("""Binary search tree main menu. Please select an option below. """, parent_menu=parent) rotate_menu = BST.Rotate.get_menu(parent=menu) # Menu options # Pass *references* to functions, not return values menu.add_option("Add an item to the tree", function=BST.insert) menu.add_option("Find an item in the tree", function=BST.search) menu.add_option("Delete an item from the tree", function=BST.delete) menu.add_option("Rotate tree", submenu=rotate_menu) menu.add_option("Show the tree", function=BST.show) menu.add_option("Toggle verbose on/off", function=BST.toggle_verbose) menu.add_option("Empty the tree", function=BST.empty_tree) return menu
from DataStructures.BinaryMaxHeap import BinaryMaxHeap from ConsoleMenu import Menu from ExampleFunctions import MaxHeap, BST myHeap = BinaryMaxHeap() # Build menus # Build submenus before main menu # so that references are available menu = Menu("""Program main menu. Please select a data structure type.""") # Max heap menu_maxheap = MaxHeap.get_menu(menu) menu_bst = BST.get_menu(menu) # Build main menu menu.add_option("Binary max heap", submenu=menu_maxheap) menu.add_option("Binary search tree", submenu=menu_bst) # Loop main menu menu.do_menu() # Finito print("\nThank you\n\n")