What are the inorder postorder preorder of this binary tree? - binary-tree

I am trying to find a binary tree which all of its keys are different but the three traversals inorder postorder inorder are the same so i thought of this tree:
1
\
2
\
3
\
4
So basically there are no left sons
But i am not sure if that would lead to similar three traversals
So is postorder=inorder=preorder = 1234 ?

Post-order means that a parent is always visited after ("post") its children, so the output 1, 2, 3, 4 violates that rule. Post-order for that tree will be 4, 3, 2, 1.
Pre-order means that a parent is always visited before ("pre") its children, so then the output is as you have it: 1, 2, 3, 4.
In-order means that a parent is always visited after its left child and before its right child -- coming "in" between them. As there are no left children in your example input, the in-order traversal is the same as the pre-order traversal.

Related

Uniqueness of Inorder, Preorder, and Postorder traversal with null elements

We all know that different binary trees can have the same inorder, preorder, or postorder traversal. But if we were to include null elements into a preorder traversal, then result of the traversal would be unique as long as the trees are unique. Consider these two trees:
3 3
/ \
4 vs. 4
Their normal preorder traversal would be {3,4} for both, but if we were to include null elements, then their traversal would be {3,4,null,null,null} and {3,null,4,null,null} respectively, making the traversal unique.
My question is, would this be true for inorder and postorder traversals as well? And how can we prove it?
It is true for a postorder traversal, because that's just the reverse of the preorder traversal of the reversed tree.
It is NOT true for an inorder traversal, which would always start with null, end with null, and alternate between nulls and nodes.
E.g., these trees:
B D
/ \ / \
A D B E
/ \ / \
C E A C
both produce
null, A, null, B, null, C, null, D, null, E, null
The inorder is not right.
e.g.:
2 2
/ \ / \
2 n n 2
/ \ / \
n n n n
They are both [n, 2, n, 2, n]
Preorder and postorder are right because with 2 null, we can determine a parent, and this parent must be a child of other node, and so on. We can always determine a unique parent node. But this is not true for inorder.

Preorder and relation to Inorder Traversal

I find that if we have Preorder and Inorder Traversal, we have a unique tree.
can i conclude:
For Each Preorder Traversal, we have multiple Inorder Traversal. this is True or False Conclusion? every one would help me and add some detail.
thanks again.
Yes because from a single preorder sequence you can create multiple trees. For instance: [4,3,1, 2] can be repesented as a tree with root 4, children 3 and 2. Then you can insert 1 as either left or right child of 3. Depending on where you will insert it the inorder sequence will change.
You can also reason about it in this way: you have n numbers, with them you can get n! permutations. Generating a tree from your traversal would be possible if the number of permutations was equal to the number of possible trees you can create usin those n numbers. This is not the case, though as you can for instance create many trees, where every node has only a left child or only a right child, this gives you 2*n! and there are more so there has to be a permutation that can generate more than 1 tree => more than 1 inorder traversal.
This is of course true in general case, BSTs have a unique inorder sequence.
True,
for example:
given a preorder traversal
abdec
a
/ \
b c
/ \
d e
many inorder traversals are possible:
baedc,
dbeac
and so on...

Inorder starts from the "leftmost left node" so why doesn't Preorder start from "leftmost middle node"?

Consider this tree:
7
/ \
/ \
/ \
1 9
/ \ / \
0 3 8 10
/ \
2 5
/ \
4 6
Inorder:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Preorder:
7, 1, 0, 3, 2, 5, 4, 6, 9, 8, 10
While doing Inorder traversal, the leftmost left node is first located and the traversal starts from there. But when it comes to Preorder the same logic(as in leftmost middle node) is not applied
In the above tree apart from the root 7, there is 1 and 9 which are both middle nodes. 1 being the leftmost middle node and 9 being the rightmost middle node. Going by the logic applied for above InOrder, the Preorder traversal should have begun from the node 1 which is the leftmost middle node, but its not so, why?
Why is it that in Inorder the traversal starts from the leftmost left node but the PreOrder traversal does not start from leftmost middle node?
Thanks,
Chris.
Preorder always puts the parent before its descendants (that is its definition), hence it has to start with the root. You could use the term "midllemost middle node" for the root if you like.
A typical use of preorder is the standard function notation: If you have something like
f(g(x, h(y, z)))
then this is a preorder notation of the following expression tree which uses the function name for inner nodes and the variables as leave nodes:
f
|
g
/ \
x h
/ \
y z
On the other hand, the usual notation with operators like + and * uses an inorder:
a + b * c
is the inorder notation for
+
/ \
a *
/ \
b c
if we use the standard mathematical precedence rules that * is binding stronger than +.
And writing expressions in reverse polish notation would be an example of postorder.
Inorder: Traverse Left subtree; Visit Current Node; Traverse Right subtree;
Preorder: Visit Current Node; Traverse Left subtree; Traverse Right subtree;
Postorder: Traverse Left subtree; Traverse Right subtree; Visit Current Node;
Note that you'll never have the same node in Inorder and Preorder unless the left subtree is missing.
However you can have the same node in Inorder and PostOrder representations if there is a left subtree.

