''''
(a) Inorder (Left, Root, Right) : 4 2 5 1 3
(b) Preorder (Root, Left, Right) : 1 2 4 5 3
(c) Postorder (Left, Right, Root) : 4 5 2 3 1
'''
class Node:
def __init__(self, node):
self.left = None
self.right = None
self.val = node
def printInorder(root):
if root:
#First recur on left child
printInorder(root.left)
#then print the data of node
print(root.val)
#now recur on right child
printInorder(root.right)
def printPostorder(root):
if root:
#First recur on left child
printPostorder(root.left)
#the recur on right child
printPostorder(root.right)
#root
print(root.val)
def printPreorder(root):
if root:
#First print the data of node
print(root.val)
#Then recur on left child
printPreorder(root.left)
#finally recur on right child
printPreorder(root.right)
if __name__ == '__main__':
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.right.right = Node(5)
root.left.left.left = Node(10)
root.right.right.right = Node(100)
print('Inorder traversal of binary tee is:')
printInorder(root)
print('\nPreorder Traversal of binary tree is:')
printPreorder(root)
print('\nPostorder traversal of binary tree is:')
printPostorder(root)
0 Comments