# TreeForm outputs different results for the same input, Why?

Bug introduced in 8.0.4 or earlier and persisting through 11.0.1

First of all, please look at the below inputs and outputs:

In:= $a = Array[# Range[#] &, {5}] Out= {{1}, {2, 4}, {3, 6, 9}, {4, 8, 12, 16}, {5, 10, 15, 20, 25}} In:=$af = Flatten[$a, {{1}, {2}}] Out= {{1}, {2, 4}, {3, 6, 9}, {4, 8, 12, 16}, {5, 10, 15, 20, 25}} In:=$a === $af Out= True In:= FullForm[$a] === FullForm[$af] Out= True In:= TreeForm[$a]

Out:= In:= TreeForm[$af] Out:= In:= TreeForm[$a] === TreeForm[$af] Out= True  Then, I have natural questions: Why are there the differences between the output of TreeForm[$a] and that of TreeForm[$af]? What makes these differences? Though Mathematica says "True"s for the checking equalities. • Post your code as formatted text, not as graphics. Jun 30 '16 at 17:14 • @MarcoB I corrected it, sorry. Jun 30 '16 at 17:28 • This is weird - I would say that TreeForm[Array[ # Range[#] &, {5}]], the first one, is clearly wrong. Also, the output of TreeForm@Array[Range[#] &, {5}] is even weirder... Jun 30 '16 at 17:39 • Okay, so this is a known bug where TreeForm will fail on arrays where the sublists are packed. Compare the output of DeveloperPackedArrayQ /@ a and DeveloperPackedArrayQ /@$af . A workaround is to unpack the subarrays, TreeForm[\$a /. {x_?DeveloperPackedArrayQ :> DeveloperFromPackedArray[x]}]  Jun 30 '16 at 17:47
• @TaikiBessho - that is correct. I give a workaround function below that should suit your needs. Jun 30 '16 at 18:29

The difference between your two examples is that one uses packed arrays and the other doesn't. The lists generated by Range are, by default, packed.

<< Developer

PackedArrayQ /@ {{1, 2, 3, 4}, Range}
(* {False, True} *)


There is a bug, where if the sublists in the array fed to TreeForm are packed, then the function doesn't behave properly:

TreeForm /@ {{{1, 2, 3, 4}}, {Range}} As a workaround you can first search for any packed arrays, unpack them, and then feed the result to TreeForm

treeForm[arr_] :=
TreeForm[arr /. {x_?PackedArrayQ :> FromPackedArray[x]}]

treeForm /@ {{{1, 2, 3, 4}}, {Range}} Or, if you haven't loaded the Developer package, you can use this:

treeForm[arr_] :=
TreeForm[arr /. {x_?DeveloperPackedArrayQ :>
DeveloperFromPackedArray[x]}]
`