|  | 
|  | 1 | +import os | 
|  | 2 | + | 
|  | 3 | + | 
|  | 4 | +class _Node: | 
|  | 5 | + ''' | 
|  | 6 | + Initialises node with the passed arguments, None if no arguments are passed. | 
|  | 7 | + ''' | 
|  | 8 | + __slots__ = '_element', '_left', '_right' | 
|  | 9 | + | 
|  | 10 | + def __init__(self, element, left=None, right=None): | 
|  | 11 | + self._element = element | 
|  | 12 | + self._left = left | 
|  | 13 | + self._right = right | 
|  | 14 | + | 
|  | 15 | + | 
|  | 16 | +class BinaryTree: | 
|  | 17 | + def __init__(self): | 
|  | 18 | + ''' | 
|  | 19 | + Initialises Root node to None. | 
|  | 20 | + ''' | 
|  | 21 | + self._root = None | 
|  | 22 | + | 
|  | 23 | + def createTree(self, node): | 
|  | 24 | + ''' | 
|  | 25 | + Recursively adds node to the tree using Postorder traversing pattern. | 
|  | 26 | + That means, it will first ask to add all the left childs of root | 
|  | 27 | + and then right child of the most recently added node. | 
|  | 28 | + ''' | 
|  | 29 | + # os.system('cls') | 
|  | 30 | + position = input( | 
|  | 31 | + f"Add node on the Left of {node._element} ? [y/n]: ") | 
|  | 32 | + if position == 'y': | 
|  | 33 | + nodeData = int(input("Enter data for node: ")) | 
|  | 34 | + curr = _Node(nodeData) | 
|  | 35 | + node._left = curr | 
|  | 36 | + self.createTree(curr) | 
|  | 37 | + | 
|  | 38 | + # os.system('cls') | 
|  | 39 | + position = input( | 
|  | 40 | + f"Add node on the Right of {node._element} ? [y/n]: ") | 
|  | 41 | + if position == 'y': | 
|  | 42 | + nodeData = int(input("Enter data for node: ")) | 
|  | 43 | + curr = _Node(nodeData) | 
|  | 44 | + node._right = curr | 
|  | 45 | + self.createTree(curr) | 
|  | 46 | + | 
|  | 47 | + def createRoot(self): | 
|  | 48 | + ''' | 
|  | 49 | + Function to create Root node. | 
|  | 50 | + ''' | 
|  | 51 | + rootData = int(input("Enter data for Root: ")) | 
|  | 52 | + self._root = _Node(rootData) | 
|  | 53 | + self.createTree(self._root) | 
|  | 54 | + | 
|  | 55 | + def inorder(self, troot): | 
|  | 56 | + ''' | 
|  | 57 | + Function for Inorder Traversal. | 
|  | 58 | + ''' | 
|  | 59 | + if troot: | 
|  | 60 | + self.inorder(troot._left) | 
|  | 61 | + print(troot._element, end=" ") | 
|  | 62 | + self.inorder(troot._right) | 
|  | 63 | + | 
|  | 64 | + def preorder(self, troot): | 
|  | 65 | + ''' | 
|  | 66 | + Function for Preorder Traversal. | 
|  | 67 | + ''' | 
|  | 68 | + if troot: | 
|  | 69 | + print(troot._element, end=" ") | 
|  | 70 | + self.preorder(troot._left) | 
|  | 71 | + self.preorder(troot._right) | 
|  | 72 | + | 
|  | 73 | + def postorder(self, troot): | 
|  | 74 | + ''' | 
|  | 75 | + Function for Postorder Traversal. | 
|  | 76 | + ''' | 
|  | 77 | + if troot: | 
|  | 78 | + self.postorder(troot._left) | 
|  | 79 | + self.postorder(troot._right) | 
|  | 80 | + print(troot._element, end=" ") | 
|  | 81 | + | 
|  | 82 | +############################################################################### | 
|  | 83 | + | 
|  | 84 | + | 
|  | 85 | +def options(): | 
|  | 86 | + ''' | 
|  | 87 | + Prints Menu for operations | 
|  | 88 | + ''' | 
|  | 89 | + options_list = ['Create Tree', 'Inorder', 'Preorder', | 
|  | 90 | + 'Postorder', 'Exit'] | 
|  | 91 | + | 
|  | 92 | + print("\nMENU") | 
|  | 93 | + for i, option in enumerate(options_list): | 
|  | 94 | + print(f'{i + 1}. {option}') | 
|  | 95 | + | 
|  | 96 | + choice = int(input("Enter choice: ")) | 
|  | 97 | + return choice | 
|  | 98 | + | 
|  | 99 | + | 
|  | 100 | +def switch_case(choice): | 
|  | 101 | + ''' | 
|  | 102 | + Switch Case for operations | 
|  | 103 | + ''' | 
|  | 104 | + # os.system('cls') | 
|  | 105 | + if choice == 1: | 
|  | 106 | + bt.createRoot() | 
|  | 107 | + | 
|  | 108 | + elif choice == 2: | 
|  | 109 | + print("Inorder Traversal:") | 
|  | 110 | + bt.inorder(bt._root) | 
|  | 111 | + | 
|  | 112 | + elif choice == 3: | 
|  | 113 | + print("Preorder Traversal:") | 
|  | 114 | + bt.preorder(bt._root) | 
|  | 115 | + | 
|  | 116 | + elif choice == 4: | 
|  | 117 | + print("Postorder Traversal:") | 
|  | 118 | + bt.postorder(bt._root) | 
|  | 119 | + | 
|  | 120 | + elif choice == 5: | 
|  | 121 | + import sys | 
|  | 122 | + sys.exit() | 
|  | 123 | + | 
|  | 124 | +############################################################################### | 
|  | 125 | + | 
|  | 126 | + | 
|  | 127 | +bt = BinaryTree() | 
|  | 128 | +while True: | 
|  | 129 | + choice = options() | 
|  | 130 | + switch_case(choice) | 
|  | 131 | + | 
|  | 132 | +''' | 
|  | 133 | +MENU | 
|  | 134 | +1. Create Tree | 
|  | 135 | +2. Inorder  | 
|  | 136 | +3. Preorder  | 
|  | 137 | +4. Postorder  | 
|  | 138 | +5. Exit  | 
|  | 139 | +Enter choice: 1 | 
|  | 140 | +Enter data for Root: 10 | 
|  | 141 | +Add node on the Left of 10 ? [y/n]: y | 
|  | 142 | +Enter data for node: 20 | 
|  | 143 | +Add node on the Left of 20 ? [y/n]: n | 
|  | 144 | +Add node on the Right of 20 ? [y/n]: n | 
|  | 145 | +Add node on the Right of 10 ? [y/n]: y | 
|  | 146 | +Enter data for node: 30 | 
|  | 147 | +Add node on the Left of 30 ? [y/n]: n | 
|  | 148 | +Add node on the Right of 30 ? [y/n]: n | 
|  | 149 | + | 
|  | 150 | +MENU | 
|  | 151 | +1. Create Tree | 
|  | 152 | +2. Inorder | 
|  | 153 | +3. Preorder | 
|  | 154 | +4. Postorder | 
|  | 155 | +5. Exit | 
|  | 156 | +Enter choice: 2 | 
|  | 157 | +Inorder Traversal: | 
|  | 158 | +20 10 30 | 
|  | 159 | +MENU | 
|  | 160 | +1. Create Tree | 
|  | 161 | +2. Inorder | 
|  | 162 | +3. Preorder | 
|  | 163 | +4. Postorder | 
|  | 164 | +5. Exit | 
|  | 165 | +Enter choice: 3 | 
|  | 166 | +Preorder Traversal: | 
|  | 167 | +10 20 30 | 
|  | 168 | +MENU | 
|  | 169 | +1. Create Tree | 
|  | 170 | +2. Inorder | 
|  | 171 | +3. Preorder | 
|  | 172 | +4. Postorder | 
|  | 173 | +5. Exit | 
|  | 174 | +Enter choice: 4 | 
|  | 175 | +Postorder Traversal: | 
|  | 176 | +20 30 10 | 
|  | 177 | +''' | 
0 commit comments