Question: We want to develop a method to reconstruct a binary tree from an inorder and preorder traversal sequence of the unknown tree. Please write a

We want to develop a method to reconstruct a binary tree from an inorder and preorder traversal sequence of the unknown tree. Please write a program, which lets the user input the inorder and preorder traversal sequences and from this reconstructs the corresponding binary tree and outputs it using the print() method.

Please note: - All nodes of the tree contain a single character and the inorder and preorder traversal sequences are strings formed by these characters in the corresponding order. - You can assume that all characters are different. The examples above have some duplicate characters but should still work if your implementation uses the indexOf method (i.e. uses the first occurrence of a character in a string). - Note that in our examples the reconstructed tree is a binary tree, but not a binary search tree - Please construct the tree using a list-of-list representation and use the supplied file. It has to write in python Example:

Binary Tree reconstructed:

Please enter the inorder sequence: abcdefghijklmn

Please enter the preoder sequence: hcabedgfkijnlm

[h, [c, [a, None, [b, None, None]], [e, [d, None, None], [g, [f, None, None], None]]], [k, [i, None, [j, None, None]], [n, [l, None, [m, None, None]], None]]]

Result:

We want to develop a method to reconstruct a binary tree from

C a e n m f

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!