How to enumerate all possible binary associations?
I propose a more compact approach
f[list__] := Join @@ ReplaceList[{list}, {x__, y__} :> Tuples@{f[x], f[y]}]
f[x_] := {x};
f[a, b, c, d] // Column
{a,{b,{c,d}}} {a,{{b,c},d}} {{a,b},{c,d}} {{a,{b,c}},d} {{{a,b},c},d}
One can note that the length of this list is the Catalan number
$$ C_n = \frac{1}{1+n}{2n\choose n} $$
Length[f @@ ConstantArray[a, 6]]
CatalanNumber[6 - 1]
WolframAlpha["answer to life the universe and everything"]
42 42 42
I think one way is to do your ReplaceList
repeatedly, until the result doesn't change any more.
FixedPoint[
DeleteDuplicates[Flatten[
Function[lst,
If[# === {}, {lst}, #] &[
ReplaceList[lst,
{u___, v_, w_, x___} /;
Nand[{u} === {}, {x} === {}] :>
{u, {v, w}, x}]
]
] /@ #,
1]] &,
{Range[5]}
];
TreeForm /@ %
How about a recursive approach?
ClearAll[a, b, c, d, func];
set = {a, b, c, d};
counter = 0;
rules = {};
func[{x_}] := x;
func[list_] := Module[{r}, DeleteDuplicates@Flatten[func /@
ReplaceList[list, {a___, x_, y_, b___} :> {a, {x, y} /.
rules /. {x, y} :> (r = RandomReal[]; PrependTo[rules, {x, y} -> r]; r), b}],
1]];
temp = func@set;
Fold[ReplaceAll, temp, Reverse /@ rules]
{ {{{a, b}, c}, d}, {{a, b}, {c, d}}, {{a, {b, c}}, d}, {a, {{b, c}, d}}, {a, {b, {c, d}}} }
Update
Made it faster. Random reals are generated to denote parental nodes. There is an infinitesimal chance that a set
of random reals might interfere with generated node-identifiers.