Function Repository Resource:

NestTreeInteractive (1.0.1) current version: 1.0.2 »

Source Notebook

Interactively construct a tree

Contributed by: Ian Ford

ResourceFunction["NestTreeInteractive"][f,expr0]

gives an interactive tree with initial data expr0, where clicking a leaf with data expr adds new children defined by f[expr].

ResourceFunction["NestTreeInteractive"][f,expr0,h]

additionally applies h to the data of the clicked node.

Details

ResourceFunction["NestTreeInteractive"] is an interactive version of NestTree.
ResourceFunction["NestTreeInteractive"] grows Tree objects, adding successively deeper levels of children to the leaves using an operation f.
ResourceFunction["NestTreeInteractive"] is useful for constructing a tree from an expression.
Whenever a leaf Tree[expr,None] is clicked, it is replaced by Tree[expr,{Tree[expr1,None],}], where f[expr] gives {expr1,expr2,}.
If f returns None or {}, no futher levels can be added to that leaf.
In NestTree[f,expr,h], clicking a leaf Tree[expr,None] replaces it with Tree[h[expr],].
f[expr] should give {expr1,expr2,} or None.
If f does not return {} or None, ResourceFunction["NestTreeInteractive"][f,expr,] is equivalent to ResourceFunction["NestTreeInteractive"][List@*f,expr,].

Examples

Basic Examples (2) 

Interactively build a tree from an expression:

In[1]:=
ResourceFunction["NestTreeInteractive"][Range[3 # - 1, 3 # + 1] &, 1]
Out[1]=

Clicking the leaf adds children:

In[2]:=
ResourceFunction["NestTreeInteractive"][Range[3 # - 1, 3 # + 1] &, 1]
Out[2]=

Children can be continually added by clicking the leaves:

In[3]:=
ResourceFunction["NestTreeInteractive"][Range[3 # - 1, 3 # + 1] &, 1]
Out[3]=

Give a function to apply to each new node:

In[4]:=
ResourceFunction["NestTreeInteractive"][
 Range[3 # - 1, 3 # + 1] &, 1, clicked]
Out[4]=

Applications (2) 

Interactively create a Calkin-Wilf tree:

In[5]:=
ResourceFunction["NestTreeInteractive"][
 Replace[{m_, n_} :> {{m, m + n}, {m + n, n}}], {1, 1}, Replace[{m_, n_} :> m/n]]
Out[5]=

Interactively create a Stern-Brocot tree:

In[6]:=
ResourceFunction["NestTreeInteractive"][
 n |-> Replace[
   ContinuedFraction[n], {as___, a_} :> Sort[FromContinuedFraction /@ {{as, a - 1, 2}, {as, a + 1}}]], 1]
Out[6]=

Requirements

Wolfram Language 13.0 (December 2021) or above

Version History

  • 1.0.2 – 04 March 2024
  • 1.0.1 – 19 January 2024
  • 1.0.0 – 12 January 2024

Source Metadata

Related Resources

Author Notes

Unlike NestTree, NestTreeInteractive does not support operations f that return <||> or Hold[].

License Information