Tree operators.

This load the graph package (for drawing and computing with graph).

In[196]:=

<<DiscreteMath`Combinatorica`

We define a constructor for a leaf, for unary and binary trees.

In[197]:=

Lf = EmptyGraph[1] ;

In[198]:=

MK1[g1_] := AddEdges[GraphUnion[Lf, g1], {1, 2}] ; MK2[g1_, g2_] := AddEdges[GraphUnion[Lf, g1, g2], {{1, 2}, {1, V[g1] + 2}}]

Some examples of trees built with Leaf, MK1, MK2. We first apply MK2 to two complete graphs of 2,3 points.

In[200]:=

h = MK2[CompleteGraph[2], CompleteGraph[3]]

Out[200]=

⁃Graph:<6, 6, Undirected>⁃

In[201]:=

ShowGraph[h]

[Graphics:../HTMLFiles/index_182.gif]

Out[201]=

⁃Graphics⁃

We draw a single leaf.

In[202]:=

ShowGraph[Lf]

[Graphics:../HTMLFiles/index_185.gif]

Out[202]=

⁃Graphics⁃

We use the first node of the tree as root for the tree.

In[203]:=

g = RootedEmbedding[MK2[MK2[Lf, Lf], MK1[MK2[Lf, Lf]]], 1] ;

In[204]:=

ShowGraph[g, VertexNumberTrue]

[Graphics:../HTMLFiles/index_189.gif]

Out[204]=

⁃Graphics⁃

We define the height of a tree rooted in 1 as the maximum distance between the node 1 and any node.

In[205]:=

Height[g_] := Max[Distances[g, 1]] ;

In[206]:=

Height[g]

Out[206]=

3

We define a map, Add, taking a tree g, an integer i, and adding to the point number i of the tree one new child.

In[207]:=

Add[g_, i_] := If[i0, Lf, RootedEmbedding[AddEdge[GraphUnion[g, Lf],  {i, V[g] + 1}], 1]]

When the point to which we add a child is unspecified, then it is the last point of the graph (vertices of each graph are numbered 1, ..., n). ``Add'' applied to the empty graph yields a leaf.

In[208]:=

Add[g_] := Add[g, V[g]] ;

In[209]:=

ShowGraph[Add[g]]

[Graphics:../HTMLFiles/index_197.gif]

Out[209]=

⁃Graphics⁃

An example of the use of Add.

In[210]:=

k = Add[Add[Add[Lf]]] ;

In[211]:=

ShowGraph[k]

[Graphics:../HTMLFiles/index_201.gif]

Out[211]=

⁃Graphics⁃


Created by Mathematica  (October 17, 2006)