What is the most efficient way to get the nodes of a Tree
?
tree = Tree[1, {Tree[ 2, {Tree[5, {Tree[12, None], Tree[13, None]}], Tree[6, None]}], Tree[3, {Tree[7, {Tree[14, None], Tree[15, None], Tree[16, None]}], Tree[8, None], Tree[9, None]}], Tree[4, {Tree[10, {Tree[17, None], Tree[18, None], Tree[19, None]}], Tree[11, None]}]}];
Sort@Reap[TreeScan[Sow, tree]][[2, 1]] (* this seems wrong *)
Desired output: Range[19]
Follow-up: is there a way to get Annotate
to work on Tree
to store data in each node?
4
appears twice? Maybe you meant to useTree[1, {Tree[ 2, {Tree[5, {Tree[12, None], Tree[13, None]}], Tree[6, None]}], Tree[3, {Tree[7, {Tree[14, None], Tree[15, None], Tree[16, None]}], Tree[8, None], Tree[9, None]}], Tree[4, {Tree[10, {Tree[17, None], Tree[18, None], Tree[19, None]}], Tree[11, None]}]}]
, $\endgroup$Tree
data structure or if it's documented to work withVertexList
, but it seems to.ContainsExactly[VertexList[tree][[All, 1]], Range[19]]
returnsTrue
. $\endgroup$Sort@Reap[TreeScan[Sow, tree]][[2, 1]] == Range@19
yieldsTrue
. Can I ask for two things: one can you verify that, i.e that I did not make any mistakes? Two, do you still want a solution different thanSort@Reap[TreeScan[Sow, tree]][[2, 1]]
? Could you explain why? Many thanks! $\endgroup$