Tree¶
Tree[data, {children…}] is a tree node; None in place of the children
makes a leaf. The data and the children are read back with TreeData and
TreeChildren:
TreeLeaves gives the leaf subtrees, left to right:
$ wo 'TreeLeaves[Tree[1, {Tree[2, {Tree[4, None]}], Tree[3, None]}]]'
{Tree[4, None], Tree[3, None]}
TreeCases gives the subtrees whose data matches, children before their
parent, and TreeScan visits them in that same order:
$ wo 'TreeCases[Tree[1, {Tree[2, {Tree[4, None]}], Tree[3, None]}], _?EvenQ]'
{Tree[4, None], Tree[2, {Tree[4, None]}]}
$ wo 'Reap[TreeScan[Sow, Tree[1, {Tree[2, {Tree[4, None]}], Tree[3, None]}]]]'
{Null, {{4, 2, 3, 1}}}
A tree is an atom: it has no parts, no depth beyond itself, and counts as a
single leaf — so Map and Apply leave it alone: