Uses for Binary Trees…
-- Binary Expression Trees
CS122 Algorithms and Data Structures
n Binary trees are a good way to express
arithmetic expressions.
– The leaves are operands and the other
nodes are operators.
MW 11:00 am - 12:15 pm, MSEC 101 – The left and right subtrees of an operator
Instructor: Xiao Qin node represent subexpressions that must
be evaluated before applying the operator
Lecture 11: Binary Expression Trees
at the root of the subtree.
Merits of Binary Tree Form
Binary Expression Trees: Examples
n Left and right operands are easy to visualize
+ -
n Code optimization algorithms work with the
a b a binary tree form of an expression
a+b n Simple recursive evaluation of expression
// /
* + * +
e f
+ - e f
+ -
a b c d a b c d
Levels Indicate Precedence A Binary Expression Tree
The levels of the nodes in the tree indicate
‘*’
their relative precedence of evaluation (we
do not need parentheses to indicate precedence).
Operations at lower levels of the tree are ‘+’ ‘3’
evaluated later than those at higher
levels.
‘4’ ‘2’
The operation at the root is always the last
operation performed. What value does it have?
( 4 + 2 ) * 3 = 18
5 6
1
Inorder Traversal: (A + H) / (M - Y) Inorder Traversal (cont.)
Print second
tree
+
+ *
‘/’
+ g
‘+’ ‘-’ a *
‘A’ ‘H’ ‘M’ ‘Y’ b c * f
d e
Print left subtree first Print right subtree last
7 Inorder traversal yields: (a + (b * c)) + (((d * e) + f) * g)
Preorder Traversal: / + A H - M Y Postorder Traversal: A H + M Y - /
Print first Print last
tree tree
‘/’ ‘/’
‘+’ ‘-’ ‘+’ ‘-’
‘A’ ‘H’ ‘M’ ‘Y’ ‘A’ ‘H’ ‘M’ ‘Y’
Print left subtree second Print right subtree last Print left subtree first Print right subtree second
9 10
Traversals and Expressions class ExprTree
ExprTree
n Inorder traversal produces the infix
representation of the expression.
n postorder traversal produces the postfix ExprTree ‘*’
representation of the expression. ~ExprTree private:
n Preorder traversal produces a root
‘+’ ‘3’
build
representation that is the same as the
way that the programming language Lisp expression
processes arithmetic expressions! evaluate
‘4’ ‘2’
clear
12
2
class ExprTree {
public: Each node contains two pointers
ExprTree ( ); // Constructor struct TreeNode
~ExprTree ( ); // Destructor {
void build ( ); // build tree from prefix expression InfoNode info ; // Data member
void expression ( ) const;
TreeNode* left ; // Pointer to left child
// output expression in fully parenthesized infix form
TreeNode* right ; // Pointer to right child
float evaluate ( ) const; // evaluate expression
};
void clear ( ); // clear tree
void showStructure ( ) const; // display tree
private: NULL OPERAND 7 6000
void showSub( );
whichType operand
// recursive partners
struct TreeNode *root; left info right 14
};
int Eval ( TreeNode* ptr )
InfoNode has 2 forms { switch ( ptr->info.whichType )
{
enum OpType { OPERATOR, OPERAND } ; case OPERAND : return ptr->info.operand ;
struct InfoNode case OPERATOR :
{ switch ( tree->info.operation )
OpType whichType; {
union // ANONYMOUS union case ‘+’ : return ( Eval ( ptr->left ) + Eval ( ptr->right ) ) ;
{
char operation ; case ‘-’ : return ( Eval ( ptr->left ) - Eval ( ptr->right ) ) ;
int operand ;
} case ‘*’ : return ( Eval ( ptr->left ) * Eval ( ptr->right ) ) ;
};
case ‘/’ : return ( Eval ( ptr->left ) / Eval ( ptr->right ) ) ;
}
}
OPERATOR ‘+’ OPERAND 7 }
whichType operation whichType operand
15 16
Constructing an Expression Tree Expression Tree Algorithm
n There is a simple O(N) stack-based n Read the postfix expression one symbol at at
algorithm to convert a postfix time:
expression into an expression tree. – If the symbol is an operand, create a one-node
tree and push a pointer to it onto the stack.
n Recall we also have an algorithm to – If the symbol is an operator, pop two tree pointers
convert an infix expression into postfix, T1 and T2 from the stack, and form a new tree
whose root is the operator, and whose children
so we can also convert an infix are T1 and T2.
expression into an expression tree – Push the new tree pointer on the stack.
without difficulty (in O(N) time).
3
Example Example
ab+cde+ :
ab+ :
+ c d e
a b
a b
+
Note: These stacks are depicted horizontally. + c +
a b
a b d e
Example Example
ab+cde+* : ab+cde+**:
+
* *
a b c +
+
d e *
a b c +
d e