java - Expression Trees as Binary Trees -
i have simple question.
why expression trees modeled "binary trees" , not 'n ary trees' ?
is there reason why expression cannot modeled using n-ary tree?
there reasons why expression trees binary:
- the common expression trees represent arithmetic operations (
+
,-
,*
,/
) or logical predicates (and
,or
,not
,xor
). there binary (and unary) operations, binary trees make sense. could have example n-ary+
, complicates things without reason. from more theoretical perspective, if have n-ary tree, can represent using equivalent binary tree without losing anything. using n-ary
+
example following trees (one n-ary , 1 binary) considered same:+ + /|\ / \ b c + c / \ b
on other hand, there libraries use n-ary expression trees make sense. example, c# expression trees (from system.linq.expressions
namespace) use n-ary trees invocation expressions. so, expression f(a, b, c)
represented invocationexpression
looks this:
f /|\ b c
Comments
Post a Comment