Function Repository Resource:

CompleteLevelsKaryTree

Source Notebook

Compute the complete k-ary tree with different amounts of branches per level

Contributed by: Sander Huisman

ResourceFunction["CompleteLevelsKaryTree"][{n1,n2,n3,}]

gives the complete k-ary tree with ni branches at level i.

Details and Options

ResourceFunction["CompleteLevelsKaryTree"][…, DirectedEdges True] gives a directed complete tree.

Examples

Basic Examples (1) 

Create a k-ary tree with two branches on the top level, three branches in the level below, and four in the bottom level:

In[1]:=
ResourceFunction["CompleteLevelsKaryTree"][{2, 3, 4}]
Out[1]=

Scope (2) 

There can be one branch for a certain level:

In[2]:=
ResourceFunction["CompleteLevelsKaryTree"][{3, 2, 1, 2}]
Out[2]=

Use directed edges:

In[3]:=
ResourceFunction["CompleteLevelsKaryTree"][{3, 2, 1, 2}, DirectedEdges -> True]
Out[3]=

Options (2) 

DirectedEdges (2) 

By default, an undirected graph is generated:

In[4]:=
ResourceFunction["CompleteLevelsKaryTree"][{2, 3}]
Out[4]=

Use DirectedEdges True to generate a directed graph:

In[5]:=
ResourceFunction["CompleteLevelsKaryTree"][{2, 3}, DirectedEdges -> True]
Out[5]=

Properties and Relations (1) 

CompleteKaryTree and CompleteLevelsKaryTree can be the same:

In[6]:=
Column[{CompleteKaryTree[5, 2], ResourceFunction["CompleteLevelsKaryTree"][{2, 2, 2, 2}]}]
Out[6]=

Possible Issues (1) 

If a zero is present the branching is stopped:

In[7]:=
ResourceFunction["CompleteLevelsKaryTree"][{2, 3, 0, 2}]
Out[7]=

Neat Examples (2) 

Here is a complete k-ary tree:

In[8]:=
ResourceFunction["CompleteLevelsKaryTree"][{5, 4, 3, 2}]
Out[8]=

Using the radial embedding emphasizes the symmetry:

In[9]:=
Graph[ResourceFunction["CompleteLevelsKaryTree"][{5, 4, 3, 2}], GraphLayout -> "RadialEmbedding"]
Out[9]=

Publisher

SHuisman

Version History

  • 1.0.0 – 06 May 2022

License Information