c# - Creating all binary trees with unique permutations -
I have a silly question that I swear is not homework for my life, I do not remember that I have ever To do this, we have studied an algorithm, and the eye / creativity of my mind is failing me.
I have a list of unique nodes I have to generate all the unique changes in the binary tree with these nodes. Chirality, if you are thinking, matters; A binary tree flipped on its axis (left / right) is not the same
Some background information, if you are thinking: This is for the evolutionary program for seed production algorithm, A large number of is OK.
Edit: Explanation of Specification
Example: it: 1 / \ 2 3 Not so: 1 / \ 3 2 1 / 2/3/2>> Eric Lepert has a relevant post (Actually the beginning of the series of posts) The main bit is the recursive LINQ method:
Fixed IEnumerable & lt; Node & gt; AllBinaryTrees (integer size) {if (size == 0) returns the new node [] {null}; Select the new node in AllBinaryTrees (size - 1 - i) from AllBinaryTrees (i) with ring (0, size) range from Ring in a Bunion (left, right); }
It becomes all possible binary tree structures of a given size.
I think if you list all the permutations (A) of your nodes, and (b) list of all the tree structures of Eric, and the "cross product" of two (where you have a In the structure allocate nodes to their ordering nodes left to right in some consistent order) You should meet all those trees you want.
For example 3 items
scrape tree structures 123 132. . . . 213 231 .///// \. \. 312 321 ./ \. ./ \. Results: 1 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 1 2 1 2 3 1 1 3/3/3 1/3 3 3/3 3/3 3/1 1/1: 3 3 3 3 3 3 3 3 3 3: 1 / 1/1 / \ 2 \ 1 \ 1 2/2/2 / 2/1 \ 2 \ 2: 2/2 2 / \ 2 1 / \ 1 1 / \ 1 \ / code>
It would be difficult if you did not care about charity.
(To change the order of the change in the order of its input nodes, and assigning one of Eric's structures, it should be quite trivial ... correct?)
Comments
Post a Comment