PYTHON: My program was supposed to identify the orders of the missing traversal, which is either the preorder or postorder. However, my program only works in ide

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
100%

PYTHON:
My program was supposed to identify the orders of the missing traversal, which is either the preorder or postorder. However, my program only works in identifying the PREORDER. There is something wrong with the function of my POSTORDER. Can you please identify the problem and fix it? I will also add an image below for the expected output and for your reference. Thank you.

 

# Main program
inorder_list = []
preorder_list = []
postorder_list = []
n = int(input("How many nodes want to enter? "))


# Methods
def searchR(arr, x, n):   # CHANGED THE NAME OF FUNCTION AS THERE ARE TWO FUNCTION WITH SAME NAME
     for i in range(n):
          if (arr[i] == x):
               return i

     return -1


def PostOrderT(Ino, preo, n):
     root = searchR(Ino, preo[0], n)
     if (root != 0):
          PostOrderT(Ino, preo[1:n], root)
     if (root != n - 1):
          PostOrderT(Ino[root + 1:n], preo[root + 1:n], n - root - 1)
     print(preo[0], end=" ")


# Make new node
class newNode:

     def __init__(self, data):
          self.data = data
          self.left = self.right = None


# Construct binary tree
def MakeTree(Ino, posto, inoStrt, inoEnd, p_Index):
     if (inoStrt > inoEnd):
          return None
     node = newNode(posto[p_Index[0]])
     p_Index[0] -= 1

     if (inoStrt == inoEnd):
          return node
     iIndex = search(Ino, inoStrt, inoEnd, node.data)
     node.right = MakeTree(Ino, posto, iIndex + 1, inoEnd, p_Index)
     node.left = MakeTree(Ino, posto, inoStrt, iIndex - 1, p_Index)

     return node


def bTree(Ino, posto, n):
     p_Index = [n - 1]
     return MakeTree(Ino, posto, 0, n - 1, p_Index)


def search(arr, astrt, aend, value):
     i = 0
     for i in range(astrt, aend + 1):
          if (arr[i] == value):
               break
     return i


def PreOrderT(node):
     if node == None:
          return
     print(node.data, end=" ")
     PreOrderT(node.left)
     PreOrderT(node.right)


print("Enter INORDER:")
for i in range(0, n):
     ele = input().split()
     if len(ele) == n:
          for i in ele:
               inorder_list.append(i) # adding the element
          break
     elif len(ele) > n:
               print("You have entered more nodes")
               break
     else:
          print("You have entered less nodes")
          break

print(inorder_list)

print("\nWould you like to use Preorder or Postorder?")
choice = int(input("Press [1] for PREORDER and Press [2] for POSTORDER>> "))
if choice == 1:
     print("Enter PREORDER: ")
     for i in range(0, n):
          ele = input().split()
          if len(ele) == n:
               for i in ele:
                    preorder_list.append(i) # adding the element
               break
          elif len(ele) > n:
               print("You have entered more nodes")
               break
          else:
               print("You have entered less nodes")
               break
     print(preorder_list)
     print("\nMissing Traversal Identified!")
     print("POSTORDER traversal: ")
     bTree(inorder_list, preorder_list, n)                # NEED TO BUILD THE TREE FIRST
     PostOrderT(inorder_list, preorder_list, n)      # CALLING CORRECT FUNCTION
elif choice == 2:
     print("Enter POSTORDER: ")
     for i in range(0, n):
          ele = input().split()
          if len(ele) == n:
               for i in ele:
                    postorder_list.append(i) # adding the element
               break
          elif len(ele) > n:
               print("You have entered more nodes")
               break
          else:
               print("You have entered less nodes")
               break
     print(postorder_list)
     print("\nMissing Traversal Identified!")
     print("PREORDER traversal: ")
     PostOrderT(inorder_list, preorder_list, n)
     
else:
     print("Invalid choice")

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>> 2
Please input your POSTORDER>> G D E B F C A
Inorder:
G D BE A C F
Postorder: G DE B F C A
Missing Traversal Identified!
Preorder: A B D G E F C
Transcribed Image Text: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>> 2 Please input your POSTORDER>> G D E B F C A Inorder: G D BE A C F Postorder: G DE B F C A Missing Traversal Identified! Preorder: A B D G E F C
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