问题
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.
回答1:
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
回答2:
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.
回答3:
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!
回答4:
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.
回答5:
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.
回答6:
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.
回答7:
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
来源:https://stackoverflow.com/questions/1136999/reconstructing-a-tree-from-its-preorder-and-postorder-lists