This is documentation for Mathematica 3, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.1)
 Documentation / Mathematica / The Mathematica Book / Principles of Mathematica / Expressions  /

2.1.6 Expressions as Trees

  • Here is an expression in full form.
  • In[1]:= FullForm[x^3 + (1 + x)^2]


  • TreeForm prints out expressions to show their "tree" structure.
  • In[2]:= TreeForm[x^3 + (1 + x)^2]


    You can think of any Mathematica expression as a tree. In the expression above, the top node in the tree consists of a Plus. From this node come two "branches", x^3 and (1+x)^2. From the x^3 node, there are then two branches, x and 3, which can be viewed as "leaves" of the tree.

  • This matrix is a simple tree with just two levels.
  • In[3]:= TreeForm[{{a, b}, {c, d}}]


  • Here is a more complicated expression.
  • In[4]:= {{a b, c d^2}, {x^3 y^4}}


  • The tree for this expression has several levels. The representation of the tree here was too long to fit on a single line, so it had to be broken onto two lines.
  • In[5]:= TreeForm[%]


    The indices that label each part of an expression have a simple interpretation in terms of trees. Descending from the top node of the tree, each index specifies which branch to take in order to reach the part you want.