Anonymous user
List rooted trees: Difference between revisions
→{{header|Mathematica}} / {{header|Wolfram Language}}: changed to local variable for n, removed ClearAll, and offered method to output as a tree graph.
No edit summary |
(→{{header|Mathematica}} / {{header|Wolfram Language}}: changed to local variable for n, removed ClearAll, and offered method to output as a tree graph.) |
||
Line 1,253:
=={{header|Mathematica}} / {{header|Wolfram Language}}==
The following defines functions which create a nest of functions, bags wrapped inside a main bag which are stored inside a "cabinet".
<lang Mathematica>Addbags[configs_List] :=
DeleteDuplicates[
Addbags[configs_List] := DeleteDuplicates[Map[LexicographicSort, Catenate[AddbagAll /@ configs], \[Infinity]]]▼
Map[LexicographicSort,
AddbagAll[config_] := Addbag[config, #] & /@ Position[config, bag[___], \[Infinity]]▼
▲
Addbag[config_, pos_] := ReplacePart[config, pos -> Append[Extract[config, pos], bag[]]]▼
AddbagAll[config_] :=
Nest[Addbags, {cabinet[bag[]]}, n - 1] // Column</lang>▼
Addbag[config_, pos_] :=
▲With[{n = 5}, Nest[Addbags, {cabinet[bag[]]}, n - 1] // Column]</lang>
The output can be viewed as a tree graph by replacing the last line with <Lang Mathematica>With[{n = 5}, TreeForm /@ Nest[Addbags, {cabinet[bag[]]}, n - 1]]</lang>
{{out}}
<pre>cabinet[bag[bag[bag[bag[bag[]]]]]]
|