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

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

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))

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 CA
Transcribed Image Text: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 CA
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY