0% found this document useful (0 votes)
38 views1 page

Expression Tree Algorithms

The algorithm constructs an expression tree from a postfix expression by reading symbols one by one. Operands are made into single node trees, pushed onto a stack. Operators pop two trees from the stack, create a new tree with the operator as the root and the popped trees as children, pushing it to the stack. The final tree is the only element remaining in the stack. The algorithm computes the value of an expression tree by recursively evaluating left and right subtrees if the root is an operator, then applying the operator to the results.

Uploaded by

Ryan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views1 page

Expression Tree Algorithms

The algorithm constructs an expression tree from a postfix expression by reading symbols one by one. Operands are made into single node trees, pushed onto a stack. Operators pop two trees from the stack, create a new tree with the operator as the root and the popped trees as children, pushing it to the stack. The final tree is the only element remaining in the stack. The algorithm computes the value of an expression tree by recursively evaluating left and right subtrees if the root is an operator, then applying the operator to the results.

Uploaded by

Ryan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Algorithm to construct an expression tree from a given postfix expression:

1. Create a Stack.

2. Read the given postfix one symbol at a time, and repeat Steps 3 to 4.

3. If the symbol is an operand, then

o Create a single node expression tree with the operand.


o Push the root pointer of the generated tree into the stack.

4. If the symbol is an operator, then

o Pop out pointers to the two trees (T1(first popped out) and T2) from the stack.
o Create a new tree with the operator as the root and whose left-child
and right-child point to trees T2 and T1, respectively.
o Push the root pointer of this new tree back into the stack.
5.The stack will contain a single tree; return it as the final expression tree.

Algorithm to compute value of an expression from the expression tree

Let ROOT be a pointer to the expression tree node.


1. If ROOT is not NULL, then
If symbol at ROOT is operand, then
Return the operand.
2. Else // Symbol at ROOT is operator.
Recursively compute the value of ROOT.left, operand1
Recursively compute the value of ROOT.right, operand2
3. Apply the operator at ROOT to the values stored in operand1 and operand2
4. Return the result obtained in Step 3.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy