ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ExpressionTree.h

The tree of an arithmetic expression (the parse tree), beyond the book. More...

Files

file  ADS/tree/ExpressionTree.h
 The tree of an arithmetic expression (the parse tree), beyond the book: a binary tree [see BinaryTree.h] whose leaves are the operands, numbers or names of variables, and whose internal nodes are the binary operators +, -, *, / and ^ (the power), the left subtree being the left operand.

Functions

BinaryTree< std::string > * ADS::expressionTree (std::string_view e)
 the expression tree of the postfix expression \( e \), whose tokens are separated by spaces; the caller owns it
double ADS::evaluate (const BinaryTree< std::string > *t, const std::map< std::string, double > &env={})
 the value of the expression \( t \), with the variables valued by env; a leaf is a number, possibly signed (e.g., -3, +2.5, .5), or a variable
std::string ADS::prefixForm (const BinaryTree< std::string > *t)
 the prefix form of \( t \), each subtree in parentheses, by a previsit (section 5.6)
std::string ADS::infixForm (const BinaryTree< std::string > *t)
 the infix form of \( t \), fully parenthesized, by an invisit (section 5.6)

Detailed description

The tree of an arithmetic expression (the parse tree), beyond the book.

The definitions, the invariants and the costs of the area are in Theory of trees.

Function documentation

◆ evaluate()

double ADS::evaluate ( const BinaryTree< std::string > * t,
const std::map< std::string, double > & env = {} )

the value of the expression \( t \), with the variables valued by env; a leaf is a number, possibly signed (e.g., -3, +2.5, .5), or a variable

In pseudocode (section 5.6), a postvisit:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{evaluate}( t ) \cr 2 & \quad \textbf{if}\ t \text{ is a leaf}\ \textbf{then return} \ \text{the number in } t \text{, or the value of the variable in } t \cr 3 & \quad \textbf{return}\ \text{the operator of } t \text{ applied to } \mathrm{evaluate}( t.\mathrm{left}() ) \text{ and } \mathrm{evaluate}( t.\mathrm{right}() ) \end{array}} \]

Precondition
\( t \) is not nil, env values each variable, no division by 0

◆ expressionTree()

BinaryTree< std::string > * ADS::expressionTree ( std::string_view e)

the expression tree of the postfix expression \( e \), whose tokens are separated by spaces; the caller owns it

In pseudocode (sections 5.5 and 5.6, as evalPostfix() of Expressions.h with trees in place of values):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{expressionTree}( e )\quad \triangleright \ e \text{ in postfix form} \cr 2 & \quad S \gets \text{an empty stack of trees} \cr 3 & \quad \textbf{foreach}\ \text{token } x \text{ of } e \text{, from the left}\ \textbf{do} \cr 4 & \qquad \textbf{if}\ x \text{ is an operator}\ \textbf{then}\ r \gets \mathrm{pop}( S );\ l \gets \mathrm{pop}( S );\ \mathrm{push}( S , \mathrm{join}( x , l , r ) ) \cr 5 & \qquad \textbf{else}\ \mathrm{push}( S , \text{the leaf } x ) \cr 6 & \quad \textbf{return}\ \mathrm{pop}( S )\quad \triangleright \ \text{the only tree left} \end{array}} \]

Precondition
\( e \) is a well formed postfix expression

◆ infixForm()

std::string ADS::infixForm ( const BinaryTree< std::string > * t)

the infix form of \( t \), fully parenthesized, by an invisit (section 5.6)

In pseudocode (section 5.6):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{infixForm}( t ) \cr 2 & \quad \textbf{if}\ t \text{ is a leaf}\ \textbf{then return} \ t.\mathrm{read}() \cr 3 & \quad \textbf{return}\ \text{“(”} \cdot \mathrm{infixForm}( t.\mathrm{left}() ) \cdot t.\mathrm{read}() \cdot \mathrm{infixForm}( t.\mathrm{right}() ) \cdot \text{“)”} \end{array}} \]

Precondition
\( t \) is not nil

◆ prefixForm()

std::string ADS::prefixForm ( const BinaryTree< std::string > * t)

the prefix form of \( t \), each subtree in parentheses, by a previsit (section 5.6)

Precondition
\( t \) is not nil