|
| 1 | +# Author: OMKAR PATHAK |
| 2 | + |
| 3 | +# This program illustrates an example of Binary Search Tree using Python |
| 4 | +# Binary Search Tree, is a node-based binary tree data structure which has the following properties: |
| 5 | +# |
| 6 | +# The left subtree of a node contains only nodes with keys less than the node’s key. |
| 7 | +# The right subtree of a node contains only nodes with keys greater than the node’s key. |
| 8 | +# The left and right subtree each must also be a binary search tree. |
| 9 | +# There must be no duplicate nodes. |
| 10 | + |
| 11 | +class Node(object): |
| 12 | + def __init__(self, data): |
| 13 | + self.data = data |
| 14 | + self.leftChild = None |
| 15 | + self.rightChild = None |
| 16 | + |
| 17 | + def insert(self, data): |
| 18 | + ''' For inserting the data in the Tree ''' |
| 19 | + if self.data == data: |
| 20 | + return False # As BST cannot contain duplicate data |
| 21 | + |
| 22 | + elif data < self.data: |
| 23 | + ''' Data less than the root data is placed to the left of the root ''' |
| 24 | + if self.leftChild: |
| 25 | + return self.leftChild.insert(data) |
| 26 | + else: |
| 27 | + self.leftChild = Node(data) |
| 28 | + return True |
| 29 | + |
| 30 | + else: |
| 31 | + ''' Data greater than the root data is placed to the right of the root ''' |
| 32 | + if self.rightChild: |
| 33 | + return self.rightChild.insert(data) |
| 34 | + else: |
| 35 | + self.rightChild = Node(data) |
| 36 | + return True |
| 37 | + |
| 38 | + def minValueNode(self, node): |
| 39 | + current = node |
| 40 | + |
| 41 | + # loop down to find the leftmost leaf |
| 42 | + while(current.leftChild is not None): |
| 43 | + current = current.leftChild |
| 44 | + |
| 45 | + return current |
| 46 | + |
| 47 | + def delete(self, data): |
| 48 | + ''' For deleting the node ''' |
| 49 | + if self is None: |
| 50 | + return root |
| 51 | + |
| 52 | + # if current node's data is less than that of root node, then only search in left subtree else right subtree |
| 53 | + if data < self.data: |
| 54 | + self.leftChild = self.leftChild.delete(data) |
| 55 | + elif data > self.data: |
| 56 | + self.rightChild = self.rightChild.delete(data) |
| 57 | + else: |
| 58 | + # deleting node with one child |
| 59 | + if self.leftChild is None: |
| 60 | + temp = self.rightChild |
| 61 | + self = None |
| 62 | + return temp |
| 63 | + elif self.rightChild is None: |
| 64 | + temp = self.leftChild |
| 65 | + self = None |
| 66 | + return temp |
| 67 | + |
| 68 | + # deleting node with two children |
| 69 | + # first get the inorder successor |
| 70 | + temp = self.minValueNode(self.rightChild) |
| 71 | + self.data = temp.data |
| 72 | + self.rightChild = self.rightChild.delete(temp.data) |
| 73 | + |
| 74 | + return self |
| 75 | + |
| 76 | + def find(self, data): |
| 77 | + ''' This function checks whether the specified data is in tree or not ''' |
| 78 | + if(data == self.data): |
| 79 | + return True |
| 80 | + elif(data < self.data): |
| 81 | + if self.leftChild: |
| 82 | + return self.leftChild.find(data) |
| 83 | + else: |
| 84 | + return False |
| 85 | + else: |
| 86 | + if self.rightChild: |
| 87 | + return self.rightChild.find(data) |
| 88 | + else: |
| 89 | + return False |
| 90 | + |
| 91 | + def preorder(self): |
| 92 | + '''For preorder traversal of the BST ''' |
| 93 | + if self: |
| 94 | + print(str(self.data), end = ' ') |
| 95 | + if self.leftChild: |
| 96 | + self.leftChild.preorder() |
| 97 | + if self.rightChild: |
| 98 | + self.rightChild.preorder() |
| 99 | + |
| 100 | + def inorder(self): |
| 101 | + ''' For Inorder traversal of the BST ''' |
| 102 | + if self: |
| 103 | + if self.leftChild: |
| 104 | + self.leftChild.inorder() |
| 105 | + print(str(self.data), end = ' ') |
| 106 | + if self.rightChild: |
| 107 | + self.rightChild.inorder() |
| 108 | + |
| 109 | + def postorder(self): |
| 110 | + ''' For postorder traversal of the BST ''' |
| 111 | + if self: |
| 112 | + if self.leftChild: |
| 113 | + self.leftChild.postorder() |
| 114 | + if self.rightChild: |
| 115 | + self.rightChild.postorder() |
| 116 | + print(str(self.data), end = ' ') |
| 117 | + |
| 118 | +class Tree(object): |
| 119 | + def __init__(self): |
| 120 | + self.root = None |
| 121 | + |
| 122 | + def insert(self, data): |
| 123 | + if self.root: |
| 124 | + return self.root.insert(data) |
| 125 | + else: |
| 126 | + self.root = Node(data) |
| 127 | + return True |
| 128 | + |
| 129 | + def delete(self, data): |
| 130 | + if self.root is not None: |
| 131 | + return self.root.delete(data) |
| 132 | + |
| 133 | + def find(self, data): |
| 134 | + if self.root: |
| 135 | + return self.root.find(data) |
| 136 | + else: |
| 137 | + return False |
| 138 | + |
| 139 | + def preorder(self): |
| 140 | + if self.root is not None: |
| 141 | + print() |
| 142 | + print('Preorder: ') |
| 143 | + self.root.preorder() |
| 144 | + |
| 145 | + def inorder(self): |
| 146 | + print() |
| 147 | + if self.root is not None: |
| 148 | + print('Inorder: ') |
| 149 | + self.root.inorder() |
| 150 | + |
| 151 | + def postorder(self): |
| 152 | + print() |
| 153 | + if self.root is not None: |
| 154 | + print('Postorder: ') |
| 155 | + self.root.postorder() |
| 156 | + |
| 157 | +if __name__ == '__main__': |
| 158 | + tree = Tree() |
| 159 | + tree.insert(10) |
| 160 | + tree.insert(12) |
| 161 | + tree.insert(5) |
| 162 | + tree.insert(4) |
| 163 | + tree.insert(20) |
| 164 | + tree.insert(8) |
| 165 | + tree.insert(7) |
| 166 | + tree.insert(15) |
| 167 | + tree.insert(13) |
| 168 | + print(tree.find(1)) |
| 169 | + print(tree.find(12)) |
| 170 | + ''' Following tree is getting created: |
| 171 | + 10 |
| 172 | + / \ |
| 173 | + 5 12 |
| 174 | + / \ \ |
| 175 | + 4 8 20 |
| 176 | + / / |
| 177 | + 7 15 |
| 178 | + / |
| 179 | + 13 |
| 180 | + ''' |
| 181 | + |
| 182 | + tree.preorder() |
| 183 | + tree.inorder() |
| 184 | + tree.postorder() |
| 185 | + print('\n\nAfter deleting 20') |
| 186 | + tree.delete(20) |
| 187 | + tree.inorder() |
| 188 | + tree.preorder() |
| 189 | + print('\n\nAfter deleting 10') |
| 190 | + tree.delete(10) |
| 191 | + tree.inorder() |
| 192 | + tree.preorder() |
0 commit comments