Binary tree parent same as in order first output - algorithm

So i have a question that is confusing me.
It says that the INORDER traversal output of a binary tree is [2,5,4,8,1,6,3,7].
The PREORDER traversal output of the SAME tree is [2,1,4,5,8,3,6,7].
Using this 2 output, i have to find the postorder and construct the binary tree.
I know that the first element of preorder is the root of the binary tree.
If so, how can it be possible that the root of the binary tree is the first element of inorder traversal output as well?

If root only has one child node on its right side.

The tree that is represented is:
As you can see, it is quite possible that both orders list the root as first node. In-order lists the left subtree first, then the node (root) itself, and then the right subtree. If there is no left subtree (like in the image above), then the first output will be the node itself.
And as you already know preorder will anyway output the root first.

Related

I want to know for which binary tree does it support?

In which case(or for which binary tree) the last character of preorder and inorder are same(or 1st of post and inorder) ?
As per me for a binary tree it follows the above but i encounter a tree for which it is not supporting...
[i was not allowed to put picture here so m explaining the tree]
A is the root
B,C are child of A respectively
E,F are of B
G is left child of C
in the above description you can get the last char of preorder and inorder are not same.
I want to know for which binary tree does it support ?
The answer is skew Tree.
Preorder means the following:
First the Root,Then the Left subtree and then the right subtree.
Inorder means the following:
First the left subtree,then the Root and then the right subtree.
Now consider a skew tree which has no left child anywhere.This means we might as well completely ignore the Left subtree part of the two definitions above and what we get is:
The Root and then the right subtree.
In both cases.
Hence the last character(in fact all characters) of a binary tree which is skew and has no left child anywhere is same for preorder and inorder.
Post order means the following:
First the left subtree,then the right subtree and then the root
Now consider a skew tree which has no right child anywhere.This means we might as well completely ignore the right subtree part of the two definitions of postorder and inorder and what we get is:
The left subtree and then the root.
in both cases
Hence the first character(in fact all characters) of a binary tree which is skew and has no right child anywhere is same for postorder and inorder.

Proving that one binary tree is a subtree of another

Assume you have two binary trees and you want to know whether one is a subtree of the other. One solution is to get the inorder and preorder traversals of both trees and check whether the traversals of the candidate subtree are substrings of the corresponding traversal for the other tree. I read several posts about this posts about this solution. One discussion shows that inorder AND preorder traversal are both necessary. Can someone explain why they are sufficient? Why is the case that if the inorder and preorder traversal of tree2 are substrings of those of tree1, then tree2 is a subtree of tree1?
Q: One discussion shows that inorder AND preorder traversal are both
necessary. Can someone explain why they are sufficient?
Because of the simple fact that it is possible to uniquely reconstruct a binary tree from these two traversals (or inorder and postorder, as well). Check this example:
Inorder : [1,2,3,4,5,6]
Preorder : [4,2,1,3,5,6]
From preorder, you know that 4 is the root of the tree. From inorder, you can determine the left and right subtree, and you proceed recursively from this point:
4
/ \
Left subtree Right subtree
Inorder : [1,2,3] Inorder : [5,6]
Preorder: [2,1,3] Preorder: [5,6]
Check for more details in this excellent article:
Reconstructing binary trees from tree traversal. Since these two serializations (traversals actually serialize tree to a string) of the tree combined together have to be unique for a binary tree, we get that one tree is a subtree of another if and only if these traversals are substrings of other two serializations.
People agreed that binary tree can represent the order on it's nodes by left/right relation. That means that left part comes before right part. You may call trees equivalent if the order is the same. So in-order string represents the the order and if you want to check the equivalence, then it is sufficient to check only in-order (by definition).
But when you want to check the full equality of trees then we have to find the way how we can distinguish equivalent trees.For example it can be level-order check. But for subtrees level order doesn't fit, because the level order string for subtree is split. For pre-order you walk the subtree form root before other parts of tree.
Suppose equivalent trees are not equal, then traversing in pre-order everything will be equal until first differ. 2 situations can happen.
1) The value of node of one tree differs from another. That means that pre-order strings differs, because you walk the tree in pre-order.
2) Children signature (no children, only left, only right, both children) differs. But in this situation easy to understand that the in-order will change and trees are not equivalent, which contradicts the conditions.
Note that this works only when all the nodes are unique. If you have all nodes of value like "a" then no matter how you walk, your string is always "aa...a". So you have to distinguish the nodes somehow, not only by "value".

BST from Preorder by just inserting the nodes in same order

To construct a BST from the preorder traversal given, if I try to insert in the BST in the same order as given in preorder, I get the BST.
So, we don't to create the in-order by sorting of the elements or perform any other alogrithm?
Is there an example which shows that tree can't be constructed by just inserting the elements ?
You're correct... you can just insert the nodes in the order given by a preorder traversal to rebuild the tree.
The first node inserted must be placed at the right place, since it's the root and a preorder traversal always places the root first. What follows in the preorder traversal is the preorder layout of the left subtree followed by the right subtree. As the left subtree nodes are inserted, they are inserted by going left from the root, then recursively applying the same procedure on that subtree. The right subtree is rebuilt the same way. Using induction, you can formally prove this if you like.
Hope this helps!

Does inorder in reverse also classify as inorder?

I know inorder traversal works like this:
Traverse the left subtree.
Visit the root.
Traverse the right subtree.
But what if we have an algorithm that does the following
Traverse the right subtree.
Visit the root.
Traverse the left subtree.
Would such a tree traversal be consdered inorder as well?
I was wondering the same thing for some time.
I would say, it can be called inorder traversal also. The result would be reverse sorted array instead of the sorting that would come up with left-root-right.
But the definitions are strict on saying left-root-right.
It will still be an inorder traversal. Inorder is simetrical.
From wikipedia:
To traverse a non-empty binary tree in
inorder (symmetric), perform the
following operations recursively at
each node:
Traverse the left subtree.
Visit the root.
Traverse the right subtree.
Reference.
Yes, assuming it's a binary search tree, it's still an inorder traversal. The order would be the reverse of that obtained from visiting the left subtree first, instead of the right.

Postorder Traversal

In-order tree traversal obviously has application; getting the contents in order.
Preorder traversal seems really useful for creating a copy of the tree.
Is there a common use for postorder traversal of a binary tree?
Let me add another one:
Postorder traversal is also useful in deleting a tree. In order to free up allocated memory of all nodes in a tree, the nodes must be deleted in the order where the current node can only be deleted when both of its left and right subtrees are deleted.
Postorder does exactly just that. It processes both of the left and right subtrees before processing the current node.
If the tree represents a mathematical expression, then to evaluate the expression, a post-order traversal is necessary.
Yes. Postorder is sometimes used to translate mathematical expressions between different notations.
It can also generate a postfix representation of a binary tree.

Resources