Function Repository Resource:

TreeFoldInteractive

Source Notebook

Interactively fold a tree

Contributed by: Ian Ford

ResourceFunction["TreeFoldInteractive"][f,tree]

successively folds the subtrees of tree, applying f to both the data of a subtree and the list of results for its children whenever it is clicked.

ResourceFunction["TreeFoldInteractive"][f,tree,h]

applies f to h[tree] instead of the data of tree.

ResourceFunction["TreeFoldInteractive"][{f,f-1},tree,h]

applies f-1 at the last level and f at each inner level.

Details

TreeFoldInteractive is an interactive version of TreeFold.
ResourceFunction["TreeFoldInteractive"] constructs an expression from a Tree object, folding its subtrees from the bottom up using a binary operation f.
ResourceFunction["TreeFoldInteractive"] is useful for accumulating or reducing the data or subtrees of a tree to a single value.
ResourceFunction["TreeFoldInteractive"] folds subtrees in a depth-first order, folding children before folding parents.
In ResourceFunction["TreeFoldInteractive"][f,tree,h], h[tree] is used as the first argument of f rather than TreeData[tree].
ResourceFunction["TreeFoldInteractive"][{,f-1},tree,h] gives f-1[h[tree]] if tree is a leaf.

Examples

Basic Examples (2) 

Interactively fold a tree with a binary function f:

