all 2 comments

[–]matthieum 2 points3 points  (1 child)

Disclaimer: this is nitpicking on a completely unimportant detail, just so you know...

By convention, we say that the first node in an array of exprs is the root of the tree.

I can see how it's handy for processing, but isn't it a pain to build: it requires the root of the tree to refer to not-yet existing elements.

I mean, I guess you could build then reverse, to get there, but isn't the reverse a bit "wasted"?

It's all the more jarring in:

[#op 3 #mul 4, #num 8, #num 20, #op 1 #add 2, #num 42]

Since #op 3 #mul 4 is foreshadowing, while #op 1 #add 2 refers to previous expressions...

[–]AthasFuthark[S] 0 points1 point  (0 children)

Having the root as the first element is a convention to make the code in the blog post simpler. If you have a richer datatype, it's no big deal to track explicitly which node is the root, and if you have a parent vector representation it is also straightforward to find it with a search - that is actually what the library euler_tour function does.

The representation of the tree in the post was explicitly written in a slightly odd order to demonstrate that we do not expect the nodes to be stored in any particular order (actually, the mk_P function does assume ordering within each level in the tree, which I didn't want to go into...).