When building a unary operator in a tree, I usually draw it as a parent-child tree, for example:
-4
(-)
|
|
4
And when drawing a binary operator in a tree, it will have a left and right node, something like:
2-4
-
/ \
2 4
It makes sense to me where there needs to be associativity with multiple operators, even =
will have the lhs and rhs:
a=4
=
/ \
/ \
a (lv) 4 (rv)
But I don't really understand why a unary operator -- where there is just a single-child in the parse tree, would have the concept of associativity. Why would, for example, the unary plus/minus be right-associative rather than just being 'non-associativite' or 'doesnt-matter' ?
Here is a helpful answer too: https://stackoverflow.com/a/14084830/12283181. And:
Associativity specification is redundant for unary operators and is only shown for completeness: unary prefix operators always associate right-to-left (sizeof ++*p is sizeof(++(*p))) and unary postfix operators always associate left-to-right (a[1][2]++ is ((a[1])[2])++). Note that the associativity is meaningful for member access operators, even though they are grouped with unary postfix operators: a.b++ is parsed (a.b)++ and not a.(b++).
Source: C++ Operator Precedence