Please input your INORDER>> G D BEAC 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 GEFC Inorder: G D BEAC F Preorder: A B D G E F C Missing Traversal Identified! Postorder: G D E B F C A
PYTHON:
Please help me modify this program. Instead of putting the letters/nodes in the code, please change it to accept user input of inorder and preorder traversal rather than putting the nodes in the code itself. I also inserted an image below because I am trying to print out the same output as the image. I you can modify it just like the image, then please do. Thank you very much.
#POSTORDER
class Node:
def __init__(self, data):
self.left = None
self.right = None
self.data = data
# Insert Node
def insert(self, data):
if self.data:
if data < self.data:
if self.left is None:
self.left = Node(data)
else:
self.left.insert(data)
elif data > self.data:
if self.right is None:
self.right = Node(data)
else:
self.right.insert(data)
else:
self.data = data
# Print the Tree
def PrintTree(self):
if self.left:
self.left.PrintTree()
print(self.data),
if self.right:
self.right.PrintTree()
# Postorder traversal
# Left ->Right -> Root
def PostorderTraversal(self, root):
res = []
if root:
res = self.PostorderTraversal(root.left)
res = res + self.PostorderTraversal(root.right)
res.append(root.data)
return res
root = Node('G')
root.insert('D')
root.insert('E')
root.insert('B')
root.insert('A')
root.insert('F')
root.insert('C')
print(root.PostorderTraversal(root))
#PREORDER
class Node:
def __init__(self, data):
self.left = None
self.right = None
self.data = data
# Insert Node
def insert(self, data):
if self.data:
if data < self.data:
if self.left is None:
self.left = Node(data)
else:
self.left.insert(data)
elif data > self.data:
if self.right is None:
self.right = Node(data)
else:
self.right.insert(data)
else:
self.data = data
# Print the Tree
def PrintTree(self):
if self.left:
self.left.PrintTree()
print(self.data),
if self.right:
self.right.PrintTree()
# Preorder traversal
# Root -> Left ->Right
def PreorderTraversal(self, root):
res = []
if root:
res.append(root.data)
res = res + self.PreorderTraversal(root.left)
res = res + self.PreorderTraversal(root.right)
return res
root = Node('A')
root.insert('B')
root.insert('D')
root.insert('G')
root.insert('E')
root.insert('F')
root.insert('C')
print(root.PreorderTraversal(root))
#INORDER
class Node:
def __init__(self, data):
self.left = None
self.right = None
self.data = data
# Insert Node
def insert(self, data):
if self.data:
if data < self.data:
if self.left is None:
self.left = Node(data)
else:
self.left.insert(data)
elif data > self.data:
if self.right is None:
self.right = Node(data)
else:
self.right.insert(data)
else:
self.data = data
# Print the Tree
def PrintTree(self):
if self.left:
self.left.PrintTree()
print(self.data),
if self.right:
self.right.PrintTree()
# Inorder traversal
# Left -> Root -> Right
def inorderTraversal(self, root):
res = []
if root:
res = self.inorderTraversal(root.left)
res.append(root.data)
res = res + self.inorderTraversal(root.right)
return res
root = Node('G')
root.insert('D')
root.insert('B')
root.insert('E')
root.insert('A')
root.insert('C')
root.insert('F')
print(root.inorderTraversal(root))
Step by step
Solved in 2 steps