Construct Tree from given Inorder and Preorder traversals

Let us consider the below traversals:

Inorder sequence: D B E A F C
Preorder sequence: A B D E C F

In a Preorder sequence, leftmost element is the root of the tree. So we know ‘A’ is root for given sequences. By searching ‘A’ in Inorder sequence, we can find out all elements on left side of ‘A’ are in left subtree and elements on right are in right subtree. So we know below structure now.

A
/ \
/ \
D B E F C

We recursively follow above steps and get the following tree.

A
/ \
/ \
B C
/ \ /
/ \ /
D E F

Algorithm: buildTree()
1) Pick an element from Preorder. Increment a Preorder Index Variable (preIndex in below code) to pick next element in next recursive call.
2) Create a new tree node tNode with the data as picked element.
3) Find the picked element’s index in Inorder. Let the index be inIndex.
4) Call buildTree for elements before inIndex and make the built tree as left subtree of tNode.
5) Call buildTree for elements after inIndex and make the built tree as right subtree of tNode.
6) return tNode.

The given inorder traversal would be E D B A, and preorder would be A, B, D, E.
In the above code, we one by one take elements from preorder traversal and search the element in inorder traversal. Searching the first element will take O(n) time, searching the second element will take O(n-1) time, searching the 3rd element will take O(n-2) time, and so on. So total time would be O(n) + O(n-1) + O(n-2) + … + 1. The sum of this series is O(n*(n-1)/2) which is O(n^2)

@tiger: When you call the same function for different inputs, static variables may cause unexpected results. Because static variables may be storing the values of previous function call for a different input.

i think u know that very well,pre order is necessary for making a tree.ok
now
pre order is like that,root left right.
so 35 is our parent node or root.
then covered it by a circle in both pre and in order.
in pre order the next element is 31,now we search the respective position of 31 in in order form.
31 is in the left side of the root node ’35’ ok
that means 31 is the left child of 35.
similarly we check for all element.
if any other query then contact me on ankitkharkwal@gmail.com

ankit

i think u know that very well,pre order is necessary for making a tree.ok
now
pre order is like that,root left right.
so 35 is our parent node or root.
then covered it by a circle in both pre and in order.
in pre order the next element is 31,now we search the respective position of 31 in in order form.
31 is in the left side of the root node ’35’ ok
that means 31 is the left child of 35.
similarly we check for all element.
if any other query then contact me on ankitkharkwal@gmail.com

http://www.facebook.com/profile.php?id=100003406015146 Hatlar

Iterative DFS, looking for an efinefcit implementation.I know an explicit stack can be used. One way is, use explicit stack, and keep pushing all the adjacent nodes while exploring each path node in unexplored path. Following this approach, in the worst case, explicit stack will have all the adjacent nodes in the longest path of a node in graph.Where as in recursive approach, the system stack will have max nodes proportional to the depth of the longest node.Any thoughts? I am thinking to keep track of visited node and current exploring node in it’s adjacent list. In other words, mitigating system stack functionality by storing stack frame explicitly.

Abhishek

Use Binary Search in
int search(char arr[], int strt, int end, char value)
Since INorder is Increasing sequence, and that would make it O(nlogn) solution

Kk

It is a binary tree that we are trying to construct, not a binary search tree. As a result, the inorder traversal of the tree may / may not yield a sorted array.

Additionally, if we wanted to construct a binary search tree, pre-order traversal would have been sufficient.

aayush kumar

yup for binary tree i think inorder travesal is ncessary and other travesal can be post/level/pre.
but for BST preoder/postorder alone are sufficient.
but using postoder to construct is very diificult.however preoder traversal alone can be used for constructing binary tree by including some symbol in place of null.
see leetcodehttp://www.leetcode.com/2010/09/serializationdeserialization-of-binary.html.

xTristan

(assume no duplicate chars allowed, otherwise there is some chance that the tree is ambiguous, like the AAA example above)

to optimize, you may pre-process the inorder string, create a hash map between each char and its index. That eliminates the need for a linear search in your search() method, reducing the time complexity of finding the index from linear to constant.