A binary expression tree is a specific kind of a binary tree used to represent expressions. Two common types of expressions that a binary expression tree can represent are algebraic and boolean. These trees can represent expressions that contain both unary and binary operators.

Each node of a binary tree, and hence of a binary expression tree, has zero, one, or two children. This restricted structure simplifies the processing of expression trees.
The leaves of a binary expression tree are operands, such as constants or variable names, and the other nodes contain operators. These particular trees happen to be binary, because all of the operations are binary, and although this is the simplest case, it is possible for nodes to have more than two children. It is also possible for a node to have only one child, as is the case with the unary minus operator. An expression tree, T, can be evaluated by applying the operator at the root to the values obtained by recursively evaluating the left and right subtrees.
Algebraic Expression Trees
An algebraic expression such as (3 * 7) + 9 consists of:Operands such as 3, 7, 9 or x, y, z,
Binary Operators such as +, – , *, /, DIV, MOD, ^
Unary Operators such as –
Algebraic expressions can be represented using a binary expression tree where:
- Each node is an operator,
- Each leaf is an operand.
Boolean expressions
If we traverse the expression in in order we get infix expression, if we traverse in pre order. We get prefix expression and we traverse in post order we get post fix expression.
Example
(5-x)*y+6/(x + z)
No comments:
Post a Comment