简体   繁体   English

如何从叶节点的一组路径片段构建一棵树

[英]How to build a tree from a set of path fragments for leaf nodes

I am looking for an algorithm to construct a possible top down tree structure when the only input data is path decisions (in random order) for the leaf nodes.当唯一的输入数据是叶节点的路径决策(以随机顺序)时,我正在寻找一种算法来构建可能的自上而下的树结构。

The path decisions is notaded with [nodeID]+ if the leaf node has passed through that node and [nodeID]- if the leaf node has not passed through that node.如果叶节点已通过该节点,则路径决策用 [nodeID]+ 标记,如果叶节点未通过该节点,则用 [nodeID]- 标记。 Example input:示例输入:

在此处输入图片说明

One possible tree structure from the example input above would be:来自上面示例输入的一种可能的树结构是:

在此处输入图片说明

The output should be a list of nodes and the node respective parent, like this:输出应该是节点列表和节点各自的父节点,如下所示:

在此处输入图片说明

As you can see the possible positions for each leaf node is not limited to one, since the input data is not complete paths.如您所见,每个叶节点的可能位置不限于一个,因为输入数据不是完整路径。 Leaf1 in the example can be a child of node D or node I.示例中的 Leaf1 可以是节点 D 或节点 I 的子节点。

There can be any number of leafs you get path data from, but only one single line of path data per leaf.您可以从任意数量的叶子中获取路径数据,但每个叶子只有一行路径数据。 There can be leafs you get no data from at all and you are not aware of how many total leafs there is in the tree.可能有些叶子根本无法获得任何数据,并且您不知道树中总共有多少叶子。 All nodes mentioned in path data should be present in the output table and only the calculated root shouldn't have any parent assigned.路径数据中提到的所有节点都应该出现在输出表中,并且只有计算出的根不应该分配任何父节点。

I guess one must somehow combine the facts from each Leaf paths, like from Leaf1 you know: A and E must not be in parallell lines, and from Leaf2 you know that A and I must not be in parallell lines, and so on ...我想人们必须以某种方式结合来自每个叶子路径的事实,例如从您知道的 Leaf1 中:A 和 E 不能在平行线上,从 Leaf2 中您知道 A 和 I 不能在平行线上,依此类推.. .

Preferable javascript but other language or pseudo code is also welcomed!最好使用 javascript,但也欢迎其他语言或伪代码!

Since nobody answered, I'll give some partial thoughts.既然没有人回答,我就说一些片面的想法。

You start with:你开始:

1: [A+, E+, H-]
2: [B-, A+, I+, D-]
3: [K+, G+, E+, C-]
4: [H+, A-, G-]

First, search for nodes that only appear with a +.首先,搜索仅带有 + 的节点。 Make those roots.做那些根。 Scanning this, we can do that for nodes E , I , and K .扫描这个,我们可以对节点EIK So our answer starts with:所以我们的回答开始于:

E
E I
I K

And our path data is simplified to:我们的路径数据简化为:

1: [A+, H-]
2: [B-, A+, D-]
3: [G+, C-]
4: [H+, A-, G-]

And now we have a partition operation.现在我们有一个分区操作。 For this we make a graph where 2 nodes are connected iff they appear together with a +.为此,我们制作了一个图,其中 2 个节点连接,如果它们与 + 一起出现。 We then separate nodes by connected components, and separate leaves into partitions where there is a + (any leaf without a partition can be made a leaf right there in the tree and then dropped).然后我们通过连接的组件将节点分开,并将叶子分成有 + 的分区(任何没有分区的叶子都可以在树中成为叶子然后丢弃)。 Deleting any - that is taken care of by this separation.删除任何 - 由这种分离处理的。 In our case this is a totally disconnected graph which puts each node into its own partition and divides the path data into 3 groups (note, we lose all - rules which are explained by the partitioning):在我们的例子中,这是一个完全断开的图,它将每个节点放入自己的分区并将路径数据分为 3 组(注意,我们丢失了所有 - 分区解释的规则):

1: [A+]
2: [A+]

3: [G+]

4: [H+]

And now we solve each problem, coming to a final solution of:现在我们解决了每个问题,最终得出了以下解决方案:

E
E I
I K
K A
K B
K C
K D
K G
K H

I believe that this approach will only fail to make progress in the case where no tree is possible.我相信这种方法只会在没有树的情况下无法取得进展。 If there is a tree to be found, it will find one.如果要找到一棵树,它会找到一棵树。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM