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.
Related
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.
For a Binary SEARCH Tree, a preorder or a postorder traversal is sufficient to reconstruct its original binary search tree unambigiously.
But I cannot find a proof or a explanation for why it is so.
For inorder traversal, it is trivial to come up with a counter-example to show that there may be many different BSTs correspond to a given inorder traversal.
Is there any proof or reference that a preorder or a postorder traversal is sufficient to reconstruct its original BST unambiguously?
This is for a BST, not for a general binary tree.
By the definition of pre-order traversal, it is guaranteed that root will be the first element.
Now, given the root, there is a unique set of elements that will come in the left half of the root - i.e. all elements having value less that root->val.
Likewise for the right subtree.
Now, we have to apply the above logic recursively for the left subtree and the right subtree.
Since, post-order traversal is just the reverse of pre-order traversal on the reverse tree, the same logic applies to post-order traversal too.
I have a difficulty in understanding the differences between dfs, bfs, and in-order, pre-order and post-order in tree traversal (binary tree).
My understanding that
dfs is neither pre-order, in-order or post-order
Bfs is also neither pre-order, in-order or post-order
But I see pre-order, in-order or post-order versions in dfs in wikipedia, anybody knows why?
https://en.wikipedia.org/wiki/Tree_traversal
It depended on what order you visit the nodes in your recursive function.
https://en.wikipedia.org/wiki/Tree_traversal#Depth-first_search.28DFS.29
If you visit the left child tree, then current node, then right child tree, the iterator will come put in order because the first node to be read will be the far right leaf, then it's parent, then it's parent's right leaf, etc.
Other arrangements of these operations will result in other orderings.
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".
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!