|
| 1 | +''' |
| 2 | + Inorder Traversal of Binary Tree: |
| 3 | + In the Inorder Traversal first the left node of the tree will be printed, |
| 4 | + than the root node, and at end right node will be printed for every Sub-tree, |
| 5 | + Just like: (A+B) |
| 6 | +
|
| 7 | + Preorder Traversal of Binary Tree: |
| 8 | + In the Preorder Traversal first the left node of the tree will be printed, |
| 9 | + than the right node, and at end root node will be printed for every Sub-tree, |
| 10 | + Just like: (AB+) |
| 11 | +
|
| 12 | + Postorder Traversal of Binary Tree: |
| 13 | + In the Inorder Traversal first the root node of the tree will be printed, |
| 14 | + than the left node, and at end right node will be printed for every Sub-tree, |
| 15 | + Just like: (+AB) |
| 16 | +''' |
| 17 | +# Defination of Class Node. |
| 18 | +class Node: |
| 19 | + # Defination of Node class constructor. |
| 20 | + def __init__(self, data: int): |
| 21 | + self.left = None |
| 22 | + self.right = None |
| 23 | + self.data = data |
| 24 | + |
| 25 | + # inorder() function defination, |
| 26 | + # that will print the Inorder Traversal of given tree. |
| 27 | + def inorder(self, root: object)->None: |
| 28 | + if(root): |
| 29 | + root.inorder(root.left) |
| 30 | + print("[", root.data,"]", end ="\t") |
| 31 | + root.inorder(root.right) |
| 32 | + |
| 33 | + # preorder() function defination, |
| 34 | + # that will print the Preorder Traversal of given tree. |
| 35 | + def preorder(self, root: object)->None: |
| 36 | + if(root): |
| 37 | + print("[", root.data,"]", end ="\t") |
| 38 | + root.preorder(root.left) |
| 39 | + root.preorder(root.right) |
| 40 | + |
| 41 | + # Postorder() function defination, |
| 42 | + # that will print the Postorder Traversal of given tree. |
| 43 | + def postorder(self, root: object)->None: |
| 44 | + if(root): |
| 45 | + root.postorder(root.left) |
| 46 | + root.postorder(root.right) |
| 47 | + print("[", root.data,"]", end ="\t") |
| 48 | + |
| 49 | + |
| 50 | +''' |
| 51 | +Structure of Tree: |
| 52 | +
|
| 53 | +Test Case 01: |
| 54 | + 11 |
| 55 | + / \ |
| 56 | + 9 15 |
| 57 | + / \ / \ |
| 58 | + 7 10 13 20 |
| 59 | + / \ |
| 60 | + 5 8 |
| 61 | +
|
| 62 | +Test Case 02: |
| 63 | + 10 |
| 64 | + / \ |
| 65 | + 20 30 |
| 66 | + / \ |
| 67 | + 40 50 |
| 68 | + / \ |
| 69 | + 60 70 |
| 70 | +''' |
| 71 | +# main function or the driver function. |
| 72 | +if __name__ == '__main__': |
| 73 | + ''' |
| 74 | + Formation of tree, |
| 75 | + as per Test Case 01. |
| 76 | + ''' |
| 77 | + root = Node(11) |
| 78 | + root.left = Node(9) |
| 79 | + root.right = Node(15) |
| 80 | + root.left.left = Node(7) |
| 81 | + root.left.right = Node(10) |
| 82 | + root.right.left = Node(13) |
| 83 | + root.right.right = Node(20) |
| 84 | + root.left.left.left = Node(5) |
| 85 | + root.left.left.right = Node(8) |
| 86 | + |
| 87 | + print("\n***Running Test Case 01***", end = " ") |
| 88 | + # Printing the Inorder Traversal for Test Case 01. |
| 89 | + print("\nInorder Traversal :") |
| 90 | + root.inorder(root) |
| 91 | + # Printing the Preorder Traversal for Test Case 01. |
| 92 | + print("\n\nPreorder Traversal :") |
| 93 | + root.preorder(root) |
| 94 | + # Printing the Postorder Traversal for Test Case 01. |
| 95 | + print("\n\nPostorder Traversal :") |
| 96 | + root.postorder(root) |
| 97 | + |
| 98 | + ''' |
| 99 | + Formation of tree, |
| 100 | + as per Test Case 02. |
| 101 | + ''' |
| 102 | + root = Node(10) |
| 103 | + root.left = Node(20) |
| 104 | + root.right = Node(30) |
| 105 | + root.right.left = Node(40) |
| 106 | + root.right.right = Node(50) |
| 107 | + root.right.right.left = Node(60) |
| 108 | + root.right.right.right = Node(70) |
| 109 | + |
| 110 | + print("\n\n***Running Test Case 02***", end = " ") |
| 111 | + # Printing the Inorder Traversal for Test Case 01. |
| 112 | + print("\nInorder Traversal :") |
| 113 | + root.inorder(root) |
| 114 | + # Printing the Preorder Traversal for Test Case 01. |
| 115 | + print("\n\nPreorder Traversal :") |
| 116 | + root.preorder(root) |
| 117 | + # Printing the Postorder Traversal for Test Case 01. |
| 118 | + print("\n\nPostorder Traversal :") |
| 119 | + root.postorder(root) |
| 120 | + |
| 121 | +''' |
| 122 | +Sample Output: |
| 123 | +
|
| 124 | +***Running Test Case 01*** |
| 125 | +Inorder Traversal : |
| 126 | +[ 5 ] [ 7 ] [ 8 ] [ 9 ] [ 10 ] [ 11 ] [ 13 ] [ 15 ] [ 20 ] |
| 127 | +
|
| 128 | +Preorder Traversal : |
| 129 | +[ 11 ] [ 9 ] [ 7 ] [ 5 ] [ 8 ] [ 10 ] [ 15 ] [ 13 ] [ 20 ] |
| 130 | +
|
| 131 | +Postorder Traversal : |
| 132 | +[ 5 ] [ 8 ] [ 7 ] [ 10 ] [ 9 ] [ 13 ] [ 20 ] [ 15 ] [ 11 ] |
| 133 | +
|
| 134 | +***Running Test Case 02*** |
| 135 | +Inorder Traversal : |
| 136 | +[ 20 ] [ 10 ] [ 40 ] [ 30 ] [ 60 ] [ 50 ] [ 70 ] |
| 137 | +
|
| 138 | +Preorder Traversal : |
| 139 | +[ 10 ] [ 20 ] [ 30 ] [ 40 ] [ 50 ] [ 60 ] [ 70 ] |
| 140 | +
|
| 141 | +Postorder Traversal : |
| 142 | +[ 20 ] [ 40 ] [ 60 ] [ 70 ] [ 50 ] [ 30 ] [ 10 ] |
| 143 | +''' |
0 commit comments