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:
Please write in python or other program. Thank you.
C a e n m f
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started