从preorder和后序表重建一棵树 [英] reconstructing a tree from its preorder and postorder lists

查看:206
本文介绍了从preorder和后序表重建一棵树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑,你有节点的两个列表中的情况,其中所有你知道的是,一个是一些树的preorder遍历和后序的另一重presentation的重新presentation遍历同一棵树的。

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.

我相信这是可能从这些两个列表完全重建的树,我觉得我有一个算法来做到这一点,但还没有证明它。由于这将是一个硕士项目,我需要绝对肯定,这是可能的,正确的一部分(从数学上证明)。但它不会是该项目的重点,所以我想知道是否有在那里源(即纸或书),我可以引述的证据。 (也许在TAOCP?有人知道的部分有可能?)

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?)

总之,我需要一个成熟的算法,在重建一棵树从pre和后序遍历一个可引用的资源。

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.

注2:只使用preorder或后序列表会更好,但我不认为这是可能的。

Note2: Using only the preorder or the postorder list would be even better, but I do not think it is possible.

注3:一个节点可以有孩子的任何金额

Note3: A node can have any amount of children.

注4:我只关心兄弟姐妹的顺序。左或右无关紧要,只有一个孩子。

Note4: I only care about the order of siblings. Left or right does not matter when there is only one child.

推荐答案

您不能只用一个列表,因为你会得到没有意义的树的深度。因此,你肯定需要两个或多个列表。

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:

使用您的preorder遍历与了解的数据排序的一种手段。这是有意义的,因为你知道第一个节点是顶部,并且知道该数据进一步遍历的左侧属于树等的左

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

我们知道,我们从1开始,因为它是在preorder遍历的第一个节点。然后我们来看看下一个号码,2。在后顺序,因为2号到来之前节点1,我们知道,2已是1。接下来一个孩子,我们来看看3.3自带2之前,因而3是2 4个孩子是2,但前3后,所以我们知道4是2个孩子,但不是的3个孩子等。

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.

编辑:孩子们的顺序是preserved与此解决方案,只是由于知道节点的顺序通过preorder遍历,然后通过了解结构后序遍历。

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:的证明可以躺在这里:<一href="http://ieeexplore.ieee.org/Xplore/login.jsp?url=http%3A%2F%2Fieeexplore.ieee.org%2Fiel2%2F215%2F626%2F00017225.pdf%3Farnumber%3D17225&authDecision=-203">http://ieeexplore.ieee.org/Xplore/login.jsp?url=http%3A%2F%2Fieeexplore.ieee.org%2Fiel2%2F215%2F626%2F00017225.pdf%3Farnumber%3D17225&authDecision=-203

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...

下面是psented是一个解决方案的书面证明$ P $:

Here is a written proof presented to be a solution:

<一个href="http://www14.informatik.tu-muenchen.de/lehre/2007WS/fa-cse/tutorials/tutorial09-solutions.pdf">http://www14.informatik.tu-muenchen.de/lehre/2007WS/fa-cse/tutorials/tutorial09-solutions.pdf

这篇关于从preorder和后序表重建一棵树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