# prograMing: generating trees

• To: mathgroup@smc.vnet.net
• Subject: [mg10645] prograMing: generating trees
• From: "Xah" <xah@best.com>
• Date: Tue, 27 Jan 1998 03:10:09 -0500
• Organization: Venus & Xah Love Factory

```Here's another recreational programing problem related to trees.

In this message, I'll give an example of a function that does certain
things, and will ask readers to write a similar function for which I
don't have a solution ready. (and am interested to see creativity from

all parts up to positionIndex. Suppose positionIndex is {2,3,1}, then
the result will contain parts having position index of
{0,0,0}<={i,j,k}<={2,3,1}, e.g. {1,3}, {2,2,2}, {2,0,1}...etc. The
option Heads->False will ignore all 0s in positionIndex. 0 is used as
the Atom in the resulting tree.

which is an expression having indexes
{0},{1},{2},{1,0},{1,1},{1,2},{2,0},{2,1}, and {2,2}. Now,
indexes contained in that expression are:

In[29]:=
{{0,0},{0,1},{0,2},{0},{1,0},{1,1},{1,2},{1},{2,0},{2,1},{2,2},{2}}

Here is a recursive and an iterative implementation of TreeGenerator.

---------

Clear[TreeGenerator,TreeGenerator2]; TreeGenerator::"usage"=
having \
all parts up to position index {i1,i2,...}. If Heads->False, all indexes
that \
have value 0 are ignored. Example: \

(*Recursive version*)

(*Iterative version*)

Module[{i=Length@d,g},g=0;
Do[pos=Abs@d[[i]];If[pos===0,g=g[],g=g@@Table[g,{pos}]];--i,{i}];g];

Module[{i=Length@d,g},g=0;
Do[pos=Abs@d[[i]];If[pos===0,Null,g=0@@Table[g,{pos}]];--i,{i}];g];

--------
The following snippet will let you test the versions. Suppose one of the
version is named TreeGenerator2.

Clear[opt];
Table[Random[Integer,{0,3}],{200},{Random[Integer,{0,3}]}])

Notice that TreeGenerator does not generate a minimum tree for a given
positionIndex. For example, given an index {2,2}, TreeGenerator will
generate elements at {1,1} and {1,2}, which are not really necessary
for a tree to have an index at {2,2}.

The challenge is to write a MinimumTreeGenerator with the following
spec:

False)] generates a minimum expression having position index
positionIndex. If Heads->False, all indexes in positionIndex that have
value 0 are ignored.
MinimumTreeGenerator[{positionIndex1,positionIndex2,...},(opt)] returns
a minimum tree having elements at the specified indexes.";

I'm looking for exemplary codes with or without speed considerations.
Have fun!

PS I'll be posting a solution in about a week, if no solutions come up.
This problem seems to be a good candidate as a snippet in the
Mathematica Journal.

Xah, xah@best.com
http://www.best.com/~xah/Wallpaper_dir/c0_WallPaper.html
Perl side effects: make one assignment, the next thing you know your
house is deleted.

```

• Prev by Date: Importing HTML files into MATHEMATICA
• Next by Date: A bug in DesignedRegress?
• Prev by thread: Importing HTML files into MATHEMATICA
• Next by thread: A bug in DesignedRegress?