|
| 1 | +# Import the BinaryTree package from the same directory. |
| 2 | +from BinaryTree import BinaryTree, _Node |
| 3 | + |
| 4 | + |
| 5 | +class BST: |
| 6 | + def __init__(self): |
| 7 | + ''' |
| 8 | + Initialises the root to None and creates instance of Binary Tree modules, |
| 9 | + from the BinaryTree.py file. |
| 10 | + ''' |
| 11 | + self._root = None |
| 12 | + self.BT = BinaryTree() |
| 13 | + |
| 14 | + def insertion_iterative(self, e): |
| 15 | + ''' |
| 16 | + Inserts an item into the BST using Iterative Approach |
| 17 | + ''' |
| 18 | + temp = None |
| 19 | + troot = self._root |
| 20 | + while troot: |
| 21 | + temp = troot |
| 22 | + if e < troot._element: |
| 23 | + troot = troot._left |
| 24 | + else: |
| 25 | + troot = troot._right |
| 26 | + |
| 27 | + if self._root: |
| 28 | + if e < temp._element: |
| 29 | + temp._left = _Node(e) |
| 30 | + else: |
| 31 | + temp._right = _Node(e) |
| 32 | + else: |
| 33 | + self._root = _Node(e) |
| 34 | + |
| 35 | + def insertion_recursive(self, troot, e): |
| 36 | + ''' |
| 37 | + Inserts an item into the BST using Recursive Approach |
| 38 | + ''' |
| 39 | + if troot: |
| 40 | + if e < troot._element: |
| 41 | + troot._left = self.insertion_recursive(troot._left, e) |
| 42 | + elif e > troot._element: |
| 43 | + troot._right = self.insertion_recursive(troot._right, e) |
| 44 | + else: |
| 45 | + troot = _Node(e) |
| 46 | + return troot |
| 47 | + |
| 48 | + def search_iterative(self, key): |
| 49 | + ''' |
| 50 | + Searches for an item in the BST using Iterative Approach |
| 51 | + ''' |
| 52 | + troot = self._root |
| 53 | + while troot: |
| 54 | + if key == troot._element: |
| 55 | + return True |
| 56 | + elif key < troot._element: |
| 57 | + troot = troot._left |
| 58 | + elif key > troot._element: |
| 59 | + troot = troot._right |
| 60 | + return False |
| 61 | + |
| 62 | + def search_recursive(self, troot, key): |
| 63 | + ''' |
| 64 | + Searches for an item in the BST using Recursive Approach |
| 65 | + ''' |
| 66 | + if troot: |
| 67 | + if key == troot._element: |
| 68 | + return True |
| 69 | + elif key < troot._element: |
| 70 | + return self.search_recursive(troot._left, key) |
| 71 | + elif key > troot._element: |
| 72 | + return self.search_recursive(troot._right, key) |
| 73 | + else: |
| 74 | + return False |
| 75 | + |
| 76 | + def delete(self, key): |
| 77 | + ''' |
| 78 | + Deletes an item in the BST |
| 79 | + |
| 80 | + p -- reference to the node we want to delete |
| 81 | + pp -- reference to parent node of p |
| 82 | + s -- reference to the largest element in the left subtree |
| 83 | + ps -- reference to the node we want to delete (temporary) |
| 84 | + later it becomes parent to s node |
| 85 | + c -- reference to the child node |
| 86 | + ''' |
| 87 | + p = self._root |
| 88 | + pp = None |
| 89 | + |
| 90 | + # Finding the Node we want to delete |
| 91 | + while p and p._element != key: |
| 92 | + pp = p |
| 93 | + if key < p._element: |
| 94 | + p = p._left |
| 95 | + else: |
| 96 | + p = p._right |
| 97 | + |
| 98 | + # If the Node doesn't exist in the tree. |
| 99 | + if not p: |
| 100 | + return False |
| 101 | + |
| 102 | + # If the Node has both left and right sub tree. |
| 103 | + if p._left and p._right: |
| 104 | + s = p._left |
| 105 | + ps = p |
| 106 | + # Finds the largest element in the left subtree |
| 107 | + while s._right: |
| 108 | + ps = s |
| 109 | + s = s._right |
| 110 | + p._element = s._element |
| 111 | + p = s |
| 112 | + pp = ps |
| 113 | + |
| 114 | + # If the node has atmost one child i.e. left child or right child |
| 115 | + # or no child (lead node) |
| 116 | + c = None |
| 117 | + if p._left: |
| 118 | + c = p._left |
| 119 | + else: |
| 120 | + c = p._right |
| 121 | + |
| 122 | + if p == self._root: |
| 123 | + self._root = None |
| 124 | + else: |
| 125 | + if p == pp._left: |
| 126 | + pp._left = c |
| 127 | + else: |
| 128 | + pp._right = c |
| 129 | + |
| 130 | + return True |
| 131 | + |
| 132 | + def inorder(self): |
| 133 | + self.BT.inorder(self._root) |
| 134 | + |
| 135 | + |
| 136 | +def options(): |
| 137 | + ''' |
| 138 | + Prints Menu for operations |
| 139 | + ''' |
| 140 | + options_list = ['Add an item (Iterative)', 'Add multiple items (Iterative)', |
| 141 | + 'Add an item (Recursive)', 'Add multiple items (Recursive)', |
| 142 | + 'Search (Iterative)', 'Search (Recursive)', 'Delete', |
| 143 | + 'Display BST (Inorder)', 'Exit'] |
| 144 | + print("\nMENU") |
| 145 | + for i, option in enumerate(options_list): |
| 146 | + print(f'{i + 1}. {option}') |
| 147 | + choice = int(input("Enter choice: ")) |
| 148 | + return choice |
| 149 | + |
| 150 | + |
| 151 | +def switch_case(choice): |
| 152 | + ''' |
| 153 | + Switch Case for operations |
| 154 | + ''' |
| 155 | + # os.system('cls') |
| 156 | + if choice == 1: |
| 157 | + elem = int(input("Enter an item: ")) |
| 158 | + bst.insertion_iterative(elem) |
| 159 | + |
| 160 | + elif choice == 2: |
| 161 | + A = list(map(int, input("Enter numbers: ").split())) |
| 162 | + for elem in A: |
| 163 | + bst.insertion_iterative(elem) |
| 164 | + |
| 165 | + elif choice == 3: |
| 166 | + elem = int(input("Enter an item: ")) |
| 167 | + bst.insertion_recursive(elem) |
| 168 | + |
| 169 | + elif choice == 4: |
| 170 | + A = list(map(int, input("Enter numbers: ").split())) |
| 171 | + for elem in A: |
| 172 | + bst.insertion_recursive(elem) |
| 173 | + |
| 174 | + elif choice == 5: |
| 175 | + elem = int(input("Enter an item to search: ")) |
| 176 | + print("Found item.") if bst.search_iterative( |
| 177 | + elem) else print("Item does not exist.") |
| 178 | + |
| 179 | + elif choice == 6: |
| 180 | + elem = int(input("Enter an item to search: ")) |
| 181 | + print("Found item.") if bst.search_recursive(bst._root, |
| 182 | + elem) else print("Item does not exist.") |
| 183 | + |
| 184 | + elif choice == 7: |
| 185 | + elem = int(input("Enter an item to delete: ")) |
| 186 | + print("Item Deleted.") if bst.delete( |
| 187 | + elem) else print("Item doesn't exist.") |
| 188 | + |
| 189 | + elif choice == 8: |
| 190 | + print("Inorder Traversal:") |
| 191 | + bst.inorder() |
| 192 | + |
| 193 | + elif choice == 9: |
| 194 | + import sys |
| 195 | + sys.exit() |
| 196 | + |
| 197 | +############################################################################### |
| 198 | + |
| 199 | + |
| 200 | +if __name__ == '__main__': |
| 201 | + bst = BST() |
| 202 | + while True: |
| 203 | + choice = options() |
| 204 | + switch_case(choice) |
0 commit comments