Please input your INORDER>> G D BE A C F Would you like to use PREORDER or POSTORDER? Press 1 for PREORDER and press 2 for POSTORDER>> 1 Please input your PREORDER>> A B D GEF C Inorder: G D BE A C F Preorder: A B D G E F C Missing Traversal Identified! Postorder: G D E B F C A
PYTHON:
Can you please help me to modify this code in a way that it prints out the same output as the image below.
class Node:
def __init__(self, key):
self.left = None
self.right = None
self.val = key
# this is the function for Inorder
def printInorder(root):
if root:
# here calling the left child
printInorder(root.left)
# here printing the node data
print(root.val, end=" "),
# here calling the right child
printInorder(root.right)
# this is the Preorder method
def printPreorder(root):
if root:
# here printing the node data
print(root.val, end=" "),
# here calling the left child
printPreorder(root.left)
# here calling the right child
printPreorder(root.right)
# thsi is the postorder method
def printPostorder(root):
if root:
# here calling the left child
printPostorder(root.left)
# here calling the right child
printPostorder(root.right)
# here printing the node data
print(root.val, end=" "),
# this is the main function
if __name__ == "__main__":
root = Node('G')
root.left = Node('D')
root.right = Node('B')
root.left.left = Node('E')
root.left.right = Node('A')
root.right.left = Node('C')
root.right.right = Node('F')
# here calling the inorder method
print("Inorder:")
printInorder(root)
print('')
# here calling the Preorder method
print("Preorder:")
printPreorder(root)
print('')
# here calling the Postorder method
print("Postorder:")
printPostorder(root)
Step by step
Solved in 2 steps