从项目组合列表创建树
我有 n 个列表 A,B,C... 其中包含 a,b,c... 元素。我使用它们创建可能组合的列表(列表列表),这样第一个元素取自表 A,第二个元素取自 B 等。将这种平面结构转换为根跟随的树的最佳方法是什么由列表 A 的元素组成,每个元素都具有列表 B 的所有元素等。从而以从根开始的路径形式创建每种可能的组合?
I have n lists A,B,C... which contain a,b,c... elements. I'm using them to create a lists of possible combinations (list of lists), such that first element is taken from table A, second from B etc. What is the best way to transform this flat structure to a tree which root is followed by elements of list A, each having all elements of list B etc. thus creating every possible combination in a form of a path from the root?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
算法
1:从输入开始
因此,如果您有列表
[1, 2, 3]
、[4, 5, 6]
、[7, 8 , 9]
你有这个可能的排列列表所以我现在会浏览列表及其元素。所有代表你想要的树的路径。因此,如果所有内容都放入树中,我将能够找到节点
1
,然后移动到4
,然后找到7
> 在第三级。如果我不这样做,我必须在那里添加它。2:回到原始列表
如果树像看起来一样对称,您也可以只对树的级别进行切片,得到列表 A、B 和 C。然后,您回到第一个方并可以构建树:
它遍历列表堆(a、b、c)的每一层,并将唯一成员存储在列表中。然后它有列表 A、B、C 并从中生成树。
结果
这是我得到的树,0是人工根。这些节点被“回收”,因为它们都具有相同的内容。 (使用 dot 制作。)
http://wstaw.org/m/2011/10/11/tree.png
Algorithms
1: Going from the input
So if you have the lists
[1, 2, 3]
,[4, 5, 6]
,[7, 8, 9]
you have this list of possible permutiationsSo I would now go through the lists and their elements. The all represent paths of the tree you want. So if everything was put into the tree, I would be able to find a node
1
, then move on to a4
and then find a7
at the third level. If I do not, I have to add this there.2: Back to the original lists
If the tree is as symmetric as it seems, you could also just slice the levels of the trees, giving you the list A, B and C. Then, you are back to square one and can build up the tree:
It goes through each layer of the pile of lists (a, b, c) and stores the unique members in a list. Then it has the lists A, B, C and generates the tree out of it.
Result
This is the tree I get, 0 is the artificial root. The nodes are "recycled" since they all bear the same content. (Made with dot.)
http://wstaw.org/m/2011/10/11/tree.png