I'm trying to ascertain whether it is possible for the following two traversals to be indentical for a single binary tree:
In-order traversal / Post-order traversal
In-order traversal / Pre-order traversal
Are the two below examples that I put together examples of binary trees? From my understanding, they would be skewed trees, unbalananced and functionally useless, but binary trees nonetheless.
1 in-order traversal: 3, 2, 1
/ post-order traversal: 3, 2, 1
2
/
3
1 in-order traversal: 1, 2, 3
\ pre-order traversal: 1, 2, 3
2
\
3
Yes, in some cases (like the examples you gave), different traversals will give the same result. They are different in approach, but that does not mean they will give different results for any input.
Related
I read somewhere that given a (preorder and inorder) or (Postorder and inorder) can uniquely define the tree.
I tried my own example and found that I am getting the wrong results.
The details are given below
Pre-order:-7,6,4,1,3,5,2,8
In-order:- 1,2,3,4,5,6,7,8
[Basically, it is a binary search tree(just by looking at in-order )]
I made the tree using this information and found out that the post-order corresponding to this is-2,3,1,5,4,6,8,7
Now I look to my tree and find preorder. It was not matching with the given order it is like- 7,6,4,1,3,2,5,8
Why is this error?
This is happening because 7,6,4,1,3,5,2,8 is not a valid pre-order for a BST.
If we follow the procedure, we get this tree from above given pre-order and in-order
7
/ \
6 8
/
4
/ \
1 5
\
3
/
2
And now, if we find the pre-order by traversing the above tree we get 7,6,4,1,3,2,5,8. This pre-order is correct for a BST. And we also get this pre-order from using post-order and in-order of the above tree.
Why pre-order 7,6,4,1,3,5,2,8 was wrong?
If we look carefully, we are breaking at points 7, 6, 4, 1, 3.
But after 3 it should be 2 not 5, because we are in recursion of root node, left node and right node of a pre-order. We cannot go to breaking at point 5 without breaking at 2 before.
As, 2 and 5 have no child nodes, it gave us a tree, found the post order and now we are in confusion why are we not able to produce the same pre-order again?
It is simple because 7,6,4,1,3,5,2,8 was never the pre-order.
Let me make it more clear if we had numbers 1.5 and 5.5 in the BST. We are in the pre-order recursion and have broken at point 3 to get {2,2.5} on left and {} on right, but how can we move to breaking at 5, without breaking at 2. We are in recursion, remember this.
Moreover, constructing the tree using a pre-order and a sorted in-order, and if we are not able to generate the same pre-order is a method of checking if the given pre-order was a valid pre-order or not.
Check this article : valid pre-order of a BST
Use the given program in link and run it on your pc to check your pre-order is valid or not for a BST.
For example
preorder->0,1,2
postorder->2,1,0
0
/
1
/
2
0
/
1
\
2
0
\
1
/
2
0
\
1
\
2
These are the 4 binary trees possible for above case.How many trees are possible in general for any preorder and postorder traversals?
There are two answers to your question:
For given postorder and preorder traversals of a binary tree, you can only find a single tree. (note the emphasis on binary! I only allow the root to have degree 2)
A proof sketch is given in this answer, where you can also find a reconstruction algorithm.
If your tree is allowed to have inner nodes of degree 2, i.e. nodes with only one child, then you can place it either on the left or on the right side, the corresponding subtree's position in the pre- and postorder traversal won't change. So if you have k such nodes, you have 2^k equivalent trees
I'm given an in-order traversal and need to find a binary tree. I referred my sites and most of them said it is not possible. However, i think a non-unique binary tree is possible. Can i find a binary tree using just given in-order traversal? If not, can i find a corresponding pre-order traversal from the given in-order traversal?
I tried to convert the in-order into pre-order by selecting the central node of in-order as the root but I'm not sure if its correct. Please guide me.
Thank you.
Given just the in-order traversal of the nodes, and no more information in the question, you can find a binary tree, but as you said, there won't be a unique solution (especially if the tree doesn't have to be balanced). As a result, you can again find a pre-order traversal.
As an example, if your in-order traversal is [1,2,3,4,5,6,7,8], then even if the tree is balanced, there are multiple possibilities for the root node (namely, 4 or 5). If the tree doesn't have to be balanced, you could potentially pick any of these as the root node.
Here's an example of a non-balanced tree you could build after arbitrarily choosing 4 as the root node:
4
/ \
3 6
/ / \
2 5 7
/ \
1 8
Pre-order traversal for this tree would yield 4,3,2,1,6,5,7,8. Again, if the only requirements are that you just find a binary tree, this is just as valid as setting 1 as the root node and making everything else a right node:
1
\
2
\
3
\
4
\
5
\
6
\
7
\
8
The pre-order traversal for this tree would be 1,2,3,4,5,6,7,8. Since these trees both generate the same in-order traversal, but different pre-order traversals, there isn't guaranteed to be a single, unique tree or even a single, unique pre-order traversal for a given in-order traversal.
For more of one node there is no a unique binary search tree that contains the inorder traversal.
But if you want a tree, then just perform a random shuffle of inorder sequence and then insert the resulting randomized sequence in a binary search tree
How to check whether two complete binary trees are mirror of each other where only the level order traversal of the trees are given ?
A Complete binary tree is a binary tree which all the nodes except the leaf nodes have 2 child nodes.
I don't think this is possible. Consider these two trees:
0 0
/ \ / \
1 2 1 2
/ \ / \ / \
3 4 3 4 5 6
/ \
5 6
These are complete binary trees (according to your definition), and even though they're different trees they have the same level-order traversals: 0123456.
Now, look at their mirrors:
0 0
/ \ / \
2 1 2 1
/ \ / \ / \
4 3 6 5 4 3
/ \
6 5
Notice that the left tree has level-order traversal 0214365, while the right tree has level-order traversal 0216543. In other words, the original trees have the same level order traversals, but their mirrors have different traversals.
Now, think about what happens if you have your algorithm and you feed in 0123456 (the level-order traversal of either of the trees) and 0214365 (the level-order traversal of one of the mirrors). What can the algorithm say? If it says that they're mirrors, it will be wrong if you fed in the second of the input trees. If it says that they're not mirrors, it will be wrong if you fed in the first of the input trees. Therefore, there's no way for the algorithm to always produce the right answer.
Hope this helps!
According to general definitions, in a complete binary tree, every level except possibly the last, is completely filled, and all nodes are as far left as possible. So a complete binary tree may have a node with just one child (for eg, one root node with one left child is a complete binary tree). A tree where all nodes except the leaves have 2 child nodes is called a full binary tree.
For complete binary trees, the problem would be trivial. Starting from the top-down, for the ith level you need to compare 2^i elements (root being the 0th level) of the given level-order traversals A and B. For any given i, the set of 2^i elements from A should be equal to the reverse of these elements from B. However, the last level may not be completely filled and you'll need to account for that.
For full binary trees, where the only constraint given is that every node has 2 or no children, it won't be possible unless you have constructed the tree itself. And you cannot construct a tree by using only level-order traversal. templatetypedef provides a good example.
Why do we need to compare in-order traversal string AND pre-order traversal string to determine if a tree is a subtree of a bigger tree? Why is 1 traversal string not enough?
Can we replace one of the above strings with the post-order traversal string?
Assume:
I have an unbalanced binary tree. This is NOT necessarily a binary search tree.
Data at inner nodes and leaves can be compared using a given compareTO() method
The tree is huge, thus making use of suffix trees impossible. Assume normal recursion is possible.
Ad1. Let's say you have your bigger tree:
5
/
6 <something>
/ \
7 3
/
2
Now notice that the inorder traversal of the subtree rooted at node 6 will be [7,6,2,3]. Now tell me what will be the inorder traversal of the following tree:
6
/ \
7 2
\
3
You guessed it: [7,6,2,3]. Inorder traversal does not, in general case describe a single binary tree but a number of binary trees. Should this be a binary search tree then you only need 1 preorder or postorder traversal since the inorder traversal will always be the same (all nodes in a sorted order).
Ad2. You can reconstruct a tree with inorder and preorder or postorder traversals. You cannot, in the general case, reconstruct a tree with a preorder and postorder traversals, there might be more than 1 option. The only case where you can do that is for a full binary tree (each node, except the leaves, has to have 2 children).