2
$\begingroup$
TreeForm[HoldForm[ (9-2^2+10 8)/9]]

produces this:

operations tree

Is there a way to use Divide instead of Power to $-1$ and delete the "HoldForm" node or change it another string? Change the "Times", "Plus", etc. nodes to the result of the operation?

I'm trying to make trees illustrating the order of operations.

$\endgroup$
2
  • 2
    $\begingroup$ Subtract and Divide are automagically converted (see their respective doc pages for details), but have a look at ExpressionTree[Unevaluated[(9 - 2^2 + 10 8)/9]]. $\endgroup$ May 27 at 20:40
  • 2
    $\begingroup$ Is this good?: Hold[(9 - 2^2 + 10 8)/9] /. Times[x_, Power[b_, -1]] :> HoldForm[Divide][x, b] /. op : Plus | Times | Power -> HoldForm[op] // First // TreeForm $\endgroup$
    – Michael E2
    May 27 at 20:40

1 Answer 1

3
$\begingroup$
TreeForm[First[
  Hold[1/9 (9 - 2^2 + (10 8)/7)] //. {x_/b_ :> 
      HoldForm[Divide][x, b]} /. 
   op : Plus | Times | Power -> HoldForm[op]]]

tree

🎩 tip: Michael E2's comment above and here

$\endgroup$
1
  • $\begingroup$ I think the first replacement needs to be ReplaceRepeated because of the way replacement matches are handled. Example: Hold[(9 - 2^2 + 10 8/7)/9] //. {Times[x_, Power[b_, -1]] :> HoldForm[Divide][x, b]} /. op : Plus | Times | Power -> HoldForm[op] // First // TreeForm. $\endgroup$
    – Michael E2
    May 27 at 20:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.