|
| 1 | +# Author: OMKAR PATHAK |
| 2 | +# This program illustrates an example of Binary Search Tree using Python |
| 3 | + |
| 4 | +class Node(object): |
| 5 | + def __init__(self, data): |
| 6 | + self.data = data |
| 7 | + self.leftChild = None |
| 8 | + self.rightChild = None |
| 9 | + |
| 10 | + def insert(self, data): |
| 11 | + ''' For inserting the data in the Tree ''' |
| 12 | + if self.data == data: |
| 13 | + return False # As BST cannot contain duplicate data |
| 14 | + |
| 15 | + elif data < self.data: |
| 16 | + ''' Data less than the root data is placed to the left of the root ''' |
| 17 | + if self.leftChild: |
| 18 | + return self.leftChild.insert(data) |
| 19 | + else: |
| 20 | + self.leftChild = Node(data) |
| 21 | + return True |
| 22 | + |
| 23 | + else: |
| 24 | + ''' Data greater than the root data is placed to the right of the root ''' |
| 25 | + if self.rightChild: |
| 26 | + return self.rightChild.insert(data) |
| 27 | + else: |
| 28 | + self.rightChild = Node(data) |
| 29 | + return True |
| 30 | + |
| 31 | + def preorder(self): |
| 32 | + '''For preorder traversal of the BST ''' |
| 33 | + if self: |
| 34 | + print(str(self.data)) |
| 35 | + if self.leftChild: |
| 36 | + self.leftChild.preorder() |
| 37 | + if self.rightChild: |
| 38 | + self.rightChild.preorder() |
| 39 | + |
| 40 | + def inorder(self): |
| 41 | + ''' For Inorder traversal of the BST ''' |
| 42 | + if self: |
| 43 | + if self.leftChild: |
| 44 | + self.leftChild.inorder() |
| 45 | + print(str(self.data)) |
| 46 | + if self.rightChild: |
| 47 | + self.rightChild.inorder() |
| 48 | + |
| 49 | + def postorder(self): |
| 50 | + ''' For postorder traversal of the BST ''' |
| 51 | + if self: |
| 52 | + if self.leftChild: |
| 53 | + self.leftChild.postorder() |
| 54 | + if self.rightChild: |
| 55 | + self.rightChild.postorder() |
| 56 | + print(str(self.data)) |
| 57 | + |
| 58 | +class Tree(object): |
| 59 | + def __init__(self): |
| 60 | + self.root = None |
| 61 | + |
| 62 | + def insert(self, data): |
| 63 | + if self.root: |
| 64 | + return self.root.insert(data) |
| 65 | + else: |
| 66 | + self.root = Node(data) |
| 67 | + return True |
| 68 | + |
| 69 | + def preorder(self): |
| 70 | + if self.root is not None: |
| 71 | + print('Preorder: ') |
| 72 | + self.root.preorder() |
| 73 | + |
| 74 | + def inorder(self): |
| 75 | + if self.root is not None: |
| 76 | + print('Inorder: ') |
| 77 | + self.root.inorder() |
| 78 | + |
| 79 | + def postorder(self): |
| 80 | + if self.root is not None: |
| 81 | + print('Postorder: ') |
| 82 | + self.root.postorder() |
| 83 | + |
| 84 | +if __name__ == '__main__': |
| 85 | + tree = Tree() |
| 86 | + tree.insert(10) |
| 87 | + tree.insert(12) |
| 88 | + tree.insert(5) |
| 89 | + tree.insert(4) |
| 90 | + tree.insert(20) |
| 91 | + tree.insert(8) |
| 92 | + tree.insert(7) |
| 93 | + tree.insert(15) |
| 94 | + tree.insert(13) |
| 95 | + tree.preorder() |
| 96 | + tree.inorder() |
| 97 | + tree.postorder() |
0 commit comments