AVL tree with a sorted list?

After reading about AVL trees I can't get one question out of my head.
If we have a sorted list of numbers, e.g. [1,2,3,4,5] and we insert them into the AVL tree, wouldn't the tree stay unblanaced because it will go 1-2-3-4-5 (i.e. they will all become right child).
I am asking this because I know that in AVL tree for every internal node v of T, the heights of the children of v can differ by at most 1.
But if we have only 1 child for each node, how can we do this comparison?
An empty tree has height 0, so in your example after adding 1-2-3 the left child of 1 had height 0 and the right had 2, triggering a rotation that makes 2 the root.

reconstructing a tree from its preorder and postorder lists

Consider the situation where you have two lists of nodes of which all you know is that one is a representation of a preorder traversal of some tree and the other a representation of a postorder traversal of the same tree.
I believe it is possible to reconstruct the tree exactly from these two lists, and I think I have an algorithm to do it, but have not proven it. As this will be a part of a masters project I need to be absolutely certain that it is possible and correct (Mathematically proven). However it will not be the focus of the project, so I was wondering if there is a source out there (i.e. paper or book) I could quote for the proof. (Maybe in TAOCP? anybody know the section possibly?)
In short, I need a proven algorithm in a quotable resource that reconstructs a tree from its pre and post order traversals.
Note: The tree in question will probably not be binary, or balanced, or anything that would make it too easy.
Note2: Using only the preorder or the postorder list would be even better, but I do not think it is possible.
Note3: A node can have any amount of children.
Note4: I only care about the order of siblings. Left or right does not matter when there is only one child.
Preorder and postorder do not uniquely define a tree.
In general, a single tree traversal does not uniquely define the
structure of the tree. For example, as we have seen, for both
the following trees, an inorder traversal yields [1,2,3,4,5,6].
4 3
/ \ / \
2 5 2 5
/ \ \ / / \
1 3 6 1 4 6
The same ambiguity is present for preorder and postorder
traversals. The preorder traversal for the first tree above is
[4,2,1,3,5,6]. Here is a different tree with the same preorder
traversal.
4
/ \
2 1
/ \
3 6
\
5
Similarly, we can easily construct another tree whose postorder
traversal [1,3,2,6,5,4] matches that of the first tree above.
You cannot use only one list, because you'll get no sense of the depth of the tree. Thus, you definitely require two or more lists.
Here's my attempt at a solution:
Use your preorder traversal as a means of knowing the ordering of the data. This makes sense because you know the first node is the top, and you know that data further to the left of the traversal belongs to the left of the tree, etc.
Your post order traversal can determine the depth of the tree. For example, let's say I have a structure like this:
1
2 5 6
3 4 7
Where 2 is the parent of 3 and 4, and 5 is the parent of 7.
Preorder: 1 2 3 4 5 7 6
Postorder: 3 4 2 7 5 6 1
We know we start with 1, because it is the first node in the preorder traversal. Then we look at the next number, 2. In the post order, because the number 2 comes BEFORE node 1, we know that 2 has to be a child of 1. Next we look at 3. 3 comes before 2, and thus 3 is a child of 2. 4 is before 2 but after 3, so we know 4 is a child of 2 but NOT a child of 3. Etc.
Now, this may not work if the nodes are not unique, but at the very least its a start to a solution.
Edit: The order of the children is preserved with this solution, simply due to knowing the ordering of the nodes via the preorder traversal, and then knowing the structure via the postorder traversal.
Edit2: The proof may lie here: http://ieeexplore.ieee.org/Xplore/login.jsp?url=http%3A%2F%2Fieeexplore.ieee.org%2Fiel2%2F215%2F626%2F00017225.pdf%3Farnumber%3D17225&authDecision=-203
I think you need to purchase the document, however...
Here is a written proof presented to be a solution:
http://www14.informatik.tu-muenchen.de/lehre/2007WS/fa-cse/tutorials/tutorial09-solutions.pdf
Consider an arbitrary tree T as the quadruple (A, B, C, D), where A is the root node, B is the root node of the first child, C is a vector of any non-empty children of B, and D is a vector of any non-empty siblings of B. The elements of C and D are themselves trees.
Any of A, B, C and D may be empty. If B is empty, so must be C and D; if A, then everything.
Since nodes are unique, the sets of nodes contained anywhere within C and D are disjoint, and neither contains A or B.
Functions pre() and post() generate ordered sequences of the form:
pre(T) = [A, B, pre(C), pre(D)]
post(T) = [post(C), B, post(D), A]
where the function applied to a vector is defined to be the concatenation of the sequences resulting from applying the function to each element in turn.
Now consider the cases:
if A is empty, the output of both functions is the empty sequence []
if B is empty, the output of both functions is just [A]
if C and D are empty, pre(T) = [A, B] and post(T) = [B, A]
if just C is empty, pre(T) = [A, B, D'] and post(T) = [B, D'', A] (where the primes indicate some permutation of the nodes contained within D)
if just D is empty, pre(T) = [A, B, C'] and post(T) = [C'', B, A]
if none are empty, pre(T) = [A, B, C', D'] and post(T) = [C'', B, D'', A]
In all cases we can unambiguously partition the members of the two output sequences into the appropriate subsequences, by using A and B (if present) as delimiters.
The question then is, can we also partition the vector sequences? If we can, then each can be recursively processed and we're done.
Since the result of pre() will always be a chain of sequences starting with A nodes, and the result of post() will always be a chain of sequences ending with A nodes, we can indeed divide them up, provided that the A nodes are never empty.
This is where the process falls down in the case of binary (or indeed any) trees with fixed children that may independently be empty. In our case, however, we have defined C and D to contain only non-empty nodes, and so the reconstruction is guaranteed to work.
Um, I think so, anyway. Obviously this is just an argument, not a formal proof!
Create a binary tree with this restriction that has at least one node that this node has only one child(right or left ,there is no difference).
Now, write its Preorder and Postorder lists. then try to reconstructing the tree from these lists. and you realize that on that node you cannot decide that its child is right or left.
The preorder and postorder traversals are sufficient to reconstruct the tree, assuming the nodes are uniquely named. The key to creating the algorithms to do so is to understand that
X is an ancestor of Y iff X precedes Y in the preorder and is after Y in the postorder.
Given this, we can always find all the descendants of any node. The descendants of X always immediately follow X in the preorder, and precede X in the postorder. So once we know we're interested in producing the subtree rooted at X, we can extract the preorder and postorder traversal for the subtree rooted at X. This leads naturally to a recursive algorithm, once we realize that the node immediately after X must be its leftmost child, if it is a descendant at all.
There is also a stack-based implementation, which iterates through the preorder nodes, and keeps on the stack any nodes which are candidates to be the direct parent of the next preorder node. For each preorder node, repeatedly pop all nodes off the stack which are not parents of the next preorder node. Make that node a child of the top node on the stack, and push the child onto the stack.
As already pointed out by others, a binary tree can not be reconstructed by using only pre and post order traversal. A single child node has ambiguous traversals that cannot identify whether it is left or right child e.g. consider following preorder and postorder traversals:
preorder: a,b
postorder b,a
It can produce both of the following trees
a a
\ /
b b
It is simply not possible to know if b is a's left or right child without any additional information like inorder traversal.
It is not possible to construct a general Binary Tree from preorder and postorder traversals (See this). But if know that the Binary Tree is Full, we can construct the tree without ambiguity. Let us understand this with the help of following example.
Let us consider the two given arrays as pre[] = {1, 2, 4, 8, 9, 5, 3, 6, 7} and post[] = {8, 9, 4, 5, 2, 6, 7, 3, 1};
In pre[], the leftmost element is root of tree. Since the tree is full and array size is more than 1. The value next to 1 in pre[], must be left child of root. So we know 1 is root and 2 is left child. How to find the all nodes in left subtree? We know 2 is root of all nodes in left subtree. All nodes before 2 in post[] must be in left subtree. Now we know 1 is root, elements {8, 9, 4, 5, 2} are in left subtree, and the elements {6, 7, 3} are in right subtree.
1
/ \
/ \
{8, 9, 4, 5, 2} {6, 7, 3}
We recursively follow the above approach and get the following tree.
1
/ \
2 3
/ \ / \
4 5 6 7
/ \
8 9

Resources