In[1]:=
ResourceFunction[
 "TreeFoldInteractive", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][f, \!\(\*
GraphicsBox[
NamespaceBox["Trees",
DynamicModuleBox[{Typeset`tree = HoldComplete[
Tree[$CellContext`a, {
Tree[$CellContext`b, {
Tree[$CellContext`c, None], 
Tree[$CellContext`d, None]}], 
Tree[$CellContext`e, None]}]]}, 
NamespaceBox[{
{Hue[0.6, 0.7, 0.5], Opacity[0.7], Arrowheads[Medium], 
{RGBColor[0.6, 0.5882352941176471, 0.5529411764705883], AbsoluteThickness[1], LineBox[{{0.8944271909999159, 1.75586247584374}, {
           0.4472135954999579, 0.87793123792187}}]}, 
{RGBColor[0.6, 0.5882352941176471, 0.5529411764705883], AbsoluteThickness[1], LineBox[{{0.8944271909999159, 1.75586247584374}, {
           1.3416407864998738`, 0.87793123792187}}]}, 
{RGBColor[0.6, 0.5882352941176471, 0.5529411764705883], AbsoluteThickness[1], LineBox[{{0.4472135954999579, 0.87793123792187}, {0., 0.}}]}, 
{RGBColor[0.6, 0.5882352941176471, 0.5529411764705883], AbsoluteThickness[1], LineBox[{{0.4472135954999579, 0.87793123792187}, {
           0.8944271909999159, 0.}}]}}, 
{Hue[0.6, 0.2, 0.8], EdgeForm[{GrayLevel[0], Opacity[0.7]}], 
TagBox[InsetBox[
FrameBox["a",
Background->Directive[
RGBColor[0.9607843137254902, 0.9882352941176471, 0.9764705882352941]],
            
BaseStyle->GrayLevel[0],
FrameMargins->{{2, 2}, {1, 1}},
FrameStyle->Directive[
RGBColor[0.4196078431372549, 0.6313725490196078, 0.4196078431372549], AbsoluteThickness[1], 
Opacity[1]],
ImageSize->Automatic,
RoundingRadius->4,
StripOnInput->False], {0.8944271909999159, 1.75586247584374}],
"DynamicName",
BoxID -> "VertexID$1"], 
TagBox[InsetBox[
FrameBox["b",
Background->Directive[
RGBColor[0.9607843137254902, 0.9882352941176471, 0.9764705882352941]],
            
BaseStyle->GrayLevel[0],
FrameMargins->{{2, 2}, {1, 1}},
FrameStyle->Directive[
RGBColor[0.4196078431372549, 0.6313725490196078, 0.4196078431372549], AbsoluteThickness[1], 
Opacity[1]],
ImageSize->Automatic,
RoundingRadius->4,
StripOnInput->False], {0.4472135954999579, 0.87793123792187}],
"DynamicName",
BoxID -> "VertexID$2"], 
TagBox[InsetBox[
FrameBox["c",
Background->Directive[
RGBColor[0.9607843137254902, 0.9882352941176471, 0.9764705882352941]],
            
BaseStyle->GrayLevel[0],
FrameMargins->{{2, 2}, {1, 1}},
FrameStyle->Directive[
RGBColor[0.4196078431372549, 0.6313725490196078, 0.4196078431372549], AbsoluteThickness[1], 
Opacity[1]],
ImageSize->Automatic,
RoundingRadius->0,
StripOnInput->False], {0., 0.}],
"DynamicName",
BoxID -> "VertexID$3"], 
TagBox[InsetBox[
FrameBox["d",
Background->Directive[
RGBColor[0.9607843137254902, 0.9882352941176471, 0.9764705882352941]],
            
BaseStyle->GrayLevel[0],
FrameMargins->{{2, 2}, {1, 1}},
FrameStyle->Directive[
RGBColor[0.4196078431372549, 0.6313725490196078, 0.4196078431372549], AbsoluteThickness[1], 
Opacity[1]],
ImageSize->Automatic,
RoundingRadius->0,
StripOnInput->False], {0.8944271909999159, 0.}],
"DynamicName",
BoxID -> "VertexID$4"], 
TagBox[InsetBox[
FrameBox["e",
Background->Directive[
RGBColor[0.9607843137254902, 0.9882352941176471, 0.9764705882352941]],
            
BaseStyle->GrayLevel[0],
FrameMargins->{{2, 2}, {1, 1}},
FrameStyle->Directive[
RGBColor[0.4196078431372549, 0.6313725490196078, 0.4196078431372549], AbsoluteThickness[1], 
Opacity[1]],
ImageSize->Automatic,
RoundingRadius->0,
StripOnInput->False], {1.3416407864998738, 0.87793123792187}],
"DynamicName",
BoxID -> "VertexID$5"]}}]]],
AlignmentPoint->Center,
Axes->False,
AxesLabel->None,
AxesOrigin->Automatic,
AxesStyle->{},
Background->None,
BaseStyle->{},
BaselinePosition->Automatic,
ContentSelectable->Automatic,
DefaultBaseStyle->"TreeGraphics",
DisplayFunction->Identity,
Epilog->{},
FormatType->StandardForm,
Frame->False,
FrameLabel->None,
FrameStyle->{},
FrameTicks->None,
FrameTicksStyle->{},
GridLines->None,
GridLinesStyle->{},
ImageMargins->0.,
ImagePadding->All,
ImageSize->{43.2728216423409, Automatic},
LabelStyle->{},
PlotLabel->None,
PlotRange->All,
PlotRangeClipping->False,
PlotRangePadding->Automatic,
PlotRegion->Automatic,
Prolog->{},
RotateLabel->True,
Ticks->Automatic,
TicksStyle->{}]\)]
Out[1]=

Interactively fold a tree with a binary function f after applying a function g to the data in the leaves:

In[2]:=
ResourceFunction[
 "TreeFoldInteractive", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][{f, g}, \!\(\*
GraphicsBox[
NamespaceBox["Trees",
DynamicModuleBox[{Typeset`tree = HoldComplete[
Tree[1, {
Tree[2, None], 
Tree[3, {
Tree[4, None], 
Tree[5, None], 
Tree[6, None]}], 
Tree[7, None]}]]}, 
NamespaceBox[{
{Hue[0.6, 0.7, 0.5], Opacity[0.7], Arrowheads[Medium], 
{RGBColor[0.6, 0.5882352941176471, 0.5529411764705883], AbsoluteThickness[1], LineBox[{{0.7745966692414834, 1.546714629141394}, {0., 0.773357314570697}}]}, 
{RGBColor[0.6, 0.5882352941176471, 0.5529411764705883], AbsoluteThickness[1], LineBox[{{0.7745966692414834, 1.546714629141394}, {
           0.7745966692414834, 0.773357314570697}}]}, 
{RGBColor[0.6, 0.5882352941176471, 0.5529411764705883], AbsoluteThickness[1], LineBox[{{0.7745966692414834, 1.546714629141394}, {
           1.5491933384829668`, 0.773357314570697}}]}, 
{RGBColor[0.6, 0.5882352941176471, 0.5529411764705883], AbsoluteThickness[1], LineBox[{{0.7745966692414834, 0.773357314570697}, {0., 0.}}]}, 
{RGBColor[0.6, 0.5882352941176471, 0.5529411764705883], AbsoluteThickness[1], LineBox[{{0.7745966692414834, 0.773357314570697}, {
           0.7745966692414834, 0.}}]}, 
{RGBColor[0.6, 0.5882352941176471, 0.5529411764705883], AbsoluteThickness[1], LineBox[{{0.7745966692414834, 0.773357314570697}, {
           1.5491933384829668`, 0.}}]}}, 
{Hue[0.6, 0.2, 0.8], EdgeForm[{GrayLevel[0], Opacity[0.7]}], 
TagBox[InsetBox[
FrameBox["1",
Background->Directive[
RGBColor[0.9607843137254902, 0.9882352941176471, 0.9764705882352941]],
            
BaseStyle->GrayLevel[0],
FrameMargins->{{2, 2}, {1, 1}},
FrameStyle->Directive[
RGBColor[0.4196078431372549, 0.6313725490196078, 0.4196078431372549], AbsoluteThickness[1], 
Opacity[1]],
ImageSize->Automatic,
RoundingRadius->4,
StripOnInput->False], {0.7745966692414834, 1.546714629141394}],
"DynamicName",
BoxID -> "VertexID$1"], 
TagBox[InsetBox[
FrameBox["2",
Background->Directive[
RGBColor[0.9607843137254902, 0.9882352941176471, 0.9764705882352941]],
            
BaseStyle->GrayLevel[0],
FrameMargins->{{2, 2}, {1, 1}},
FrameStyle->Directive[
RGBColor[0.4196078431372549, 0.6313725490196078, 0.4196078431372549], AbsoluteThickness[1], 
Opacity[1]],
ImageSize->Automatic,
RoundingRadius->0,
StripOnInput->False], {0., 0.773357314570697}],
"DynamicName",
BoxID -> "VertexID$2"], 
TagBox[InsetBox[
FrameBox["3",
Background->Directive[
RGBColor[0.9607843137254902, 0.9882352941176471, 0.9764705882352941]],
            
BaseStyle->GrayLevel[0],
FrameMargins->{{2, 2}, {1, 1}},
FrameStyle->Directive[
RGBColor[0.4196078431372549, 0.6313725490196078, 0.4196078431372549], AbsoluteThickness[1], 
Opacity[1]],
ImageSize->Automatic,
RoundingRadius->4,
StripOnInput->False], {0.7745966692414834, 0.773357314570697}],
"DynamicName",
BoxID -> "VertexID$3"], 
TagBox[InsetBox[
FrameBox["4",
Background->Directive[
RGBColor[0.9607843137254902, 0.9882352941176471, 0.9764705882352941]],
            
BaseStyle->GrayLevel[0],
FrameMargins->{{2, 2}, {1, 1}},
FrameStyle->Directive[
RGBColor[0.4196078431372549, 0.6313725490196078, 0.4196078431372549], AbsoluteThickness[1], 
Opacity[1]],
ImageSize->Automatic,
RoundingRadius->0,
StripOnInput->False], {0., 0.}],
"DynamicName",
BoxID -> "VertexID$4"], 
TagBox[InsetBox[
FrameBox["5",
Background->Directive[
RGBColor[0.9607843137254902, 0.9882352941176471, 0.9764705882352941]],
            
BaseStyle->GrayLevel[0],
FrameMargins->{{2, 2}, {1, 1}},
FrameStyle->Directive[
RGBColor[0.4196078431372549, 0.6313725490196078, 0.4196078431372549], AbsoluteThickness[1], 
Opacity[1]],
ImageSize->Automatic,
RoundingRadius->0,
StripOnInput->False], {0.7745966692414834, 0.}],
"DynamicName",
BoxID -> "VertexID$5"], 
TagBox[InsetBox[
FrameBox["6",
Background->Directive[
RGBColor[0.9607843137254902, 0.9882352941176471, 0.9764705882352941]],
            
BaseStyle->GrayLevel[0],
FrameMargins->{{2, 2}, {1, 1}},
FrameStyle->Directive[
RGBColor[0.4196078431372549, 0.6313725490196078, 0.4196078431372549], AbsoluteThickness[1], 
Opacity[1]],
ImageSize->Automatic,
RoundingRadius->0,
StripOnInput->False], {1.5491933384829668, 0.}],
"DynamicName",
BoxID -> "VertexID$6"], 
TagBox[InsetBox[
FrameBox["7",
Background->Directive[
RGBColor[0.9607843137254902, 0.9882352941176471, 0.9764705882352941]],
            
BaseStyle->GrayLevel[0],
FrameMargins->{{2, 2}, {1, 1}},
FrameStyle->Directive[
RGBColor[0.4196078431372549, 0.6313725490196078, 0.4196078431372549], AbsoluteThickness[1], 
Opacity[1]],
ImageSize->Automatic,
RoundingRadius->0,
StripOnInput->False], {1.5491933384829668, 0.773357314570697}],
"DynamicName",
BoxID -> "VertexID$7"]}}]]],
AlignmentPoint->Center,
Axes->False,
AxesLabel->None,
AxesOrigin->Automatic,
AxesStyle->{},
Background->None,
BaseStyle->{},
BaselinePosition->Automatic,
ContentSelectable->Automatic,
DefaultBaseStyle->"TreeGraphics",
Epilog->{},
FormatType->StandardForm,
Frame->False,
FrameLabel->FormBox["False", StandardForm],
FrameStyle->{},
FrameTicks->None,
FrameTicksStyle->{},
GridLines->None,
GridLinesStyle->{},
ImageMargins->0.,
ImagePadding->All,
ImageSize->{59.25, Automatic},
LabelStyle->{},
PlotLabel->None,
PlotRange->All,
PlotRangeClipping->False,
PlotRangePadding->Automatic,
PlotRegion->Automatic,
Prolog->{},
RotateLabel->True,
Ticks->Automatic,
TicksStyle->{}]\)]
Out[2]=

Clicking a leaf applies g:

In[3]:=
ResourceFunction[
 "TreeFoldInteractive", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][{f, g}, \!\(\*
GraphicsBox[
NamespaceBox["Trees",
DynamicModuleBox[{Typeset`tree = HoldComplete[
Tree[1, {
Tree[2, None], 
Tree[3, {
Tree[4, None], 
Tree[5, None], 
Tree[6, None]}], 
Tree[7, None]}]]}, 
NamespaceBox[{
{Hue[0.6, 0.7, 0.5], Opacity[0.7], Arrowheads[Medium], 
{RGBColor[0.6, 0.5882352941176471, 0.5529411764705883], AbsoluteThickness[1], LineBox[{{0.7745966692414834, 1.546714629141394}, {0., 0.773357314570697}}]}, 
{RGBColor[0.6, 0.5882352941176471, 0.5529411764705883], AbsoluteThickness[1], LineBox[{{0.7745966692414834, 1.546714629141394}, {
           0.7745966692414834, 0.773357314570697}}]}, 
{RGBColor[0.6, 0.5882352941176471, 0.5529411764705883], AbsoluteThickness[1], LineBox[{{0.7745966692414834, 1.546714629141394}, {
           1.5491933384829668`, 0.773357314570697}}]}, 
{RGBColor[0.6, 0.5882352941176471, 0.5529411764705883], AbsoluteThickness[1], LineBox[{{0.7745966692414834, 0.773357314570697}, {0., 0.}}]}, 
{RGBColor[0.6, 0.5882352941176471, 0.5529411764705883], AbsoluteThickness[1], LineBox[{{0.7745966692414834, 0.773357314570697}, {
           0.7745966692414834, 0.}}]}, 
{RGBColor[0.6, 0.5882352941176471, 0.5529411764705883], AbsoluteThickness[1], LineBox[{{0.7745966692414834, 0.773357314570697}, {
           1.5491933384829668`, 0.}}]}}, 
{Hue[0.6, 0.2, 0.8], EdgeForm[{GrayLevel[0], Opacity[0.7]}], 
TagBox[InsetBox[
FrameBox["1",
Background->Directive[
RGBColor[0.9607843137254902, 0.9882352941176471, 0.9764705882352941]],
            
BaseStyle->GrayLevel[0],
FrameMargins->{{2, 2}, {1, 1}},
FrameStyle->Directive[
RGBColor[0.4196078431372549, 0.6313725490196078, 0.4196078431372549], AbsoluteThickness[1], 
Opacity[1]],
ImageSize->Automatic,
RoundingRadius->4,
StripOnInput->False], {0.7745966692414834, 1.546714629141394}],
"DynamicName",
BoxID -> "VertexID$1"], 
TagBox[InsetBox[
FrameBox["2",
Background->Directive[
RGBColor[0.9607843137254902, 0.9882352941176471, 0.9764705882352941]],
            
BaseStyle->GrayLevel[0],
FrameMargins->{{2, 2}, {1, 1}},
FrameStyle->Directive[
RGBColor[0.4196078431372549, 0.6313725490196078, 0.4196078431372549], AbsoluteThickness[1], 
Opacity[1]],
ImageSize->Automatic,
RoundingRadius->0,
StripOnInput->False], {0., 0.773357314570697}],
"DynamicName",
BoxID -> "VertexID$2"], 
TagBox[InsetBox[
FrameBox["3",
Background->Directive[
RGBColor[0.9607843137254902, 0.9882352941176471, 0.9764705882352941]],
            
BaseStyle->GrayLevel[0],
FrameMargins->{{2, 2}, {1, 1}},
FrameStyle->Directive[
RGBColor[0.4196078431372549, 0.6313725490196078, 0.4196078431372549], AbsoluteThickness[1], 
Opacity[1]],
ImageSize->Automatic,
RoundingRadius->4,
StripOnInput->False], {0.7745966692414834, 0.773357314570697}],
"DynamicName",
BoxID -> "VertexID$3"], 
TagBox[InsetBox[
FrameBox["4",
Background->Directive[
RGBColor[0.9607843137254902, 0.9882352941176471, 0.9764705882352941]],
            
BaseStyle->GrayLevel[0],
FrameMargins->{{2, 2}, {1, 1}},
FrameStyle->Directive[
RGBColor[0.4196078431372549, 0.6313725490196078, 0.4196078431372549], AbsoluteThickness[1], 
Opacity[1]],
ImageSize->Automatic,
RoundingRadius->0,
StripOnInput->False], {0., 0.}],
"DynamicName",
BoxID -> "VertexID$4"], 
TagBox[InsetBox[
FrameBox["5",
Background->Directive[
RGBColor[0.9607843137254902, 0.9882352941176471, 0.9764705882352941]],
            
BaseStyle->GrayLevel[0],
FrameMargins->{{2, 2}, {1, 1}},
FrameStyle->Directive[
RGBColor[0.4196078431372549, 0.6313725490196078, 0.4196078431372549], AbsoluteThickness[1], 
Opacity[1]],
ImageSize->Automatic,
RoundingRadius->0,
StripOnInput->False], {0.7745966692414834, 0.}],
"DynamicName",
BoxID -> "VertexID$5"], 
TagBox[InsetBox[
FrameBox["6",
Background->Directive[
RGBColor[0.9607843137254902, 0.9882352941176471, 0.9764705882352941]],
            
BaseStyle->GrayLevel[0],
FrameMargins->{{2, 2}, {1, 1}},
FrameStyle->Directive[
RGBColor[0.4196078431372549, 0.6313725490196078, 0.4196078431372549], AbsoluteThickness[1], 
Opacity[1]],
ImageSize->Automatic,
RoundingRadius->0,
StripOnInput->False], {1.5491933384829668, 0.}],
"DynamicName",
BoxID -> "VertexID$6"], 
TagBox[InsetBox[
FrameBox["7",
Background->Directive[
RGBColor[0.9607843137254902, 0.9882352941176471, 0.9764705882352941]],
            
BaseStyle->GrayLevel[0],
FrameMargins->{{2, 2}, {1, 1}},
FrameStyle->Directive[
RGBColor[0.4196078431372549, 0.6313725490196078, 0.4196078431372549], AbsoluteThickness[1], 
Opacity[1]],
ImageSize->Automatic,
RoundingRadius->0,
StripOnInput->False], {1.5491933384829668, 0.773357314570697}],
"DynamicName",
BoxID -> "VertexID$7"]}}]]],
AlignmentPoint->Center,
Axes->False,
AxesLabel->None,
AxesOrigin->Automatic,
AxesStyle->{},
Background->None,
BaseStyle->{},
BaselinePosition->Automatic,
ContentSelectable->Automatic,
DefaultBaseStyle->"TreeGraphics",
Epilog->{},
FormatType->StandardForm,
Frame->False,
FrameLabel->FormBox["False", StandardForm],
FrameStyle->{},
FrameTicks->None,
FrameTicksStyle->{},
GridLines->None,
GridLinesStyle->{},
ImageMargins->0.,
ImagePadding->All,
ImageSize->{59.25, Automatic},
LabelStyle->{},
PlotLabel->None,
PlotRange->All,
PlotRangeClipping->False,
PlotRangePadding->Automatic,
PlotRegion->Automatic,
Prolog->{},
RotateLabel->True,
Ticks->Automatic,
TicksStyle->{}]\)]
Out[3]=

Clicking an inner node folds its corresponding subtree:

In[4]:=
ResourceFunction[
 "TreeFoldInteractive", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][{f, g}, \!\(\*
GraphicsBox[
NamespaceBox["Trees",
DynamicModuleBox[{Typeset`tree = HoldComplete[
Tree[1, {
Tree[2, None], 
Tree[3, {
Tree[4, None], 
Tree[5, None], 
Tree[6, None]}], 
Tree[7, None]}]]}, 
NamespaceBox[{
{Hue[0.6, 0.7, 0.5], Opacity[0.7], Arrowheads[Medium], 
{RGBColor[0.6, 0.5882352941176471, 0.5529411764705883], AbsoluteThickness[1], LineBox[{{0.7745966692414834, 1.546714629141394}, {0., 0.773357314570697}}]}, 
{RGBColor[0.6, 0.5882352941176471, 0.5529411764705883], AbsoluteThickness[1], LineBox[{{0.7745966692414834, 1.546714629141394}, {
           0.7745966692414834, 0.773357314570697}}]}, 
{RGBColor[0.6, 0.5882352941176471, 0.5529411764705883], AbsoluteThickness[1], LineBox[{{0.7745966692414834, 1.546714629141394}, {
           1.5491933384829668`, 0.773357314570697}}]}, 
{RGBColor[0.6, 0.5882352941176471, 0.5529411764705883], AbsoluteThickness[1], LineBox[{{0.7745966692414834, 0.773357314570697}, {0., 0.}}]}, 
{RGBColor[0.6, 0.5882352941176471, 0.5529411764705883], AbsoluteThickness[1], LineBox[{{0.7745966692414834, 0.773357314570697}, {
           0.7745966692414834, 0.}}]}, 
{RGBColor[0.6, 0.5882352941176471, 0.5529411764705883], AbsoluteThickness[1], LineBox[{{0.7745966692414834, 0.773357314570697}, {
           1.5491933384829668`, 0.}}]}}, 
{Hue[0.6, 0.2, 0.8], EdgeForm[{GrayLevel[0], Opacity[0.7]}], 
TagBox[InsetBox[
FrameBox["1",
Background->Directive[
RGBColor[0.9607843137254902, 0.9882352941176471, 0.9764705882352941]],
            
BaseStyle->GrayLevel[0],
FrameMargins->{{2, 2}, {1, 1}},
FrameStyle->Directive[
RGBColor[0.4196078431372549, 0.6313725490196078, 0.4196078431372549], AbsoluteThickness[1], 
Opacity[1]],
ImageSize->Automatic,
RoundingRadius->4,
StripOnInput->False], {0.7745966692414834, 1.546714629141394}],
"DynamicName",
BoxID -> "VertexID$1"], 
TagBox[InsetBox[
FrameBox["2",
Background->Directive[
RGBColor[0.9607843137254902, 0.9882352941176471, 0.9764705882352941]],
            
BaseStyle->GrayLevel[0],
FrameMargins->{{2, 2}, {1, 1}},
FrameStyle->Directive[
RGBColor[0.4196078431372549, 0.6313725490196078, 0.4196078431372549], AbsoluteThickness[1], 
Opacity[1]],
ImageSize->Automatic,
RoundingRadius->0,
StripOnInput->False], {0., 0.773357314570697}],
"DynamicName",
BoxID -> "VertexID$2"], 
TagBox[InsetBox[
FrameBox["3",
Background->Directive[
RGBColor[0.9607843137254902, 0.9882352941176471, 0.9764705882352941]],
            
BaseStyle->GrayLevel[0],
FrameMargins->{{2, 2}, {1, 1}},
FrameStyle->Directive[
RGBColor[0.4196078431372549, 0.6313725490196078, 0.4196078431372549], AbsoluteThickness[1], 
Opacity[1]],
ImageSize->Automatic,
RoundingRadius->4,
StripOnInput->False], {0.7745966692414834, 0.773357314570697}],
"DynamicName",
BoxID -> "VertexID$3"], 
TagBox[InsetBox[
FrameBox["4",
Background->Directive[
RGBColor[0.9607843137254902, 0.9882352941176471, 0.9764705882352941]],
            
BaseStyle->GrayLevel[0],
FrameMargins->{{2, 2}, {1, 1}},
FrameStyle->Directive[
RGBColor[0.4196078431372549, 0.6313725490196078, 0.4196078431372549], AbsoluteThickness[1], 
Opacity[1]],
ImageSize->Automatic,
RoundingRadius->0,
StripOnInput->False], {0., 0.}],
"DynamicName",
BoxID -> "VertexID$4"], 
TagBox[InsetBox[
FrameBox["5",
Background->Directive[
RGBColor[0.9607843137254902, 0.9882352941176471, 0.9764705882352941]],
            
BaseStyle->GrayLevel[0],
FrameMargins->{{2, 2}, {1, 1}},
FrameStyle->Directive[
RGBColor[0.4196078431372549, 0.6313725490196078, 0.4196078431372549], AbsoluteThickness[1], 
Opacity[1]],
ImageSize->Automatic,
RoundingRadius->0,
StripOnInput->False], {0.7745966692414834, 0.}],
"DynamicName",
BoxID -> "VertexID$5"], 
TagBox[InsetBox[
FrameBox["6",
Background->Directive[
RGBColor[0.9607843137254902, 0.9882352941176471, 0.9764705882352941]],
            
BaseStyle->GrayLevel[0],
FrameMargins->{{2, 2}, {1, 1}},
FrameStyle->Directive[
RGBColor[0.4196078431372549, 0.6313725490196078, 0.4196078431372549], AbsoluteThickness[1], 
Opacity[1]],
ImageSize->Automatic,
RoundingRadius->0,
StripOnInput->False], {1.5491933384829668, 0.}],
"DynamicName",
BoxID -> "VertexID$6"], 
TagBox[InsetBox[
FrameBox["7",
Background->Directive[
RGBColor[0.9607843137254902, 0.9882352941176471, 0.9764705882352941]],
            
BaseStyle->GrayLevel[0],
FrameMargins->{{2, 2}, {1, 1}},
FrameStyle->Directive[
RGBColor[0.4196078431372549, 0.6313725490196078, 0.4196078431372549], AbsoluteThickness[1], 
Opacity[1]],
ImageSize->Automatic,
RoundingRadius->0,
StripOnInput->False], {1.5491933384829668, 0.773357314570697}],
"DynamicName",
BoxID -> "VertexID$7"]}}]]],
AlignmentPoint->Center,
Axes->False,
AxesLabel->None,
AxesOrigin->Automatic,
AxesStyle->{},
Background->None,
BaseStyle->{},
BaselinePosition->Automatic,
ContentSelectable->Automatic,
DefaultBaseStyle->"TreeGraphics",
Epilog->{},
FormatType->StandardForm,
Frame->False,
FrameLabel->FormBox["False", StandardForm],
FrameStyle->{},
FrameTicks->None,
FrameTicksStyle->{},
GridLines->None,
GridLinesStyle->{},
ImageMargins->0.,
ImagePadding->All,
ImageSize->{59.25, Automatic},
LabelStyle->{},
PlotLabel->None,
PlotRange->All,
PlotRangeClipping->False,
PlotRangePadding->Automatic,
PlotRegion->Automatic,
Prolog->{},
RotateLabel->True,
Ticks->Automatic,
TicksStyle->{}]\)]
Out[4]=

Clicking the root node folds the whole tree:

In[5]:=
ResourceFunction[
 "TreeFoldInteractive", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][{f, g}, \!\(\*
GraphicsBox[
NamespaceBox["Trees",
DynamicModuleBox[{Typeset`tree = HoldComplete[
Tree[1, {
Tree[2, None], 
Tree[3, {
Tree[4, None], 
Tree[5, None], 
Tree[6, None]}], 
Tree[7, None]}]]}, 
NamespaceBox[{
{Hue[0.6, 0.7, 0.5], Opacity[0.7], Arrowheads[Medium], 
{RGBColor[0.6, 0.5882352941176471, 0.5529411764705883], AbsoluteThickness[1], LineBox[{{0.7745966692414834, 1.546714629141394}, {0., 0.773357314570697}}]}, 
{RGBColor[0.6, 0.5882352941176471, 0.5529411764705883], AbsoluteThickness[1], LineBox[{{0.7745966692414834, 1.546714629141394}, {
           0.7745966692414834, 0.773357314570697}}]}, 
{RGBColor[0.6, 0.5882352941176471, 0.5529411764705883], AbsoluteThickness[1], LineBox[{{0.7745966692414834, 1.546714629141394}, {
           1.5491933384829668`, 0.773357314570697}}]}, 
{RGBColor[0.6, 0.5882352941176471, 0.5529411764705883], AbsoluteThickness[1], LineBox[{{0.7745966692414834, 0.773357314570697}, {0., 0.}}]}, 
{RGBColor[0.6, 0.5882352941176471, 0.5529411764705883], AbsoluteThickness[1], LineBox[{{0.7745966692414834, 0.773357314570697}, {
           0.7745966692414834, 0.}}]}, 
{RGBColor[0.6, 0.5882352941176471, 0.5529411764705883], AbsoluteThickness[1], LineBox[{{0.7745966692414834, 0.773357314570697}, {
           1.5491933384829668`, 0.}}]}}, 
{Hue[0.6, 0.2, 0.8], EdgeForm[{GrayLevel[0], Opacity[0.7]}], 
TagBox[InsetBox[
FrameBox["1",
Background->Directive[
RGBColor[0.9607843137254902, 0.9882352941176471, 0.9764705882352941]],
            
BaseStyle->GrayLevel[0],
FrameMargins->{{2, 2}, {1, 1}},
FrameStyle->Directive[
RGBColor[0.4196078431372549, 0.6313725490196078, 0.4196078431372549], AbsoluteThickness[1], 
Opacity[1]],
ImageSize->Automatic,
RoundingRadius->4,
StripOnInput->False], {0.7745966692414834, 1.546714629141394}],
"DynamicName",
BoxID -> "VertexID$1"], 
TagBox[InsetBox[
FrameBox["2",
Background->Directive[
RGBColor[0.9607843137254902, 0.9882352941176471, 0.9764705882352941]],
            
BaseStyle->GrayLevel[0],
FrameMargins->{{2, 2}, {1, 1}},
FrameStyle->Directive[
RGBColor[0.4196078431372549, 0.6313725490196078, 0.4196078431372549], AbsoluteThickness[1], 
Opacity[1]],
ImageSize->Automatic,
RoundingRadius->0,
StripOnInput->False], {0., 0.773357314570697}],
"DynamicName",
BoxID -> "VertexID$2"], 
TagBox[InsetBox[
FrameBox["3",
Background->Directive[
RGBColor[0.9607843137254902, 0.9882352941176471, 0.9764705882352941]],
            
BaseStyle->GrayLevel[0],
FrameMargins->{{2, 2}, {1, 1}},
FrameStyle->Directive[
RGBColor[0.4196078431372549, 0.6313725490196078, 0.4196078431372549], AbsoluteThickness[1], 
Opacity[1]],
ImageSize->Automatic,
RoundingRadius->4,
StripOnInput->False], {0.7745966692414834, 0.773357314570697}],
"DynamicName",
BoxID -> "VertexID$3"], 
TagBox[InsetBox[
FrameBox["4",
Background->Directive[
RGBColor[0.9607843137254902, 0.9882352941176471, 0.9764705882352941]],
            
BaseStyle->GrayLevel[0],
FrameMargins->{{2, 2}, {1, 1}},
FrameStyle->Directive[
RGBColor[0.4196078431372549, 0.6313725490196078, 0.4196078431372549], AbsoluteThickness[1], 
Opacity[1]],
ImageSize->Automatic,
RoundingRadius->0,
StripOnInput->False], {0., 0.}],
"DynamicName",
BoxID -> "VertexID$4"], 
TagBox[InsetBox[
FrameBox["5",
Background->Directive[
RGBColor[0.9607843137254902, 0.9882352941176471, 0.9764705882352941]],
            
BaseStyle->GrayLevel[0],
FrameMargins->{{2, 2}, {1, 1}},
FrameStyle->Directive[
RGBColor[0.4196078431372549, 0.6313725490196078, 0.4196078431372549], AbsoluteThickness[1], 
Opacity[1]],
ImageSize->Automatic,
RoundingRadius->0,
StripOnInput->False], {0.7745966692414834, 0.}],
"DynamicName",
BoxID -> "VertexID$5"], 
TagBox[InsetBox[
FrameBox["6",
Background->Directive[
RGBColor[0.9607843137254902, 0.9882352941176471, 0.9764705882352941]],
            
BaseStyle->GrayLevel[0],
FrameMargins->{{2, 2}, {1, 1}},
FrameStyle->Directive[
RGBColor[0.4196078431372549, 0.6313725490196078, 0.4196078431372549], AbsoluteThickness[1], 
Opacity[1]],
ImageSize->Automatic,
RoundingRadius->0,
StripOnInput->False], {1.5491933384829668, 0.}],
"DynamicName",
BoxID -> "VertexID$6"], 
TagBox[InsetBox[
FrameBox["7",
Background->Directive[
RGBColor[0.9607843137254902, 0.9882352941176471, 0.9764705882352941]],
            
BaseStyle->GrayLevel[0],
FrameMargins->{{2, 2}, {1, 1}},
FrameStyle->Directive[
RGBColor[0.4196078431372549, 0.6313725490196078, 0.4196078431372549], AbsoluteThickness[1], 
Opacity[1]],
ImageSize->Automatic,
RoundingRadius->0,
StripOnInput->False], {1.5491933384829668, 0.773357314570697}],
"DynamicName",
BoxID -> "VertexID$7"]}}]]],
AlignmentPoint->Center,
Axes->False,
AxesLabel->None,
AxesOrigin->Automatic,
AxesStyle->{},
Background->None,
BaseStyle->{},
BaselinePosition->Automatic,
ContentSelectable->Automatic,
DefaultBaseStyle->"TreeGraphics",
Epilog->{},
FormatType->StandardForm,
Frame->False,
FrameLabel->FormBox["False", StandardForm],
FrameStyle->{},
FrameTicks->None,
FrameTicksStyle->{},
GridLines->None,
GridLinesStyle->{},
ImageMargins->0.,
ImagePadding->All,
ImageSize->{59.25, Automatic},
LabelStyle->{},
PlotLabel->None,
PlotRange->All,
PlotRangeClipping->False,
PlotRangePadding->Automatic,
PlotRegion->Automatic,
Prolog->{},
RotateLabel->True,
Ticks->Automatic,
TicksStyle->{}]\)]
Out[5]=

Scope (1) 

Interactively fold a property of subtrees:

In[6]:=
ResourceFunction[
 "TreeFoldInteractive", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][{f, g}, \!\(\*
GraphicsBox[
NamespaceBox["Trees",
DynamicModuleBox[{Typeset`tree = HoldComplete[
Tree[$CellContext`a, {
Tree[$CellContext`b, None], 
Tree[$CellContext`c, None], 
Tree[$CellContext`d, None]}]]}, 
NamespaceBox[{
{Hue[0.6, 0.7, 0.5], Opacity[0.7], Arrowheads[Medium], 
{RGBColor[0.6, 0.5882352941176471, 0.5529411764705883], AbsoluteThickness[1], LineBox[{{0.7745966692414834, 0.8207638204647711}, {0., 0.}}]}, 
{RGBColor[0.6, 0.5882352941176471, 0.5529411764705883], AbsoluteThickness[1], LineBox[{{0.7745966692414834, 0.8207638204647711}, {
           0.7745966692414834, 0.}}]}, 
{RGBColor[0.6, 0.5882352941176471, 0.5529411764705883], AbsoluteThickness[1], LineBox[{{0.7745966692414834, 0.8207638204647711}, {
           1.5491933384829668`, 0.}}]}}, 
{Hue[0.6, 0.2, 0.8], EdgeForm[{GrayLevel[0], Opacity[0.7]}], 
TagBox[InsetBox[
FrameBox["a",
Background->Directive[
RGBColor[0.9607843137254902, 0.9882352941176471, 0.9764705882352941]],
            
BaseStyle->GrayLevel[0],
FrameMargins->{{2, 2}, {1, 1}},
FrameStyle->Directive[
RGBColor[0.4196078431372549, 0.6313725490196078, 0.4196078431372549], AbsoluteThickness[1], 
Opacity[1]],
ImageSize->Automatic,
RoundingRadius->4,
StripOnInput->False], {0.7745966692414834, 0.8207638204647711}],
"DynamicName",
BoxID -> "VertexID$1"], 
TagBox[InsetBox[
FrameBox["b",
Background->Directive[
RGBColor[0.9607843137254902, 0.9882352941176471, 0.9764705882352941]],
            
BaseStyle->GrayLevel[0],
FrameMargins->{{2, 2}, {1, 1}},
FrameStyle->Directive[
RGBColor[0.4196078431372549, 0.6313725490196078, 0.4196078431372549], AbsoluteThickness[1], 
Opacity[1]],
ImageSize->Automatic,
RoundingRadius->0,
StripOnInput->False], {0., 0.}],
"DynamicName",
BoxID -> "VertexID$2"], 
TagBox[InsetBox[
FrameBox["c",
Background->Directive[
RGBColor[0.9607843137254902, 0.9882352941176471, 0.9764705882352941]],
            
BaseStyle->GrayLevel[0],
FrameMargins->{{2, 2}, {1, 1}},
FrameStyle->Directive[
RGBColor[0.4196078431372549, 0.6313725490196078, 0.4196078431372549], AbsoluteThickness[1], 
Opacity[1]],
ImageSize->Automatic,
RoundingRadius->0,
StripOnInput->False], {0.7745966692414834, 0.}],
"DynamicName",
BoxID -> "VertexID$3"], 
TagBox[InsetBox[
FrameBox["d",
Background->Directive[
RGBColor[0.9607843137254902, 0.9882352941176471, 0.9764705882352941]],
            
BaseStyle->GrayLevel[0],
FrameMargins->{{2, 2}, {1, 1}},
FrameStyle->Directive[
RGBColor[0.4196078431372549, 0.6313725490196078, 0.4196078431372549], AbsoluteThickness[1], 
Opacity[1]],
ImageSize->Automatic,
RoundingRadius->0,
StripOnInput->False], {1.5491933384829668, 0.}],
"DynamicName",
BoxID -> "VertexID$4"]}}]]],
AlignmentPoint->Center,
Axes->False,
AxesLabel->None,
AxesOrigin->Automatic,
AxesStyle->{},
Background->None,
BaseStyle->{},
BaselinePosition->Automatic,
ContentSelectable->Automatic,
DefaultBaseStyle->"TreeGraphics",
DisplayFunction->Identity,
Epilog->{},
FormatType->StandardForm,
Frame->False,
FrameLabel->None,
FrameStyle->{},
FrameTicks->None,
FrameTicksStyle->{},
GridLines->None,
GridLinesStyle->{},
ImageMargins->0.,
ImagePadding->All,
ImageSize->{54., Automatic},
LabelStyle->{},
PlotLabel->None,
PlotRange->All,
PlotRangeClipping->False,
PlotRangePadding->Automatic,
PlotRegion->Automatic,
Prolog->{},
RotateLabel->True,
Ticks->Automatic,
TicksStyle->{}]\), h]
Out[6]=

Requirements

Wolfram Language 13.0 (December 2021) or above

Version History

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

Source Metadata

Related Resources

Author Notes

Unlike TreeFold, TreeFoldInteractive does not support trees with an association <||> of children.

The output is reset when the notebook is closed, so to save the output, select the cell and choose the Cell ▶ Convert To ▶ Bitmap menu item.

License Information