# Binary Trees for Equations

**URL:** https://discourse.julialang.org/t/binary-trees-for-equations/95748
**Category:** Performance
**Tags:** question
**Created:** [March 8, 2023, 4:41pm UTC](https://discourse.julialang.org/t/binary-trees-for-equations/95748 "2023-03-08T16:41:47Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Kianoush\_Ramezani\_sh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kianoush_ramezani_sh/32/45765_2.png) [@Kianoush\_Ramezani\_sh](https://discourse.julialang.org/u/Kianoush_Ramezani_sh)
#### Post date: [March 8, 2023, 4:41pm UTC](https://discourse.julialang.org/t/binary-trees-for-equations/95748/1 "2023-03-08T16:41:47Z")

</div>

Hi everyone,  
I want to represent an equation like a binary tree,  
e,g., I have this equation x\*\*2+x+1  
and in Julia I want to represent it as a tree with nodes  
e.g. + sign is a Node in that tree or x is Node or 1 is another Node.  
Please comment on how I can do this in Julia?

Thanks in advance

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [March 8, 2023, 5:45pm UTC](https://discourse.julialang.org/t/binary-trees-for-equations/95748/2 "2023-03-08T17:45:39Z")

</div>

`:(x^2 + x + 1)` returns an `Expr` object which is exactly a [syntax tree](https://en.wikipedia.org/wiki/Abstract_syntax_tree) for that expression. (Julia syntax for exponentiation is `x^2`, not `x**2` as in Python or Fortran.)

What are you trying to accomplish?

---

<div class="post-metadata">

### Author: ![Kianoush\_Ramezani\_sh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kianoush_ramezani_sh/32/45765_2.png) [@Kianoush\_Ramezani\_sh](https://discourse.julialang.org/u/Kianoush_Ramezani_sh)
#### Post date: [March 8, 2023, 6:24pm UTC](https://discourse.julialang.org/t/binary-trees-for-equations/95748/3 "2023-03-08T18:24:41Z")

</div>

Thanks for the kind answer,  
I have many equations like this, and I want to calculate the depth of their corresponding trees.

---

<div class="post-metadata">

### Author: ![empet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/empet/32/221303_2.png) [@empet](https://discourse.julialang.org/u/empet)
#### Post date: [March 8, 2023, 7:51pm UTC](https://discourse.julialang.org/t/binary-trees-for-equations/95748/4 "2023-03-08T19:51:07Z")

</div>

To plot any tree or any other graph type you should install a few packages.  
In this particular case, this is the code that defines and plots the binary tree associated to the expression x^2+x+1:

```julia
using Graphs, NetworkLayout, GraphPlot
A=[0 1 1 1 0 0; 
   0 0 0 0 1 1;
   0 0 0 0 0 0;
   0 0 0 0 0 0;
   0 0 0 0 0 0;
   0 0 0 0 0 0;]
nodelabel=["+", "*", "x", "1", "x", "x"]
G = DiGraph(A);
node_pos = buchheim(G.fadjlist)
xn, yn = getindex.(node_pos, 1), getindex.(node_pos, 2)
gplot(G, xn, -yn; nodelabel=nodelabel, NODELABELSIZE=6)

```

To control the plot aesthetics inspect the kwargs that can be passed to gplot as follows:

```julia
?gplot

```

![tree-graph-plot](https://global.discourse-cdn.com/julialang/original/3X/c/1/c131e220179a19739777196d2956d8115bccced4.png)

I’m not sure that this is the tree you expected. It’s for the first time when I learn that there exists a tree associated to an expression.

---

<div class="post-metadata">

### Author: ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)
#### Post date: [March 8, 2023, 10:06pm UTC](https://discourse.julialang.org/t/binary-trees-for-equations/95748/5 "2023-03-08T22:06:59Z")

</div>

I suggest you take a look at the manual page on [expressions](https://docs.julialang.org/en/v1/manual/metaprogramming/#Expressions-and-evaluation). You can define a recursive function `depth(ex::Expr)` which uses the attributes `ex.head` and `ex.args`
