创建具有独特排列的所有二叉树 [英] Creating all binary trees with unique permutations
问题描述
我有我发誓不做作业一个相当愚蠢的问题。对于我的生活,我不记得我是否学过一个算法来做到这一点,我的心灵之眼/创意失败了我。
我有独特的名单节点。我需要生成含有这些节点的二进制树的所有独特排列。手性,如果你想知道,事项;二叉树翻转在它的轴(左/右)是不一样的。
一些背景资料,如果你想知道:这是对的进化程序的种子创造的算法,所以大量微小的种子是好的。
编辑:澄清独特
例子:
本:
1
/ \
2 3
是不一样的:
1
/ \
3 2
也不是一样的:
1
/
$ 3 b $ b /
2
这也不:
1
\
2
\
3
埃里克利珀具有相关岗位的此处(实际上是开始一系列帖子的)。键位是这样的递归方法LINQ:
静态的IEnumerable<节点> AllBinaryTrees(INT大小)
{
如果(大小== 0)
返回新节点[] {}无效;从我在Enumerable.Range
返回(0,大小)从AllBinaryTrees在AllBinaryTrees(我)
左右(大小 - 1 - I)
选择新的节点(左右);
}
这获得的的一个所有可能的二叉树的结构给定大小。
我认为,如果你把(a)您的节点列表,以及(b)埃里克的所有树形结构列表的所有排列,并执行两者的交叉产品(你指定你的置换节点到节点结构中的<击>左到右击>在一些一致的顺序),你应该得到你想要的所有树木。
例如: 3项
置换树结构
123 132。 。 。 。 。
213 231 ./ ./ ./ \。 \。 \。
312 321 ./ \。 ./ \。
结果
:1 1 1 1 1 1 1 1 1 1
:2/2/2 / \3 \2 \2 3/3/3 / \2 \3 \3
:3 / \3 3 / \3 2 / \2 2 / \2
:2 2 2 2 2 2 2 2 2 2
:1/1/1 / \3 \1 \1 3/3/3 / \1 \3 \3
:3 / \3 3 / \3 1 / \1 1 / \1
:3 3 3 3 3 3 3 3 3 3
:1/1/1 / \2 \1 \\ \\1 2/2/2 / \1 \2 \2
:2 / \2 2 / \2 1 / \1 1 / \1
这将更难,如果你不关心手性。
(生成的输入节点的排列,并Eric的结构之一分配排列,应该是相当微不足道的,对吧?)
I have a rather silly question which I swear is not homework. For the life of me, I cannot remember whether I ever studied an algorithm to do this, and my mind's eye / creativity is failing me.
I have a list of unique nodes. I need to generate all unique permutations of a binary tree containing these nodes. Chirality, in case you are wondering, matters; a binary tree flipped on its axis (left / right) is not the same.
Some background information, in case you are wondering: it's for a seed creation algorithm for an evolutionary program, so a large number of tiny seeds is fine.
Edit: Clarification of uniqueness
Examples:
This:
1
/ \
2 3
Is not the same as this:
1
/ \
3 2
Nor is it the same as this:
1
/
3
/
2
Nor this:
1
\
2
\
3
Eric Lippert has a relevant post here (actually the start of a series of posts). The key bit is this recursive LINQ method:
static IEnumerable<Node> AllBinaryTrees(int size)
{
if (size == 0)
return new Node[] { null };
return from i in Enumerable.Range(0, size)
from left in AllBinaryTrees(i)
from right in AllBinaryTrees(size - 1 - i)
select new Node(left, right);
}
This gets all possible binary tree structures of a given size.
I think that if you take (a) all permutations of your list of nodes, and (b) Eric's list of all tree structures, and perform a "cross product" of the two (where you assign your permuted nodes to the nodes in a structure left-to-rightin some consistent order) you should get all the trees you want.
E.g. for 3 items
Permutations Tree structures
123 132 . . . . .
213 231 ./ ./ ./ \. \. \.
312 321 ./ \. ./ \.
Result
: 1 1 1 1 1 1 1 1 1 1
: 2/ 2/ 2/ \3 \2 \2 3/ 3/ 3/ \2 \3 \3
: 3/ \3 3/ \3 2/ \2 2/ \2
: 2 2 2 2 2 2 2 2 2 2
: 1/ 1/ 1/ \3 \1 \1 3/ 3/ 3/ \1 \3 \3
: 3/ \3 3/ \3 1/ \1 1/ \1
: 3 3 3 3 3 3 3 3 3 3
: 1/ 1/ 1/ \2 \1 \1 2/ 2/ 2/ \1 \2 \2
: 2/ \2 2/ \2 1/ \1 1/ \1
This would be harder if you didn't care about chirality.
(Generating permutations of your input nodes, and assigning a permutation to one of Eric's structures, should be fairly trivial... right?)
这篇关于创建具有独特排列的所有二叉树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!