Function Repository Resource:

ListToCompleteBinaryTree (1.0.0) current version: 1.0.1 »

Source Notebook

Convert a list into a complete binary tree

Contributed by: Shenghui Yang

ResourceFunction["ListToCompleteBinaryTree"][list]

arrange the elements from list row-wise in a binary tree which is complete except for the last row.

Details and Options

ListToCompleteBinaryTree returns a Tree object.
A binary tree is a hierarchical data structure in which each node has at most two children, referred to as the left child and the right child.
A complete binary tree (CBT) is a special type of binary tree where all the levels of the tree are filled completely except the lowest level nodes which are filled from as left as possible.
A CBT of height h has at least 2h nodes and at most 2h+1-1 nodes.
ListToCompleteBinaryTree accepts the same options as Tree.

Examples

Basic Examples (1) 

Arrange 20 elements on a complete binary tree:

In[1]:=
ResourceFunction["ListToCompleteBinaryTree"][Range[20]]
Out[1]=

Options (2) 

ListToBinaryTree accepts the same options as Tree:

In[2]:=
ResourceFunction["ListToCompleteBinaryTree"][IntegerDigits[63357, 2], TreeElementStyle -> {TreeCases[1] -> LightRed}]
Out[2]=

ListToBinaryTree accepts multiple options:

In[3]:=
ResourceFunction["ListToCompleteBinaryTree"][RandomInteger[{0, 1}, 36],
 TreeElementShape -> All -> Graphics[{RGBColor["#101820"], Disk[{0, 1}, 1]}],
 TreeElementSize -> All -> Scaled[0.12],
 TreeElementLabelFunction -> All -> (Placed[
      Style[#, 18, Bold, FontFamily -> "Courier", RGBColor["#F0EDCC"]],
       Center] &)
 ]
Out[3]=

Properties and Relations (2) 

A complete binary tree of height h has at least 2h nodes and at most 2h+1-1 nodes. For instance when h=4:

In[4]:=
cbt1 = ResourceFunction["ListToCompleteBinaryTree"][Range[16]]
Out[4]=
In[5]:=
cbt2 = ResourceFunction["ListToCompleteBinaryTree"][Range[31]]
Out[5]=

The height of the tree are the same for both complete binary trees:

In[6]:=
TreeDepth /@ {cbt1, cbt2}
Out[6]=

A complete binary tree of 2k-1 nodes is the same as complete Kary tree with (k-1) 2's:

In[7]:=
k = 5;
In[8]:=
ResourceFunction["ListToCompleteBinaryTree"][Range[2^k - 1]]
Out[8]=
In[9]:=
ResourceFunction["CompleteLevelsKaryTree"][ConstantArray[2, k - 1]]
Out[9]=

Possible Issues (1) 

The length of the input must be at least one. Otherwise the function returns unevaluated:

In[10]:=
ResourceFunction["ListToCompleteBinaryTree"][{}]
Out[10]=

Neat Examples (2) 

Use binary expansion of n as input to create a complete binary tree and then read those bits in pre-order (OEIS A380856):

In[11]:=
cbt = ResourceFunction["ListToCompleteBinaryTree"][
  IntegerDigits[65537, 2], TreeElementStyle -> {TreeCases[1] -> LightRed}]
Out[11]=

Specify TreeTraversalOrder to implement pre-order traversal:

In[12]:=
res = Reap[
   TreeScan[Sow, cbt, TreeTraversalOrder -> {"DepthFirst", "TopDown", "LeftRight"}]][[2,
   1]]
Out[12]=

The new number and its associated complete binary tree:

In[13]:=
{FromDigits[res, 2], ResourceFunction["ListToCompleteBinaryTree"][res]}
Out[13]=

Every integer is associated with a permutation group using the operation from OEIS A380856:

In[14]:=
permGroup[n_Integer] := Module[{bits, len, perm},
  bits = IntegerDigits[n, 2];
  len = Length[bits];
  perm = Reap[TreeScan[Sow, ResourceFunction["ListToCompleteBinaryTree"][Range[len]], TreeTraversalOrder -> {"DepthFirst", "TopDown", "LeftRight"}]][[
    2, 1]];
  PermutationGroup[{FindPermutation[perm]}]
  ]

Use n=65537 for example:

In[15]:=
pg = permGroup[65537]
Out[15]=

The order of the permutation group associated with 65537 is 15:

In[16]:=
GroupOrder[pg]
Out[16]=

Repeat 15 times the rearrangement of binary bits of 65537 on complete binary tree using pre-order traversal will return to the original number:

In[17]:=
ds = CreateDataStructure["Deque"];
In[18]:=
ds["PushBack", IntegerDigits[65537, 2]]
Out[18]=
In[19]:=
If[# == 65537, Highlighted[#], #] & /@ (
  perm = NestList[
    (cbt = ResourceFunction["ListToCompleteBinaryTree"][
        IntegerDigits[#, 2]];
      res = Reap[TreeScan[Sow, cbt, TreeTraversalOrder -> {"DepthFirst", "TopDown", "LeftRight"}]][[2, 1]];
      ds["PushBack", res];
      FromDigits[res, 2]) &
    , 65537, 15])
Out[19]=

List the numbers and their complete binary tree representation:

In[20]:=
Grid[Partition[MapThread[
   Framed@Labeled[ResourceFunction["ListToCompleteBinaryTree"]@#1,
      If[#2 == 65537, Style[#2, 16, Bold, Red], #2]
      ] &, {ds["Elements"], perm}], 4]
 ]
Out[20]=

Remove unused items in the deque:

In[21]:=
ds["DropAll"]
Out[21]=

Publisher

Shenghui Yang

Requirements

Wolfram Language 14.0 (January 2024) or above

Version History

  • 1.0.1 – 18 March 2025
  • 1.0.0 – 07 March 2025

Source Metadata

Related Resources

License Information