I need to get the expression tree for some expression.
expr //TreeForm
The above grabs the expression tree but it isn't in some sort of usable format. Just an image.
Level[expr, {-1}, "Heads"->True]
This does a depth first traversal of the expression tree and does give me a list of all items in the expression tree. The issue with this is that:
expr = a+b*3*c+d
Level[expr, {-1}, "Heads"->True]
{Plus, a, Times, 3, b, c, d}
and now you can see that because of the location of d I have no information on what goes where with Levels.
So the question is, is there a way for me to get the expression tree in some format where I can actually tell where everything is supposed to go?
Acceptable formats would be of the form of {operator/operatorFunctionName, symbol/value, {operator/operatorFunctionName, symbol/value, {... continues as far as can go}}, moreSymbolsRelatedToFirstOperation}
OR
{operator/operatorFunctionName, operator/operatorFunctionName, ...} {{symbol/valueForFirstOperator, ...}, {symbol/valueForSecondOperator,...}, ...}
OR any other similar format.
expr
already is an expression tree. What format do you want? IsFullForm[expr]
helpful? $\endgroup$Expr
class provides a native Java representation of arbitrary Mathematica expressions. It has methods likehead()
,part()
,length()
,take()
,integerQ()
, etc. that you can use to analyze and deconstruct theExpr
on the Java side. Just mentioning this in case you might find it easier to pass the expression intact into Java and then walk through it with Java code. $\endgroup$